下载依赖包
go get -u github.com/xuri/excelize/v2
代码
package mainimport ("fmt""github.com/xuri/excelize/v2""time" )func main() {// 获取当前时间now := time.Now()filename := fmt.Sprintf("%s.xlsx", now.Format("2006-01-02_15-04-05"))// 创建新的 Excel 文件f := excelize.NewFile()// 设置单元格值f.SetCellValue("Sheet1", "A1", "Hello, Gopher!")// 保存文件,默认为当前路径下,亦可用绝对路径if err := f.SaveAs(filename); err != nil {fmt.Println(err)return}// 输出文件名fmt.Printf("Excel file '%s' created successfully.\n", filename) }
展示