Go language reading and writing Excel

Source: Internet
Author: User

What's excelize? Golang writes a class library that is used to manipulate office Excel documents, based on the ECMA-376 office OpenXML standard. You can use it to read, write to XLSX files. Compared to other open source class libraries, Excelize supports writing documents with pictures (tables), supports inserting pictures into Excel, and does not lose the chart style after saving.

Installation
go get github.com/360EntSecGroup-Skylar/excelize
Create XLSX
package mainimport (    "fmt"    "github.com/360EntSecGroup-Skylar/excelize")func main() {    xlsx := excelize.NewFile()    // Create a new sheet.    index := xlsx.NewSheet("Sheet2")    // Set value of a cell.    xlsx.SetCellValue("Sheet2", "A2", "Hello world.")    xlsx.SetCellValue("Sheet1", "B2", 100)    // Set active sheet of the workbook.    xlsx.SetActiveSheet(index)    // Save xlsx file by the given path.    err := xlsx.SaveAs("./Book1.xlsx")    if err != nil {        fmt.Println(err)    }}
Read and write existing documents
package mainimport (    "fmt"    "github.com/360EntSecGroup-Skylar/excelize")func main() {    xlsx, err := excelize.OpenFile("./Book1.xlsx")    if err != nil {        fmt.Println(err)        return    }    // Get value from cell by given worksheet name and axis.    cell := xlsx.GetCellValue("Sheet1", "B2")    fmt.Println(cell)    // Get all the rows in the Sheet1.    rows := xlsx.GetRows("Sheet1")    for _, row := range rows {        for _, colCell := range row {            fmt.Print(colCell, "\t")        }        fmt.Println()    }}
Insert a chart into Excel

Package Mainimport ("FMT" "Github.com/360entsecgroup-skylar/excelize") func main () {categories: = Map[string]str ing{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"} Values: = Map[string]int {"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8} xlsx: = Excelize. NewFile () for k, V: = Range Categories {xlsx. Setcellvalue ("Sheet1", K, V)} for K, V: = range values {xlsx. Setcellvalue ("Sheet1", K, V)} xlsx. Addchart ("Sheet1", "E1", ' {"type": "Col3dclustered", "series": [{"Name": "sheet1! $A $ $", "categories": "sheet1! $B $: $D $ 1 "Values": "sheet1! $B $: $D $"},{"name": "sheet1! $A $", "categories": "sheet1! $B $: $D $", "values": "sheet1! $B $: $D $ "},{" name ":" sheet1! $A $4 "," categories ":" sheet1! $B $: $D $ "," values ":" sheet1! $B $4: $D $4 "}]," title ": {" name ":"    Fruit 3D Clustered Column Chart "}} ')//Save xlsx file by the given path. ERR: = xlsx. SaveAs ("./book1.xlsx") if err! = Nil {fmt. Println(ERR)}} 
Insert a picture into Excel
package mainimport ("Fmt" _ "Image/gif" _ "Image/jpeg" _ "Image/png" "github.com /360entsecgroup-skylar/excelize ") Func main () {xlsx, err: = Excelize. OpenFile ("./book1.xlsx") if err! = Nil {fmt. PRINTLN (ERR) return}//Insert a picture. Err = xlsx. AddPicture ("Sheet1", "A2", "./image1.png", "" ") if err! = Nil {fmt. PRINTLN (ERR)}//Insert a picture to worksheet with scaling. Err = xlsx. AddPicture ("Sheet1", "D2", "./image2.jpg", ' {"X_scale": 0.5, "Y_scale": 0.5} ') if err! = Nil {fmt. PRINTLN (ERR)}//Insert a picture of offset in the cell with printing support. Err = xlsx. AddPicture ("Sheet1", "H2", "./image3.gif", "{" X_offset ":", "Y_offset": Ten, "Print_obj": True, "Lock_aspect_ratio": False, "locked": false} ') if err! = Nil {fmt. PRINTLN (ERR)}//Save the xlsx file with the Origin path. Err = xlsx. Save () if err! = Nil {fmt. PRINTLN (Err)}} 

There are other features that are not listed here, detailed use of documentation and access to post-maintenance updates can be obtained from the project's home page

Github.com/360entsecgroup-skylar/excelize

Go language reading and writing Excel

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.