How to Convert Excel (XLSX) to XML using Go

Cloudmersive
2 min readOct 31, 2022

--

For all its exciting tools & processing capabilities, Excel is essentially designed to make importing, mapping & manipulating XML data as easy as possible. With that in mind, XML is the ideal format for sharing Excel data across networks; not only will XML transform seamlessly back into XLSX format when the need arises, it’ll also offer compatibility with other apps & databases that XLSX schema is ill-suited for.

With the help of our XLSX to XML conversion API, you won’t need to waste any time converting between these two formats. All you’ll need to do is call the function with your XLSX file input, and the API will return the encoding for an XML version of that file. To authenticate the service, you’ll only need a free-tier Cloudmersive API key, which you can get by registering a free account on our website (free accounts come with a limit of 800 API calls per month & zero additional commitments). Below, I’ve provided complementary code examples in Go to help you structure your API call.

To call this API, simply copy & paste the below code into your file (and include your file input & API key where indicated by the code comments) and you’re all done:

package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/convert/xlsx/to/xml"
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("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))
}

Nice and easy!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet