Golang-based XLS read class library: XLS

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Golang Write XLS read class library, can realize the reading function of XLS table

func (w *workbook) Readallcells () (res [][]string) {for _, Sheet: = Range W.sheet s {w.preparesheet (sheet) if sheet. MaxRow! = 0 {Temp: = make ([][]string, Sheet. maxrow+1) for k, row: = Range sheet. Rows {data: = make ([]string, 0) If Len (row. Cols) > 0 {for _, col: = Range row. Cols {if uint16 (len (data)) <= Col. Lastcol () {data = append (data, make ([]string, Col.                        Lastcol ()-uint16 (len (data)) +1) ...) } str: = Col. String (W) for I: = uint16 (0); I < Col. Lastcol ()-col. Firstcol (+1); i++ {Data[col.            Firstcol () +i] = Str[i]}} temp[k] = data}        } res = append (res, temp ...) }} return} 

Read Example:

Package main import (    "FMT"    "GITHUB.COM/TEALEG/XLSX") func main () {    excelfilename: = "/home/tealeg/ Foo.xlsx "    xlfile, err: = xlsx. OpenFile (excelfilename)    if err! = Nil {        ...    }    For _, Sheet: = range Xlfile.sheets {        for _, Row: = Range sheet. Rows {            for _, Cell: = Range row. Cells {                FMT. Printf ("%s\n", cell. String ())}}}}    

Write an example:

Package main import (    "FMT"    "GITHUB.COM/TEALEG/XLSX") func main () {    var file *xlsx. File    var sheet *xlsx. Sheet    var row *xlsx. Row    var cell *xlsx. Cell    var err error     File = xlsx. NewFile ()    sheet = file. Addsheet ("Sheet1")    row = sheet. AddRow ()    cell = row. Addcell ()    cell. Value = "I am a cell!"    Err = file. Save ("myxlsxfile.xlsx")    if err! = Nil {        FMT. Printf (Err. Error ())    }}

 

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.