Go 生成(读取)xlsx文件

目录
1. go生成xlsx文件
go get github.com/xuri/excelize

生成 excel 文件

package main

import "github.com/xuri/excelize"
import "fmt"

func main() {

	xlsx := excelize.NewFile()
	// Create a new sheet.
	index := xlsx.NewSheet("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(index)
	// Save xlsx file by the given path.
	err := xlsx.SaveAs("./Book1.xlsx")
	if err != nil {
		fmt.Println(err)
	}
}

大致代码过程就是,创建一个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Go语言中,我们可以使用xlsx库来操作Excel文件,并使用CellStyle类型来设置单元格的样式。如果你想自动美化单元格,可以在读取Excel文件之后,遍历所有单元格,根据单元格的值和类型,设置不同的样式。 以下是一个示例代码,会读取example.xlsx文件中的所有单元格,如果单元格的值是数字类型,会将其设置为居中对齐、带边框的样式;如果单元格的值是字符串类型,会将其设置为居中对齐、加粗字体、背景色为灰色的样式。你可以根据需要修改样式设置。 ```go import ( "github.com/tealeg/xlsx" ) func main() { file, err := xlsx.OpenFile("example.xlsx") if err != nil { panic(err) } for _, sheet := range file.Sheets { for _, row := range sheet.Rows { for _, cell := range row.Cells { if cell.Type() == xlsx.CellTypeNumeric { style := xlsx.NewStyle() style.Alignment = *xlsx.NewAlignment("center", "center", false, false) style.Border = *xlsx.NewBorder("thin", "thin", "thin", "thin") cell.SetStyle(style) } else if cell.Type() == xlsx.CellTypeString { style := xlsx.NewStyle() style.Alignment = *xlsx.NewAlignment("center", "center", false, false) style.Font = *xlsx.NewFont(12, "Verdana Bold") style.Fill = *xlsx.NewFill("solid", "808080", "808080") cell.SetStyle(style) } } } } err = file.Save("example_formatted.xlsx") if err != nil { panic(err) } } ``` 运行以上代码会生成一个名为example_formatted.xlsx的Excel文件,其中所有数字类型的单元格都会被设置为居中对齐、带边框的样式;所有字符串类型的单元格都会被设置为居中对齐、加粗字体、背景色为灰色的样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值