🎨 Refactored XML generation logic and fixed content type override issues

This commit is contained in:
2025-04-16 17:29:40 +08:00
parent af9b26fa6c
commit 6b4be90900
8 changed files with 343 additions and 318 deletions

View File

@@ -40,7 +40,6 @@ func NewContentTypes() *ContentTypes {
ct.AddOverride("/xl/workbook.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")
ct.AddOverride("/xl/styles.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml")
ct.AddOverride("/xl/theme/theme1.xml", "application/vnd.openxmlformats-officedocument.theme+xml")
ct.AddOverride("/xl/worksheets/sheet1.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml")
ct.AddOverride("/xl/sharedStrings.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml")
return ct

View File

@@ -259,7 +259,7 @@ func main() {
})
// 保存Excel文件
err := wb.Save("sales_report.xlsx")
err := wb.Save("./workbook/examples/simple/sales_report.xlsx")
if err != nil {
fmt.Println("保存Excel文件时出错:", err)
return

View File

@@ -71,6 +71,11 @@ func (wb *Workbook) Save(filename string) error {
zipWriter := zip.NewWriter(file)
defer zipWriter.Close()
// 为每个工作表添加内容类型覆盖
for i := range wb.Worksheets {
wb.ContentTypes.AddWorksheetOverride(i + 1)
}
// 添加[Content_Types].xml
contentTypesWriter, err := zipWriter.Create("[Content_Types].xml")
if err != nil {