Go language export Excel file (. xlsx)

Source: Internet
Author: User
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)
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.