Golang操作Office Excel 文档类库Excelize
来源:互联网 发布:网络最常鼓掌处理方法 编辑:程序博客网 时间:2024/06/04 19:24
Golang操作Office Excel 文档类库Excelize
转自:http://studygolang.com/articles/9623
Excelize 是 Golang 编写的一个用来操作 Office Excel 文档类库,基于微软的 Office OpenXML 标准。可以使用它来读取、写入 XLSX 文件。相比较其他的开源类库,Excelize 支持写入原本带有图片(表)的文档,还支持向 Excel 中插入图片,并且在保存后不会丢失图表样式。
安装
go get github.com/Luxurioust/excelize
创建 XLSX
package mainimport ( "fmt" "os" "github.com/Luxurioust/excelize")func main() { xlsx := excelize.CreateFile() // Create a new sheet. xlsx.NewSheet(2, "Sheet2") // Set value of a cell. xlsx.SetCellValue("Sheet2", "A2", "Hello world.") xlsx.SetCellValue("Sheet1", "B2", 100) // Set active sheet of the workbook. xlsx.SetActiveSheet(2) // Save xlsx file by the given path. err := xlsx.WriteTo("/tmp/Workbook.xlsx") if err != nil { fmt.Println(err) os.Exit(1) }}
读写已有文档
package mainimport ( "fmt" "os" "github.com/Luxurioust/excelize")func main() { xlsx, err := excelize.OpenFile("/tmp/Workbook.xlsx") if err != nil { fmt.Println(err) os.Exit(1) } // Get value from cell by given sheet index and axis. cell := xlsx.GetCellValue("Sheet1", "B2") fmt.Println(cell) // Get all the rows in a sheet. rows := xlsx.GetRows("Sheet2") for _, row := range rows { for _, colCell := range row { fmt.Print(colCell, "\t") } fmt.Println() }}
向 Excel 中插入图片
package mainimport ( "fmt" "os" _ "image/gif" _ "image/jpeg" _ "image/png" "github.com/Luxurioust/excelize")func main() { xlsx, err := excelize.OpenFile("/tmp/Workbook.xlsx") if err != nil { fmt.Println(err) os.Exit(1) } // Insert a picture. err = xlsx.AddPicture("Sheet1", "A2", "/tmp/image1.gif", "") if err != nil { fmt.Println(err) } // Insert a picture to sheet with scaling. err = xlsx.AddPicture("Sheet1", "D2", "/tmp/image2.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`) if err != nil { fmt.Println(err) } // Insert a picture offset in the cell with printing support. err = xlsx.AddPicture("Sheet1", "H2", "/tmp/image3.gif", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`) if err != nil { fmt.Println(err) } // Save the xlsx file with the origin path. err = xlsx.Save() if err != nil { fmt.Println(err) os.Exit(1) }}
还有其他一些功能,在这里就不一一列举了,详细使用文档以及获取后期的维护更新可以从项目的主页获取 https://github.com/Luxurioust/excelize
0 0
- Golang操作Office Excel 文档类库Excelize
- Go实战--golang中操作excel(tealeg/xlsx、360EntSecGroup-Skylar/excelize)
- Apache POI 操作Office文档(Excel,Word)
- Jacob操作office文档(Word,PPT,Excel)
- poi操作office文档
- jacob操作office文档
- php操作office文档
- jacob操作office文档
- Office编程 VS操作Office文档
- 操作服务器端的OFFICE文档
- C#的office文档操作
- C#的office文档操作
- C#的office文档操作
- jacob操作window office文档
- C# OFFICE WORD EXCEL操作
- java操作office和pdf文件:读取word,excel和pdf文档内容
- Python office编程:利用xlrd模块读取并简单操作Excel文档
- Golang标准库文档
- RecyclerView 复用与 CheckBox 结合使用导致的问题
- 170321 Qt远程控制界面、文件图像,汇编第四章实验
- Android-Mina-Java.lang.NoclassDefFoundError:org.apache.mina.transport.socket.NioSocketConnector
- Table Decorations CodeForces
- 关于IE11访问百度贴吧不能翻页的解决办法
- Golang操作Office Excel 文档类库Excelize
- Google推荐的图片加载库Glide介绍
- RocketMQ(三)原理与实践
- 1
- android studio project 打不开问题
- 通过QQ号码获取QQ头像、昵称以及空间头像
- 2
- xampp + php5.6 + sqlserver2005数据连接
- UEditor的自动保存