26
Nov

Working with Spreadsheets in Go


Hello, in this video I will introduce you to how to operate an Excel document in the Go language. The excelize is a library written in pure Go providing a set of functions that allow you to write to and read from XLSX files. Install the excelize by go get Well, now it’s installed. The first example is: Read the content in the Excel document named “Online Sales Tracker1”. I’ll open this document with Excel software first and show you the contents and style of the document. Create a file called 01.read.go, Define package Name: main import standard format library and Excelize library, create main function. Open the file using the OpenFile method provided by Excelize, and don’t forget to do the error checking and processing. Skip the chart area, traverse the rows variable, and output the content. Perform this program validation effect when you are finished. You can see that the results obtained by the program are in line with expectations. In the following example, we write a program using the go language to generate the document as shown like this As in the previous example, first import standard format library and Excelize library, create main function. Declares categories variable definition classification. Declares that the values variable defines the values of each cells in the B2:D4 area. Set the values of each cells based on categories and values. Save the file as 02.create.go Execute the program and use Excel application to open the generated file to verify the effect. Add chart to Excel Document Next, continue with the above, creating a chart based on the data from the previous example. first import standard format library and Excelize library, create main function. Create a chart by AddChart method, the first parameter of the function is the name of the worksheet, the second parameter is the cell coordinates, and the third parameter is the Chart property formatted in JSON We need to declare the properties of the chart in JSON format, such as categories, values, titles, etc. Save the file as 03.chart.go Execute the program and use Excel application to open the generated file to verify the effect. Add picture to Excel document I will show you how to use Excelize to add an image to an Excel document. first import standard format library and Excelize library, create main function. Add an image to the Excel document by the AddPicture method provided by Excelize. The first parameter of the function is the name of the worksheet, the second parameter is the cell coordinate, and the third parameter is the path of the image, the last parameter is the properties of the picture in JSON format. We need to declare the properties of the image in JSON format, such as offset scaling, etc. Save the file as 04.picture.go Execute the program and use Excel application to open the generated file to verify the effect. Please visit the GitHub repo URL on the screen to view the source code. Thanks for your watching ^_^

Tags: , , , , , ,

One Comment

  • Transfacil Apps says:

    What if I want to read any sheet names that is in the file? I'm working with this possibility, the user sends an archive with any possible name in the .xlsx file.

Leave a Reply

Your email address will not be published. Required fields are marked *