Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File shows corrupted after save file #1799

Closed
jason10880 opened this issue Jan 29, 2024 · 10 comments
Closed

File shows corrupted after save file #1799

jason10880 opened this issue Jan 29, 2024 · 10 comments

Comments

@jason10880
Copy link

Description
I tried to save cell value/picture to .xlsm file, but I found the file would show corrupted after saving file.

OS: Windows 10
Go Version: 1.18.10

image

Code:

f, err := excelize.OpenFile(fileName)
if err != nil {
	fmt.Println(err)
	return
}
defer f.Close()

if err := f.SetCellValue(sheetName, cell, val); err != nil {
	fmt.Println(err)
	return
}

if err := f.Save(); err != nil {
	fmt.Println(err)
}
@xuri
Copy link
Member

xuri commented Jan 29, 2024

Thanks for your issue. Which version of Excelize are you using? I have fixed some style-related issues in v2.8.0, please try to upgrade the master branch code, and if it still doesn't work, please provide a reproducible attachment and code.

@xuri xuri added the needs more info This issue can't reproduce, need more info label Jan 29, 2024
@jhonriv
Copy link

jhonriv commented Jan 29, 2024

Hello, I have the same error too:

The server is Linux,
go v1.20,
excelize/v2 v2.8.0

Note: I use the function f.AddTable() (Maybe this is the cause)

In my case, I can generate the file, and I can open it with WPS 2019, here the file is normal.

However, when download the file from windows, show the next error in Excel (version Microsoft Office Profesional Plus 2019 - Windows 11 as reference):

image

image

error148240_01.xml

Se han detectado errores en el archivo "C:\Users\JhonRiv\Desktop\2024-01-26-OpenInvoices (3).xlsx"Característica quitada: Filtro automático de /xl/tables/table2.xml parte (Tabla)Característica quitada: Tabla de /xl/tables/table2.xml parte (Tabla)Característica quitada: Filtro automático de /xl/tables/table4.xml parte (Tabla)Característica quitada: Tabla de /xl/tables/table4.xml parte (Tabla)Característica quitada: Filtro automático de /xl/tables/table6.xml parte (Tabla)Característica quitada: Tabla de /xl/tables/table6.xml parte (Tabla)Característica quitada: Filtro automático de /xl/tables/table8.xml parte (Tabla)Característica quitada: Tabla de /xl/tables/table8.xml parte (Tabla)Característica quitada: Filtro automático de /xl/tables/table10.xml parte (Tabla)Característica quitada: Tabla de /xl/tables/table10.xml parte (Tabla)Característica quitada: Filtro automático de /xl/tables/table12.xml parte (Tabla)Característica quitada: Tabla de /xl/tables/table12.xml parte (Tabla)

Note: the file open in excel, but after that show the error.

@jason10880
Copy link
Author

@xuri, I am using github.com/xuri/excelize/v2 v2.8.0.

The attachment is the sample, and I had tried that it would be also corrupted.
Code:

f, err := excelize.OpenFile("sample.xlsm")
if err != nil {
	fmt.Println(err)
	return
}
defer f.Close()

if err := f.SetCellValue("test", "K34", "123"); err != nil {
	fmt.Println(err)
	return
}

if err := f.Save(); err != nil {
	fmt.Println(err)
}

sample.zip

@xuri
Copy link
Member

xuri commented Jan 30, 2024

Hi @jason10880, thanks for your feedback. As my reply above, I have fixed some style-related issues in v2.8.0, please try to upgrade the master branch code by command go get github.com/xuri/excelize/v2@master, and I have tested it with your attachment and it works well. These patches will be released in the next version.

@xuri
Copy link
Member

xuri commented Jan 30, 2024

Hi @jhonriv, thanks for your feedback. Please try to upgrade the master branch code by go get github.com/xuri/excelize/v2@master, and if it still doesn't work, please provide a reproducible attachment and code.

@eaglebush
Copy link

eaglebush commented Jan 30, 2024

Hi @xuri,

This is a wonderful library. I am currently experiencing corrupted export file on CentOS 8 Streaming like @jhonriv .
Linux 4.18.0-408.el8.x86_64 #1 SMP Mon Jul 18 17:42:52 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Distributor ID:	CentOSStream
Description:	CentOS Stream release 8
Release:	8
Codename:	n/a

go version go1.21.0 linux/amd64

Note: I have already updated excelize to the v2@master

It works fine on Windows 11. Here is a snippet of my code:

f := excelize.NewFile()
f.SetDocProps(
	&excelize.DocProperties{
		Creator: "Test",
		Created: time.Now().Format(time.RFC3339),
	})

// Column Header Style
style, err := f.NewStyle(
	&excelize.Style{
		Font: &excelize.Font{
			Bold: true,
		},
	})
if err != nil {
	return err
}

for _, c := range cells {
	if c.IsHeader {
		f.SetCellStyle("Sheet1", c.Axis, c.Axis, style)
	}
	f.SetCellValue("Sheet1", c.Axis, c.Value)
}

// Save to file to count the contents
if err = f.SaveAs(tempfile); err != nil {
	return err
}
b, err := os.ReadFile(tempfile)
if err != nil {
	return err
}
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
w.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(tempfile))
if _, err = w.Write(b); err != nil {
	return err
}

Thank you

@xuri
Copy link
Member

xuri commented Jan 30, 2024

Thanks for your feedback @eaglebush, if it does not work with Go 1.21.0, please reference the issues #1465, #1595, #1603, #1608, #1614, #1619, #1620, #1621, #1623, #1633, #1637, #1641, #1642, #1648, #1651, #1652, #1656, #1657, #1660, #1663, #1670, #1686, #1734, #1752, #1764 and #1768. There are some incompatible changes in the Go 1.21.0 encoding/xml library. I have given feedback to the Go team and created a patch for it (golang/go#61881), and it has been fixed on Go 1.21.1. Please using the Go 1.20.12 and previous Go released version or upgrade to Go 1.21.1 and later. I have added notice on the README and documentation website for this.

@eaglebush
Copy link

Hi @xuri ,

Thanks for the quick response. I think I will just update to 1.21.1+.

@jason10880
Copy link
Author

jason10880 commented Jan 30, 2024

@xuri It works for me after upgrade to the master branch v2.8.1.
Thanks for the help.

@xuri xuri removed the needs more info This issue can't reproduce, need more info label Jan 30, 2024
@xuri
Copy link
Member

xuri commented Jan 30, 2024

This issue has been closed. But there is still not enough info such as code and attachments to reproduce the comments by @jhonriv, so if you have any feedback, please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants