This is a creation in Article, where the information may have evolved or changed.
Read Excel Operations
<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">import ("FMT" "github.com/tealeg/xlsx") func main () {excelfilename: = "Here is the path to Excel" Xlfile, error: = xlsx . OpenFile (excelfilename) if error! = Nil {...} For _, Sheet: = Range Xlfile.sheets {For _, Row: = Range sheet. Rows {for _, Cell: = Range row. Cells {fmt. Printf ("%s\n", cell. String ())</textarea> }}}}
| 123456789101112131415161718 |
Import ( "FMT" "Github.com/tealeg/xlsx")funcMain() { Excelfilename := "Here is the path to Excel" Xlfile, Error := xlsx.OpenFile(Excelfilename) if Error != Nil { ... } for _, sheet := RangeXlfile.Sheets { for _, Row := Rangesheet.Rows { for _, Cell := RangeRow.Cells { FMT.Printf("%s\n", Cell.String()) } } }} |
Write to Excel:
Package <textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">mainimport ("OS" "Encoding/csv") func main () {f, err: = OS. Create ("Test.xls") if err! = Nil {panic (err)} defer F.close () f.writestring ("\XEF\XBB\XBF")//write utf- 8 BOM w: = csv. Newwriter (f) w.write ([]string{"number", "Name", "Age"}) W.write ([]string{"1", "Zhang San", "23°c"}) w.write ([]string{"2", "John Doe", "24"}) W.write ([]string{"3", "Harry", "+"}) w.write ([]string{"4", "Zhao Liu", "+"}) W.flush ()}</textarea>
| 12345678910111213141516171819 |
Package MainImport ( "OS" "Encoding/csv")funcMain() { F, Err := OS.Create("Test.xls") if Err != Nil { Panic(Err) } defer F.Close() F.writestring("\XEF\XBB\XBF") //write UTF-8 BOM W := CSV.Newwriter(F) W.Write([]string{"Number","Name","Age"}) W.Write([]string{"1","Zhang San","All"}) W.Write([]string{"2","John Doe"," the"}) W.Write([]string{"3","Harry","very"}) W.Write([]string{"4","Zhao Liu","a"}) W.Flush()} |
golang implementation of Excel operations