Aspose.Cells FOSS for Go 是一个免费、开源的库,用于在 Go 应用程序中处理 Excel 电子表格文件。使用 go get github.com/aspose-cells-foss/Aspose.Cells-FOSS-for-Go/v26 安装它,便可开始创建工作簿、读取和写入单元格、应用样式、添加数据验证以及导出为 CSV,无需 Microsoft Excel 或任何 Office 依赖。
该库提供了一个小巧、符合 Go 习惯的 API,围绕 Workbook、Worksheet、Cells 和 Cell 构建。通过 Cells() 集合使用 Get/Set/Remove 访问单元格,将格式化组合成可复用的 Style 对象(由 Font、Fill、Alignment 和 Border 构成),并构建带有标题行和命名表格样式的结构化 Table 范围。专用的 StreamingReader 通过回调逐行读取 .xlsx 工作簿,从而能够在不将整张表加载到内存的情况下处理大文件。
go.mod 未声明任何外部依赖 —— 该库完全基于 Go 标准库构建。它采用 MIT 许可证,托管在 GitHub 上,并可通过单个 go get 命令安装;无需 cgo、无需本机 Office 库,也无需配置系统软件包。企业产品系列,请参阅 Aspose.Cells — Enterprise Product Family。
Cells().Get(ref) / Cells().Set(ref, value) 读取和写入值。Cell.SetFormula/GetFormula 将公式字符串存储在单元格中——在打开时由 Excel 或 LibreOffice 计算,而不是在保存时由库计算。Workbook.ExportToCSV/ImportFromCSV 和 Worksheet.ToCSV/FromCSV 在 XLSX 与 CSV 之间转换。StreamingReader.ProcessRows 通过回调逐行流式处理工作表,用于大文件处理而无需将整个工作表加载到内存中。Workbook.SetPassword/VerifyPassword 用于保护和检查工作簿密码。StreamingReader 对数千行工作簿进行流式处理,而不是将其完全加载到内存中。Font(name, size, bold, italic, color)、Fill(type, color)、Alignment(horizontal, vertical, wrap text)和 Border(top/bottom/left/right, color)组合成可复用的 Style,并使用 Cell.SetStyle 应用。Worksheet.AddDataValidation 附加列表和整数范围验证规则,每个规则都有自定义的错误标题、消息和错误样式。Worksheet.AddTable/GetTable 创建结构化的 Table 区域,可选标题行并使用命名的表格样式。Worksheet.AddPicture 嵌入图像 Picture,通过 SetAnchor(row, col) 定位。Aspose.Cells FOSS for Go 可通过单个 go get github.com/aspose-cells-foss/Aspose.Cells-FOSS-for-Go/v26 命令进行安装。 go.mod 没有 require 条目 —— 没有需要审查或供应的外部依赖。
API 的表面接口有意保持简洁:Workbook、Worksheet、Cells、Cell、Style以及少量辅助类型涵盖了电子表格的创建、样式、验证以及 CSV/流式 I/O。代码库采用 MIT 许可证并托管在 GitHub 上。
使用 go get 安装,创建一个 Workbook,访问第一个 Worksheet,并通过 Cells() 集合直接向单元格写入值,然后保存。
go get github.com/aspose-cells-foss/Aspose.Cells-FOSS-for-Go/v26func main() {
wb := cells_foss.NewWorkbook()
ws := wb.Worksheets[0]
ws.Cells().Set("A1", "Hello")
ws.Cells().Set("B1", "World")
ws.Cells().Set("A2", "Answer")
ws.Cells().Set("B2", 42)
outPath := "outputfiles/basic.xlsx"
if err := wb.Save(outPath); err != nil {
fmt.Printf("Error saving workbook: %v\n", err)
return
}
fmt.Printf("Workbook saved to %s\n", outPath)
}
为两个不同的列附加列表类型的验证(允许值的下拉列表)和整数范围验证,并为每个列设置各自的错误标题和消息。
func main() {
wb := cells_foss.NewWorkbook()
ws := wb.Worksheets[0]
ws.Cells().Set("A1", "Fruit")
ws.Cells().Set("A2", "Apple")
ws.Cells().Set("A3", "Banana")
dv := &cells_foss.DataValidation{
Type: cells_foss.DataValidationTypeList,
Formula1: `"Apple,Banana,Cherry,Dragonfruit"`,
AllowBlank: true,
ShowErrorMessage: true,
ErrorTitle: "Invalid Fruit",
ErrorMessage: "Please pick a fruit from the list.",
ErrorStyle: cells_foss.ErrorStyleStop,
}
if err := ws.AddDataValidation("A2:A10", dv); err != nil {
fmt.Printf("Error: %v\n", err)
return
}
dv2 := &cells_foss.DataValidation{
Type: cells_foss.DataValidationTypeWhole,
Formula1: "1",
Formula2: "100",
ShowErrorMessage: true,
ErrorTitle: "Invalid Value",
ErrorMessage: "Enter a whole number between 1 and 100.",
ErrorStyle: cells_foss.ErrorStyleWarning,
}
ws.Cells().Set("B1", "Score (1-100)")
ws.AddDataValidation("B2:B10", dv2)
outPath := "outputfiles/data_validation.xlsx"
if err := wb.Save(outPath); err != nil {
fmt.Printf("Error saving workbook: %v\n", err)
return
}
fmt.Printf("Workbook saved to %s\n", outPath)
}
StreamingReader 通过回调一次读取工作表的一行,因此即使是数千行的工作簿也不需要完全加载到内存中。
func main() {
genPath := "outputfiles/streaming_data.xlsx"
sr := cells_foss.NewStreamingReader(genPath)
rowCount := 0
var totalScore float64
err := sr.ProcessRows("Sheet1", func(rowIdx int, cells map[string]string) error {
rowCount++
if rowIdx == 1 {
fmt.Printf(" Header: %v\n", cells)
return nil
}
if score, ok := cells["C"+fmt.Sprint(rowIdx)]; ok {
var s float64
fmt.Sscanf(score, "%f", &s)
totalScore += s
}
return nil
})
if err != nil {
fmt.Fprintf(os.Stderr, "Error streaming: %v\n", err)
os.Exit(1)
}
fmt.Printf("Streamed %d rows (header + %d data rows)\n", rowCount, rowCount-1)
}
Aspose.Cells FOSS for Go 在 MIT 许可证下发布,允许商业和开源使用。
是的。只要保留原始版权声明,MIT 许可证允许商业使用且无需披露您自己的源代码。
使用 go get github.com/aspose-cells-foss/Aspose.Cells-FOSS-for-Go/v26 将其添加为 Go 模块依赖。需要 Go 1.24 或更高版本。
不。 go.mod 未声明任何 require 条目 — 该库完全基于 Go 标准库构建。
是的。 StreamingReader.ProcessRows 逐行读取工作表,通过回调,避免完整的内存加载。