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

xlsx produces corrupt file that cannot be opened from Excel but can be opened from Libreoffice. #609

Closed
Splizard opened this issue Aug 6, 2020 · 11 comments

Comments

@Splizard
Copy link

Splizard commented Aug 6, 2020

Hi, I am working on a program that copies data from three different source spreadsheets into a final spreadsheet.

Here is a sample of the kind of processing involved:

var Writer int
Sheet1 := File.Sheet["Sheet1"]
Data := Output.Sheet["Data"]

return Sheet1.ForEachRow(func(row *xlsx.Row) error {
	s := "some string"
	newRow := Data.AddRow()
	Writer++
	ri := Writer
	newRow.AddCell().SetString("Category") //A
	t, err := row.GetCell(A).GetTime(false)
	if err != nil {
		return fmt.Errorf(`could not process file: %w`, err)
	}
	newRow.AddCell().SetString(t.Month().String()[:3]) //B

	CopyCell(newRow.AddCell(), row.GetCell(C))                                        //C
	newRow.AddCell().SetFormula(fmt.Sprintf(`COUNTIF(C%v:C%v, C%v)`, ri-1, ri+1, ri)) //D
	CopyCell(newRow.AddCell(), row.GetCell(E))                                        //E
	newRow.AddCell().SetString(s)                                                     //F
	CopyCell(newRow.AddCell(), row.GetCell(G))                                        //G
	newRow.AddCell()                                                                  //H
	CopyCell(newRow.AddCell(), row.GetCell(I))                                        //I
	CopyCell(newRow.AddCell(), row.GetCell(J))                                        //J
	CopyCell(newRow.AddCell(), row.GetCell(K))                                        //K
	CopyCell(newRow.AddCell(), row.GetCell(L))                                        //L
	CopyCell(newRow.AddCell(), row.GetCell(M))                                        //M
	CopyCell(newRow.AddCell(), row.GetCell(N))                                        //N
}

CopyCell is defined as:

//CopyCell copies src cell to dst cell.
func CopyCell(dst, src *xlsx.Cell) {
	switch src.Type() {
	case xlsx.CellTypeString:
		dst.SetString(src.String())
	case xlsx.CellTypeStringFormula:
		dst.SetFormula(src.Formula())
	case xlsx.CellTypeNumeric:
		dst.SetNumeric(src.Value)
	case xlsx.CellTypeBool:
		dst.SetBool(src.Bool())
	case xlsx.CellTypeError:
		dst.Value = src.Value
	case xlsx.CellTypeDate:
		t, err := src.GetTime(false)
		if err != nil {
			log.Println(err)
		}
		dst.SetDate(t)
	}
	dst.NumFmt = src.NumFmt
	//dst.SetStyle(src.GetStyle())
	dst.RichText = src.RichText
}

However the output file is always corrupted and cannot be opened in Excel.
I imagine this is a bug, any idea why this is happening?

@tealeg
Copy link
Owner

tealeg commented Aug 6, 2020

@Splizard these things almost always occur because Excel is extremely picky about the XML it will or won't accept, whereas LibreOffice tends to say "Valid XML is Valid XML". I have theories about why that's the case, but they don't really matter - we consider these cases to be bugs.

@github-actions
Copy link

github-actions bot commented Oct 6, 2020

Stale issue message

@tealeg tealeg added this to the 3.2.4 milestone Nov 18, 2020
@github-actions
Copy link

Stale issue message

@iamzafar
Copy link

Hello! Will this change be included in the next release?

@tealeg
Copy link
Owner

tealeg commented Feb 11, 2021

@mzt14 it's in the 3.2.4 milestone. I honestly don't know when that release will be. For transparency, I don't use this library and I haven't done for a long, long time. Since it stopped being part of my day job in 2011, I've done a lot of work on it in my own time, as have a handful of other people. I have very little time to commit to this at the moment. Honestly, this project desperately needs some of its many users to become active contributors.

@tealeg
Copy link
Owner

tealeg commented Mar 5, 2021

This is believed fixed by #681. Can anyone confirm?

@Splizard
Copy link
Author

Splizard commented Mar 5, 2021

I'll see if I can check this, cheers @tealeg

@meandrewdev
Copy link

The problem still exists today

@tealeg
Copy link
Owner

tealeg commented Mar 15, 2021

@confirm2315 OK, thanks for the confirmation. On the master branch, right? Not the release.

@meandrewdev
Copy link

@tealeg Yes, on master. Excel shows blank lines starting with the last added one

@github-actions
Copy link

Stale issue message

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

No branches or pull requests

4 participants