This is a creation in Article, where the information may have evolved or changed.
There is a lot of information about exporting Excel online, most of which is creating a new. xlsx file (the. xls library does not provide write functionality). But in this case, the export of Excel is often ugly, the other is to open a template, filled with things, often the first line fixed.
First method (directly created)
package mainimport ( "github.com/ Tealeg/xlsx ") Func main () {file: = Xlsx () Sheet: = File ( "Sheet1" ) Row: = Sheet () row (1 )//Set the height of each row cell: = Row () cell = "haha" cell = Row () cell = "Xixi" err: = File ( "file.xlsx" ) if err! = Nil {panic (err)}}
Once this is run, a file.xlsx file is generated directly.
Second method (open file to write data)
Package Mainimport ("Github.com/tealeg/xlsx") Func main () {file, err: = xlsx. OpenFile("Format.xlsx"If err! = Nil {panic (err)} First: = File. Sheets[0] Row: = First. AddRow() row. SETHEIGHTCM(1) Cell: = row. Addcell() cell. Value="1"Cell = row. Addcell() cell. Value="Zhang San"Cell = row. Addcell() cell. Value="Male"Cell = row. Addcell() cell. Value=" "Err = File. Save("File.xlsx"If err! = Nil {panic (err)}}
The template file format that opens is:
The last file to write data is:
Summary
The above two methods in fact, there is no difference in general, are similar, just use some different, add a little, the second way to open the file must be. xlsx file, otherwise there is no way to open. The second method is to simply add a row and then write something. Choose a method according to your needs ~
Add: Download the path file from the Internet
This. Ctx. Output. Header("Accept-ranges","bytes") This. Ctx. Output. Header("Content-disposition","attachment; Filename= "+fmt. Sprintf('%s ',"File.xls")//filename This. Ctx. Output. Header("Cache-control","Must-revalidate, post-check=0, pre-check=0") This. Ctx. Output. Header("Pragma","No-cache") This. Ctx. Output. Header("Expires","0")//The main sentence of the HTTP. Servefile(This. Ctx. Responsewriter, this. Ctx. Request, path)