@
1. 从几个示例开始
1.1 创建、写入
| package main |
| |
| import ( |
| "fmt" |
| |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f := excelize.NewFile() |
| |
| indexXiShu := f.NewSheet("西蜀") |
| f.NewSheet("东吴") |
| f.NewSheet("曹魏") |
| |
| f.SetCellValue("西蜀", "B2", "刘备") |
| f.SetCellValue("西蜀", "B3", "关羽") |
| f.SetCellValue("西蜀", "B4", "张飞") |
| f.SetCellValue("东吴", "B2", "孙权") |
| f.SetCellValue("曹魏", "B2", "曹操") |
| |
| f.SetActiveSheet(indexXiShu) |
| |
| if err := f.SaveAs("sanGuo.xlsx"); err != nil { |
| fmt.Println(err) |
| } |
| } |
- 生成表格如下

1.2 读表
读上例中创建工作本中的表
| package main |
| |
| import ( |
| "fmt" |
| |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func main() { |
| f, err := excelize.OpenFile("sanGuo.xlsx") |
| if err != nil { |
| fmt.Println(err) |
| return |
| } |
| defer func() { |
| if err := f.Close(); err != nil { |
| fmt.Println(err) |
| } |
| }() |
| |
| cell, err := f.GetCellValue("东吴", "B2") |
| if err != nil { |
| fmt.Println(err) |
| return |
| } |
| fmt.Printf("打印 东吴sheet B2格 内容: %q\n",cell) |
| |
| |
| rows, err := f.GetRows("西蜀") |
| if err != nil { |
| fmt.Println(err) |
| return |
| } |
| fmt.Printf("打印西蜀 sheet 内容 : %+v\n",rows) |
| for rowNumber, row := range rows { |
| fmt.Printf("行号:%d\n",rowNumber) |
| for a, colCell := range row { |
| fmt.Printf(" 单元格 %d : %q\n",a,colCell) |
| } |
| fmt.Println() |
| } |
| } |
结果输出
| 打印 东吴sheet B2格 内容: "孙权" |
| 打印西蜀 sheet 内容 : [[] [ 刘备] [ 关羽] [ 张飞]] |
| 行号:0 |
| |
| 行号:1 |
| 单元格 0 : "" |
| 单元格 1 : "刘备" |
| |
| 行号:2 |
| 单元格 0 : "" |
| 单元格 1 : "关羽" |
| |
| 行号:3 |
| 单元格 0 : "" |
| 单元格 1 : "张飞" |
- 以上代码,我们打印
rows
变量时可以看到,实际是用一个[][]string
表示了sheet中有值的区域: [ [A1 A2] [B1 B2] [C1 C2] ]
(只是有些值是空的,我们看不到)
- 在我们遍历单元格打印的时候,可以验证以上结果。
1.3 数据流
| package main |
| |
| import ( |
| "fmt" |
| "net/http" |
| |
| "github.com/xuri/excelize/v2" |
| ) |
| |
| func process(w http.ResponseWriter, req *http.Request) { |
| file, _, err := req.FormFile("file") |
| if err != nil { |
| fmt.Fprintf(w, err.Error()) |
| return |
| } |
| defer file.Close() |
| f, err := excelize.OpenReader(file) |
| if err != nil { |
| fmt.Fprintf(w, err.Error()) |
| return |
| } |
| f.NewSheet("NewSheet") |
| w.Header().Set("Content-Disposition", "attachment; filename=Book1.xlsx") |
| w.Header().Set("Content-Type", req.Header.Get("Content-Type")) |
| if _, err := f.WriteTo(w); err != nil { |
| fmt.Fprintf(w, err.Error()) |
| } |
| return |
| } |
| |
| func main() { |
| http.HandleFunc("/process", process) |
| http.ListenAndServe(":1840", nil) |
| } |
| [root@liubei-02 ~] |
| % Total % Received % Xferd Average Speed Time Time Time Current |
| Dload Upload Total Spent Left Speed |
| 100 15125 0 7460 100 7665 129k 132k --:--:-- --:--:-- --:--:-- 133k |
| curl: Saved to filename 'Book1.xlsx' |
2. 工作簿操作
2.1 创建工作簿
见 1.1
2.2 打开工作簿
| func OpenFile(filename string, opt ...Options) (*File, error) |
见1.2
2.3 保存工作簿
| func (f *File) SaveAs(name string, opt ...Options) error |
见 1.1
2.4 关闭工作簿
| func (f *File) Close() error |
见1.2
2.5 设置默认字体
| func (f *File) SetDefaultFont(fontName string) |
2.6 获取默认字体
| func (f *File) GetDefaultFont() string |
3. 文件 Writer
3.1 Write
将当前文件内容写入给定的 io.Writer
| func (f *File) Write(w io.Writer) error |
3.2 WriteTo
| func (f *File) WriteTo(w io.Writer) (int64, error) |
见 “1.3 数据流”
3.3 WriteToBuffer
| func (f *File) WriteToBuffer() (*bytes.Buffer, error) |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了