How to Convert an Excel XLSX Spreadsheet to CSV (One or Multiple Worksheets) using Go

Cloudmersive
3 min readNov 9, 2022

In another article, I highlighted a Cloudmersive Data Conversion API which converts multiple CSV files into a single Excel spreadsheet with multiple worksheets, one per CSV file. In this article, I’ll show you how to make the opposite conversion with two separate outcomes: one generating single CSV file from multiple XLSX worksheets, and one resulting in multiple CSV files (one per worksheet).

Each outcome requires a slightly different API call, so I’ll demonstrate how to call both APIs below with complementary Go code examples (for code examples in other languages like C#, Java, Python & more, visit our exhaustive API documentation page). If you’re new to using Cloudmersive APIs, make sure you have at least a free-tier API key handy before you copy from the examples below; you can get one by registering a free-tier account on our website (free accounts come with a limit of 800 API calls per month and zero additional commitments).

Let’s begin by calling the API iteration which converts a single XLSX worksheet to a CSV file. It’s important to note first that if our XLSX file contains multiple worksheets, this API will only convert the first worksheet to CSV, and it will ignore the rest. All we need to do is copy & paste the below code into our file, configure the necessary fields (your file & API key), and call the function:

package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/convert/xlsx/to/csv"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("inputFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("outputEncoding", "<string>")
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

Now let’s move on to call the API iteration which will convert each worksheet within an XLSX file into its own CSV file. The code is very similar, and the same inputs are required as in the previous API iteration:

package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/convert/xlsx/to/csv/multi"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("inputFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("outputEncoding", "<string>")
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

After that, you’re done — no more code examples are required. If you’d like to specify the text encoding for your resulting files, simply specify your preferred value (UTF-8, ASCII, UTF-32 are your options) as a string within the outputEncoding field. The default value for this field is UTF-8.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.