-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
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.
<?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
Activity
029vaibhav commentedon May 29, 2019
facing same problem
xuri commentedon 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 commentedon 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 commentedon 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.Fix corrupted Excel file issue #413
xuri commentedon Oct 23, 2019
Hi @wrycode, I have fixed it. Please try to upgrade the library with the master branch code.
Fix qax-os#413, make pivot cache ID not omit empty
Fix corrupted Excel file issue qax-os#413
Fix qax-os#413, make pivot cache ID not omit empty