需要 Go 1.26.0 或更高版本;使用 Go Modules 时须导入带 /v2 的模块路径;插入图片需自行导入 image/gif、image/jpeg、image/png 解码包
excelize
qax-osBSD-3-Clause活跃维护用 Go 读写 Excel 电子表格的库,支持 XLSX、XLSM 等格式,能处理公式、图表和样式,适合在服务端批量生成或解析报表。
Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
go get github.com/xuri/excelize/v2README 未说明,纯 Go 库无特殊硬件要求
纯 Go 实现的 Excel 读写库,无需 Office 环境即可处理 XLSX 等表格文件
用 Go 读写 Excel 电子表格的库,支持 XLSX、XLSM 等格式,能处理公式、图表和样式,适合在服务端批量生成或解析报表。
核心能力
- 纯 Go 实现,读写 XLAM/XLSM/XLSX/XLTM/XLTX 文件
- 提供流式 API,处理海量数据工作表
- 支持基于工作表数据生成图表
- 支持插入图片并设置缩放、偏移、打印属性
- 兼容 Excel 2007 及之后版本生成的文件
容易踩的坑
- v2.11.1最新正式版2026-11-02 · 2026-11-02 02:41 UTC
We are pleased to announce the release of version 2.11.1. Featured are a handful of new areas of functionality and numerous bug fixes.
A summary of changes is available in the Release Notes. A full list of changes is available in the changelog.
Release Notes
The most notable changes in this release are:
Breaking Change
- Upgraded requirement: Go language version is Go 1.26.0 or later, due to upgrading dependency package
golang.org/x/crypto
Security
- Fixed a panic when getting a cell value with a negative shared string index (CVE-2026-104819), related security advisory GHSA-48hm-4h8j-58fg
- Fixed a
GetPivotTablespanic on invalid pivot table field indices (CVE-2026-107211), related security advisory GHSA-mx22-3794-2vpv - Fixed an unbounded row number in the rows iterator (CVE-2026-107212), related security advisory GHSA-jw42-f3rr-4cc3
- Fixed a
GetSlicerspanic when getting slicers from a worksheet without a drawing part (CVE-2026-107213), related security advisory GHSA-r7x5-4969-99p7 - Fixed a panic when decrypting a malformed workbook (CVE-2026-107214), related security advisory GHSA-2j4c-ffch-9f23
- Fixed unbounded
EncryptionInfoandEncryptedPackagestream sizes being allocated when decrypting a workbook (CVE-2026-107215), related security advisory GHSA-x2q3-8cjh-766f - Fixed a
CalcCellValuestack overflow caused by circular ANCHORARRAY formula references (CVE-2026-107216), related security advisory GHSA-wp2g-vpjj-g53r - Fixed
ColumnNameToNumberandJoinCellNameaccepting invalid column names, which caused integer overflow (CVE-2026-107217), related security advisory GHSA-c85p-xxjj-2r75 - Fixed Unicode handling and count overflow in the LEFT, LEFTB, RIGHT, and RIGHTB formula functions (CVE-2026-107218), related security advisory GHSA-8jjq-8j9w-m2v6
- Fixed an unbounded spin count when deriving the agile decryption key (CVE-2026-107219), related security advisory GHSA-jrfj-fhj2-jjvm
- Fixed a panic on a worksheet with an empty merged cell reference (CVE-2026-107220), related security advisory GHSA-g27h-8qhm-6pff
- Fixed a panic on a row whose cells are not in ascending column order (CVE-2026-107221), related security advisory GHSA-8mcq-6wmr-jrjv
- Fixed a
GetConditionalFormatspanic on malformed conditional formatting rules (CVE-2026-107222), related security advisory GHSA-rxcj-4pj5-74gr - Fixed an overflow of the minimum and maximum column numbers (CVE-2026-107223), related security advisory GHSA-fq3v-74gv-27gm
- Fixed a panic by rejecting Zip64 uncompressed sizes that do not fit in a signed int64 (CVE-2026-107224), related security advisory GHSA-fw94-4wwp-w8pw
- Fixed a panic when getting styles with a negative style index (CVE-2026-107225), related security advisory GHSA-5h23-36rv-pm65
Notable Features
- Added new
EmbeddedObjectdata type - Added 2 new constants,
MaxCFBStreamSizeV3andMaxCFBStreamSizeV4 - Added new function
GetShapesto support get shape, related issue #436 - Added new functions
GetEmbeddedObjectsandAddEmbeddedObjectsto support get and add embedded objects, related issues #158 and #2186 - The
CalcCellValuefunction now supports matrix arguments in the AND formula function - The
SetConditionalFormatfunction now validates the style index, related issue #2400
Compatibility Improvements
- Aligned the numeric precision of values stored by the
SetCellValuefunction with Microsoft Excel™, related issue #1757
Bug Fixes
- Fixed the
GetPicturesfunction not returning the edit positioning in some cases, resolved issue #2217 - Fixed the
- Upgraded requirement: Go language version is Go 1.26.0 or later, due to upgrading dependency package
- v2.11.02026-07-06 · 2026-07-06 16:12 UTC
查看更新说明
We are pleased to announce the release of version 2.11.0. Featured are a handful of new areas of functionality and numerous bug fixes.
A summary of changes is available in the Release Notes. A full list of changes is available in the changelog.
Release Notes
The most notable changes in this release are:
Breaking Change
- Upgraded requirement: Go language version is 1.25.0 or later, due to upgrading dependency package
golang.org/x/net - Breaking changes for the
AddChart,AddChartSheetandAddShapefunctions- Change
Titlefield data type ofChartfrom[]RichTextRuntoChartTitle - Change
Linefield data type ofShapefromShapeLinetoLineOptions - Renamed data type
ChartDashTypetoLineDashType - Renamed data type
ChartLineTypetoLineType - Renamed data type
ChartLinetoLineOptions
- Change
Security
- Check row number when reading worksheet to prevent unbounded memory allocation (CVE-2026-54063), related security advisories GHSA-h69g-9hx6-f3v4
- Prevent attacker-controlled allocations by validating against worksheet row number overflow maximum limitation (CVE-2026-59161), related security advisories GHSA-q5j5-6p94-4gwc
- Prevent panic on getting cell value with invalid shared string table index (CVE-2026-59162), related security advisories GHSA-fx5j-qcqg-grpf
Notable Features
- Added new
ChartTitledata type - Added new
PivotTableShowValuesAsdata type - Added new
PivotTableShowValuesAsTypeenumeration - Added 3 new exported error variables:
ErrPivotTableShowValuesAsBaseField,ErrPivotTableShowValuesAsBaseItemandErrUnsupportedPivotTableShowValuesAsType - Added new field
ShowValuesAsinPivotTableFielddata type - Added new field
SelectedItemsinPivotTableOptionsandSlicerOptionsdata types - Added new function
AutoFitColWidthto support auto fit columns width, related issue #92 - The
SetSheetNamefunction allow sheet rename for invalid source names, related issue #548 - The
CalcCellValuefunction supports tilde wildcard criteria - The
CalcCellValuefunction supports implicit intersection and fixes regex criteria anchoring - The
CalcCellValuefunction supports 3D references across sheet ranges, related issue #2303 - The
CalcCellValuefunction supports returning formula calculation result with raw cell value, related issue #2316 - The
AddPivotTablefunction supports checking whether the same data field appears in pivot table column, rows and filter fields, to prevent generating a corrupted workbook - The
AddPivotTableandGetPivotTablesfunctions support setting and getting show values as of pivot table data fields, related issue #2340 - The
AddPivotTable,AddSlicer,GetPivotTablesandGetSlicersfunctions support setting and getting selected items for pivot table and pivot table slicer, related issue #2154 - The
AddChartandAddChartSheetfunctions support setting chart title with formula - The
AddChartandAddChartSheetfunctions support setting chart title and chart axis title's layout - The
AddChartandAddChartSheetfunctions support setting line format for chart title and chart axis title - The
AddShapefunction supports setting line format of shapes - The
NewStylefunction supports auto foreground and background color when creating solid fill style when no custom color value is specified
Compatibility Improvements
- Fixed chart sheet not showing charts on Excel Online and KingSoft WPS™ Office, related issue #2273
Bug Fixes
- Fixed a v2.8.1 regression issue #1383
- Fixed
AddChartfunction panic when adding a chart with no fill series - Fixed
CalcCellValuefunction panic on invalid row reference in some cases, resolved issue #2266 - Fixed formula exact text matching not matching substrings when using `
- Upgraded requirement: Go language version is 1.25.0 or later, due to upgrading dependency package
- v2.10.12026-02-24 · 2026-02-24 23:49 UTC
查看更新说明
We are pleased to announce the release of version 2.10.1. Featured are a handful of new areas of functionality and numerous bug fixes.
A summary of changes is available in the Release Notes. A full list of changes is available in the changelog.
Release Notes
The most notable changes in this release are:
Breaking Change
Removed three exported error variables:
ErrStreamSetColStyle,ErrStreamSetColWidth, andErrStreamSetPanes.Notable Features
- Added the
ChartDataPointdata type - Added the
DataPointfield toChartSeries - Added the
DropLinesandHighLowLinesfields toChartAxis - Added the
Namefield toGraphicOptions - Added two constants:
MaxGraphicAltTextLengthandMaxGraphicNameLength - Added 7 exported error variables:
ErrFillType,ErrFillGradientColor,ErrFillGradientShading,ErrFillPatternColor,ErrFillPattern,ErrMaxGraphicAltTextLengthandErrMaxGraphicNameLength - Added the exported function
GetHyperLinkCellsto retrieve hyperlink cells, related issue #1607 - Added the exported function
GetSheetProtectionto retrieve sheet protection settings - The
AddCommentfunction now returns an error when adding a comment to a cell that already has one - Added support for inserting ICO images, related issue #2234
- The
CalcCellValuefunction now supports two formula functions: SORTBY and UNIQUE - The
AddChartandAddChartSheetfunctions now support setting data point colors for doughnut, pie, and 3D pie charts, related issue #1904 - The
AddChartfunction now supports configuring font families for East Asian and complex-script fonts - The
AddChartfunction now supports drop lines and high-low lines for area and line charts - The
GetPicturesfunction can now return partial formatting properties, related issue #2157 - Added the
SetColVisiblefunction to the streaming writer to set column visibility, related issue #2075 - Added the
SetColOutlineLevelfunction to the streaming writer to group columns, related issue #2212 - The
AddShapeandAddSlicerfunctions now support one-cell anchor positioning for shapes and slicers - The
GetSlicersfunction now supports retrieving slicers with one-cell anchor positioning - The
SetConditionalFormat,GetConditionalFormats, andUnsetConditionalFormatfunctions now support the 3 triangles, 3 stars, and 5 boxes icon set conditional formats, related issue #2038 - The
UnsetConditionalFormatfunction now supports deleting a conditional format rule or data validation for a specific cell within a cell range - The
AddPictureandAddPictureFromBytesfunctions now support setting the picture name - The
AddChartandAddShapefunctions now support setting names and alternative text for charts and shapes - The
AddSlicerfunction now supports setting alternative text for slicers - Added validation for graphic names and alternative text length; returns an error when the length exceeds the limit
- Added UTF-16-aware length checking and truncation
Improve the Compatibility
- Removed empty rows on save, reducing the generated workbook file size
Bug Fixes
- Fixed a v2.10.0 regression where the
GetCellValueandGetRowsfunctions returned shared string indexes for empty strings, resolve issue #2240 - Fixed
GetPivotTablespanicking when retrieving pivot tables in some cases - Fixed a panic when reading cell values with certain number format codes containing Chinese month names, resolve issue #2224
- Fixed a panic when opening encrypted workbooks in some cases, resolve issue #2237
- Fixed missing column styles when using the streaming writer
SetRowfunction - Fixed
GetPicturesnot returning some cell images - Fixed workbook corruption caused by light theme color index overflow
- Fixed
DeleteDataValidationupdating data valida
- Added the
- v2.10.02025-10-13 · 2025-10-13 16:13 UTC
查看更新说明
We are pleased to announce the release of version 2.10.0. Featured are a handful of new areas of functionality and numerous bug fixes.
A summary of changes is available in the Release Notes. A full list of changes is available in the changelog.
Release Notes
The most notable changes in this release are:
Breaking Change
- Upgrade requirements Go language version is 1.24.0 or later, for upgrade of dependency package
golang.org/x/crypto
Notable Features
- Add new exported error variable
ErrTransparency - Add new
ChartDashType,CustomPropertyandZipWriterdata types - Add new field
Borderto theChartMarkerdata type - Add new field
Fontto theChartLegenddata type - Add new field
Legendto theChartSeriesdata type - Add new field
Transparencyto theFilldata type - Add new fields
DashandFillto theChartLinedata type - Add new field
TmpDirto theOptionsdata type, support to specifies the custom temporary directory for creating temporary files, related issue #2024 - Add new field
Charsetto theFontdata type, support to explicitly specify font encodings when generating spreadsheets - Add new functions
GetCustomPropsandSetCustomPropssupport getting and setting workbook custom properties, related issue #2146 - Add new function
SetZipWriter, support set custom ZIP writer, related issue #2199 - Add optional parameter
withoutValuesfor theGetMergeCellsfunction - The
DeleteDataValidationfunction support delete data validation in extension list, and support delete data validation by given with multiple cell ranges with reference sequence slice or blank separated reference sequence string, related issue #2133 - The
AddChartfunction support set dash line and marker border type of charts - The
AddChartfunction support to set font for chart legends, related issue #2169 - The
AddChartandAddChartSheetfunction support create 4 kinds of box and whisker stock charts: High-Low-Close, Open-High-Low-Close, Volume-High-Low-Close and Volume-Open-High-Low-Close - The
CalcCellValuefunction support BAHTTEXT formula function - Skip fallback to default font size when create style if font size less than minimum size
- Support parse number format code with Hijri and Gregorian calendar
- Support set transparency for chart and shape, related issue #2176
- Support apply number format with the new 8 language: Corsican, Croatian, Croatian (Latin), Czech, Danish, Divehi, Dutch, Dzongkha language
Improve the Compatibility
- Remove all leading equal symbol when set cell formula, for improve compatibility with Apple Numbers, related issue #2145
- Using relative sheet target path in the internal workbook relationship parts
Bug Fixes
- Fix a v2.9.1 regression bug, build failed on ARMv7 architectures, resolve issue #2132
- Fix number format parser dropped empty literals in the end of the number format
- Fix panic on get string item with invalid offset range, resolve issues #2019 and #2150
- Fix panic on read unsupported pivot table cache sorce types, resolve issue #2161
- Fix incorrect characters verification, count characters as single runes in characters length limitation checking, resolve issue #2167
- Fix add pivot table caused workbook corrupted on Excel for Mac, resolve issue #2180
- Fix incorrect month name abbreviations when read cell with the Tibetan language number format code
- Fix special date number format result not consistent with Excel, resolve issue #2192
Performance
- Optimize the
GetSheetDimensionfunction by parse worksheet XML in stream mode, speedup about 95%, memory usage reduce about 96%
Miscellaneous
- The dependencies module has been updated
- Unit tests and godoc updated
- Documentation website with multilingual: Ar
- Upgrade requirements Go language version is 1.24.0 or later, for upgrade of dependency package
- v2.9.12025-05-12 · 2025-05-12 16:16 UTC
查看更新说明
We are pleased to announce the release of version 2.9.1. Featured are a handful of new areas of functionality and numerous bug fixes.
A summary of changes is available in the Release Notes. A full list of changes is available in the changelog.
Release Notes
The most notable changes in this release are:
Breaking Change
- Upgrade requirements Go language version is 1.23 or later, for upgrade of dependency package
golang.org/x/crypto - Change the data type of
DataValidationType,DataValidationErrorStyle,DataValidationOperator,PictureInsertTypefrominttobyte - SetCellInt function required
int64data type parameter, resolve issue #2068 - When adding drawing objects such as pictures, charts, shapes, and form controls, the offset setting will no longer affect the size of the drawing object, related issue #2001
Notable Features
- Add new fields
GapWidthandOverlapin theChartdata type - Add new fields
ShowDataTableandShowDataTableKeysfields in theChartPlotAreadata type - Add new field
Alignmentin theChartAxisdata type - Add new field
DataLabelin theChartSeriesdata type - Add new field
PageOrderforPageLayoutOptionsdata type - Add 2 new exported error variables:
ErrPageSetupAdjustToandErrStreamSetColStyle - Add 2 new exported enumerations:
HeaderFooterImagePositionTypeandIgnoredErrorsType - Add 2 new exported data types:
CalcPropsOptionsandHeaderFooterImageOptions - Add 2 new functions:
SetCalcPropsandGetCalcPropssupport setting and getting workbook calculation properties - Add new
CultureNameJaJP,CultureNameKoKRandCultureNameZhTWenumeration values, support apply number format for the Japanese calendar years, the Korean Danki calendar and the Republic of China year, related issue #1885 - Add new function
AddHeaderFooterImageto support set graphics in a header and footer, related issue #1395 - Add new function
AddIgnoredErrorssupport to ignored error for a range of cells, related issue #2046 - Add new function
SetColStylefor streaming writer to support set columns style, related issue #2075 - The
AddChartandAddChartSheetfunction support set chart axis text direction and rotation, related issue #2025 - The
AddChartandAddChartSheetfunction support set gap width and overlap for column and bar chart, related issue #2033 - The
AddChartandAddChartSheetfunction support set the format of the chart series data label, related issue #2052 - The
AddChartandAddChartSheetfunction support set data table for chart, related issue #2117 - The
AddFormControlfunction support set cell link for check box, related issue #2113 - The
SetPageLayoutfunction support set page order of page layout - The
DeletePicturefunction support delete one cell anchor image, related issue #2059 - An error will be return if the option value of the
SetPageLayoutfunction is invalid - Support adjust data validations cross multiple worksheets, related issue #2072
- Support apply number format with hash and zero place holder, related issue #2058
- Support apply number format with
?symbol - Support to insert one cell anchor drawing object when specified the positioning as "oneCell", related issue #2002
Bug Fixes
- Fix a v2.9.0 regression bug, corrupted workbook generated by open the workbook generated by stream writer, resolve issue #2015
- Fix redundant none type pattern fill generated, resolve issue #2014
- Fix missing vertical and horizontal border styles in some case, resolve issue #2048
- Fix conditional format's border styles missing in some case, resolve issue #2061
- Fix get pivot tables panic in some case, resolve issues #1954 and #2051
- Fix
GetStylefunction can not getVertAlignformat - Fix
CalcCellValuefunction subexpressions aren't
- Upgrade requirements Go language version is 1.23 or later, for upgrade of dependency package
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
Excelize
Introduction
Excelize is a library written in pure Go providing a set of functions that allow you to write to and read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and writing spreadsheet documents generated by Microsoft Excel™ 2007 and later. Supports complex components by high compatibility, and provided streaming API for generating or reading data from a worksheet with huge amounts of data. This library needs Go version 1.26.0 or later. The full docs can be seen using go's built-in documentation tool, or online at go.dev and docs reference.
Basic Usage
Installation
go get github.com/xuri/excelize
- If your packages are managed using Go Modules, please install with following command.
go get github.com/xuri/excelize/v2
Create spreadsheet
Here is a minimal example usage that will create spreadsheet file.
package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
// Create a new sheet.
index, err := f.NewSheet("Sheet2")
if err != nil {
fmt.Println(err)
return
}
// Set value of a cell.
f.SetCellValue("Sheet2", "A2", "Hello world.")
f.SetCellValue("Sheet1", "B2", 100)
// Set active sheet of the workbook.
f.SetActiveSheet(index)
// Save spreadsheet by the given path.
if err := f.SaveAs("Book1.xlsx"); err != nil {
fmt.Println(err)
}
}
Reading spreadsheet
The following constitutes the bare to read a spreadsheet document.
package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f, err := excelize.OpenFile("Book1.xlsx")
if err != nil {
fmt.Println(err)
return
}
defer func() {
// Close the spreadsheet.
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
// Get value from cell by given worksheet name and cell reference.
cell, err := f.GetCellValue("Sheet1", "B2")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(cell)
// Get all the rows in the Sheet1.
rows, err := f.GetRows("Sheet1")
if err != nil {
fmt.Println(err)
return
}
for _, row := range rows {
for _, colCell := range row {
fmt.Print(colCell, "\t")
}
fmt.Println()
}
}
Add chart to spreadsheet file
With Excelize chart generation and management is as easy as a few lines of code. You can build charts based on data in your worksheet or generate charts without any data in your worksheet at all.
package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
for idx, row := range [][]interface{}{
{nil, "Apple", "Orange", "Pear"}, {"Small", 2, 3, 3},
{"Normal", 5, 2, 4}, {"Large", 6, 7, 8},
} {
cell, err := excelize.CoordinatesToCellName(1, idx+1)
if err != nil {
fmt.Println(err)
return
}
f.SetSheetRow("Sheet1", cell, &row)
}
if err := f.AddChart("Sheet1", "E1", &excelize.Chart{
Type: excelize.Col3DClustered,
Series: []excelize.ChartSeries{
{
Name: "Sheet1!$A$2",
Categories: "Sheet1!$B$1:$D$1",
Values: "Sheet1!$B$2:$D$2",
},
{
Name: "Sheet1!$A$3",
Categories: "Sheet1!$B$1:$D$1",
Values: "Sheet1!$B$3:$D$3",
},
{
Name: "Sheet1!$A$4",
Categories: "Sheet1!$B$1:$D$1",
Values: "Sheet1!$B$4:$D$4",
}},
Title: excelize.ChartTitle{
Paragraph: []excelize.RichTextRun{
{
Text: "Fruit 3D Clustered Column Chart",
},
},
},
}); err != nil {
fmt.Println(err)
return
}
// Save spreadsheet by the given path.
if err := f.SaveAs("Book1.xlsx"); err != nil {
fmt.Println(err)
}
}
Add picture to spreadsheet file
package main
import (
"fmt"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/xuri/excelize/v2"
)
func main() {
f, err := excelize.OpenFile("Book1.xlsx")
if err != nil {
fmt.Println(err)
return
}
defer func() {
// Close the spreadsheet.
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
// Insert a picture.
if err := f.AddPicture("Sheet1", "A2", "image.png", nil); err != nil {
fmt.Println(err)
}
// Insert a picture to worksheet with scaling.
if err := f.AddPicture("Sheet1", "D2", "image.jpg",
&excelize.GraphicOptions{ScaleX: 0.5, ScaleY: 0.5}); err != nil {
fmt.Println(err)
}
// Insert a picture offset in the cell with printing support.
enable, disable := true, false
if err := f.AddPicture("Sheet1", "H2", "image.gif",
&excelize.GraphicOptions{
PrintObject: &enable,
LockAspectRatio: false,
OffsetX: 15,
OffsetY: 10,
Locked: &disable,
}); err != nil {
fmt.Println(err)
}
// Save the spreadsheet with the origin path.
if err = f.Save(); err != nil {
fmt.Println(err)
}
}
Contributing
Contributions are welcome! Open a pull request to fix a bug, or open an issue to discuss a new feature or change. XML is compliant with part 1 of the 5th edition of the ECMA-376 Standard for Office Open XML.
Licenses
This program is under the terms of the BSD 3-Clause License. See https://opensource.org/licenses/BSD-3-Clause.
The Excel logo is a trademark of Microsoft Corporation. This artwork is an adaptation.
The Go gopher was created by Renee French. Licensed under the Creative Commons 4.0 Attributions license.