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

Excel corrupted after save() #413

Closed
valasek opened this issue May 29, 2019 · 7 comments
Closed

Excel corrupted after save() #413

valasek opened this issue May 29, 2019 · 7 comments
Labels
confirmed This issue can be reproduced
Projects

Comments

@valasek
Copy link

valasek commented May 29, 2019

Thank you for your great work on excelize. Reading works like a charm. But I have an issue with writing.

When I update a Cell or Add a new Sheet, excel file is corrupted.

image

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><logFileName>error179160_01.xml</logFileName><summary>Errors were detected in file 'C:\Projects\Programming\go\src\github.com\valasek\excel\excel.xlsx'</summary><repairedParts><repairedPart>Repaired Part: /xl/workbook.xml part with XML error.  (Workbook) Load error. Line 2, column 0.</repairedPart></repairedParts></recoveryLog>

Used code:

// Package excel reads and checks internal timesheets
package main

import (
	"fmt"
	"os"
	"strconv"

	"github.com/360EntSecGroup-Skylar/excelize/v2"
)

func main() {

	// open file
	xlsx, err := excelize.OpenFile("./excel.xlsx")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// jump to last row
	lastRow := 1
	for {
		cell, _ := xlsx.GetCellValue("Timesheet", "A"+strconv.Itoa(lastRow))
		if cell != "" {
			lastRow = lastRow + 1
		} else {
			break
		}
	}

	// mock the data
	// data := [][]string{
	// 	[]string{"Stanislav Valasek", "5/17/2019", "Microsoft", "8", "Customer Improving dba name", "Standard"},
	// 	[]string{"Stanislav Valasek1", "5/17/2019", "Microsoft", "10", "Parsing legal name", "Standard"},
	// }

	xlsx.NewSheet("NewSheet1")
	// index := xlsx.GetSheetIndex("Timesheet")
	// xlsx.SetActiveSheet(index)

	// // write the data
	// for i, row := range data {
	// 	for j, value := range row {
	// 		err := xlsx.SetCellStr("Timesheet", string('A'+j)+strconv.Itoa(lastRow+i), value)
	// 		if err != nil {
	// 			fmt.Println("unable to write row to excel during update, error: ", err)
	// 			os.Exit(1)
	// 		}
	// 		fmt.Printf("written [%s, %s] = %s\n", string('A'+j), strconv.Itoa(lastRow+i), value)
	// 	}
	// }

	// save the changes
	err = xlsx.Save()
	if err != nil {
		fmt.Println("unable to save Internal timesheet, error: ", err)
		os.Exit(1)
	}

}

I shared a source excel directly with xuri.

go env

set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\stanislav.valasek\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\stanislav.valasek\go
set GOPROXY=
set GORACE=
set GOROOT=C:\SW\Go
set GOTMPDIR=
set GOTOOLDIR=C:\SW\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Projects\Programming\go\src\github.com\valasek\excel\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\stanislav.valasek\AppData\Local\Temp\go-build252113706=/tmp/go-build -gno-record-gcc-switches

@xuri xuri added the confirmed This issue can be reproduced label May 29, 2019
@029vaibhav
Copy link

facing same problem

@xuri xuri closed this as completed in d038ca2 Jun 1, 2019
@xuri
Copy link
Member

xuri commented Jun 1, 2019

Hi @valasek , thanks for your issue. I have fixed it. Please try to upgrade the library with the master branch code. @029vaibhav I'm not sure the problem you're having is caused by the same cause, please provides code and file attachment if you can.

@valasek
Copy link
Author

valasek commented Jun 1, 2019

Hi @xuri I confirm that create new Sheet and writing new cells using the code above is fixed now. Tested using v2 branch.

require github.com/360EntSecGroup-Skylar/excelize/v2 v2.0.1-0.20190601073747-d038ca2e9c75

Thenk you for fix and your time!

@xuri xuri added this to Bugfix in 2.0.1 Jun 7, 2019
@ghost
Copy link

ghost commented Oct 22, 2019

I am experiencing the same issue (I think). I have some very simple code that appears to create a corrupted Excel file:

package main

import (
	"github.com/360EntSecGroup-Skylar/excelize"
	"fmt"
)

func main() {
	name := "example"
	f := excelize.NewFile()
	sheetIndex := f.NewSheet(name) // new excel sheet
	f.SetActiveSheet(sheetIndex)

	// delete original sheet
	f.DeleteSheet(f.GetSheetName(f.GetSheetIndex("Sheet1")))
	
	// write some data to spreadsheet
	for i := 1; i < 10; i++ {
		for j := 1; j < 10; j++ {
			coord, err := excelize.CoordinatesToCellName(i,j)
			if err != nil {
				fmt.Println("Trouble getting coordinates:", err)
			}
			f.SetCellInt(name, coord, i + j)			
		}
		f.SaveAs("test.xlsx")
	}
}

Here are screenshots of what happens when I try to open the file with Microsoft Excel:
Screenshot2019-10-21-194931
Screenshot2019-10-21-195238

Here is the log file it gives me.

Is there something wrong with my code? Or anything I can do to help get this fixed? The output file seems to be correct in Excel but I don't want my clients to have to click through those error messages. Thanks in advance!!

@xuri xuri added the in progress Working in progress label Oct 22, 2019
@xuri
Copy link
Member

xuri commented Oct 22, 2019

Hi @wrycode, thanks for your issue. I will fix it recently. FYI: If you just want to change the default workbook name, you can use SetSheetName without deleting it.

xuri added a commit that referenced this issue Oct 23, 2019
@xuri
Copy link
Member

xuri commented Oct 23, 2019

Hi @wrycode, I have fixed it. Please try to upgrade the library with the master branch code.

@xuri xuri removed the in progress Working in progress label Oct 23, 2019
@ghost
Copy link

ghost commented Oct 23, 2019

@xuri thanks for both the tip and the fix! The issue is gone. Thank you very much!

nullfy pushed a commit to nullfy/excelize that referenced this issue Oct 23, 2020
nullfy pushed a commit to nullfy/excelize that referenced this issue Oct 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed This issue can be reproduced
Projects
No open projects
2.0.1
  
Bugfix
Development

No branches or pull requests

3 participants