Skip to content

Excel corrupted after save() #413

@valasek

Description

@valasek

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

Activity

029vaibhav

029vaibhav commented on May 29, 2019

@029vaibhav

facing same problem

xuri

xuri commented on Jun 1, 2019

@xuri
Member

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

valasek commented on Jun 1, 2019

@valasek
Author

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!

ghost
xuri

xuri commented on Oct 22, 2019

@xuri
Member

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.

added a commit that references this issue on Oct 23, 2019
xuri

xuri commented on Oct 23, 2019

@xuri
Member

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

ghost
added 2 commits that reference this issue on Oct 23, 2020
e52949c
81f2ed5
added a commit that references this issue on Mar 14, 2024
d038ca2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmedThis issue can be reproduced

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @valasek@xuri@029vaibhav

        Issue actions

          Excel corrupted after save() · Issue #413 · qax-os/excelize