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

Adding a number format seems to break Excel reading the XLSX file #114

Closed
fchorney opened this issue Sep 16, 2019 · 1 comment · Fixed by #116
Closed

Adding a number format seems to break Excel reading the XLSX file #114

fchorney opened this issue Sep 16, 2019 · 1 comment · Fixed by #116
Labels
bug Something isn't working

Comments

@fchorney
Copy link
Contributor

I've been trying to figure out how to add number formats to my xlsx files and I seem to be hitting a road block here.

using XLSX

sheet_name = "test_sheet"

xf = XLSX.open_empty_template(sheet_name)
sheet = xf[sheet_name]
wb = XLSX.get_workbook(xf)

# Create a font style
fontattr = XLSX.FontAttribute["b", "name" => ("val" => "Calibri"), "sz" => ("val" => "12")]
boldfont = XLSX.styles_add_font(wb, fontattr)
boldstyle = XLSX.styles_add_cell_xf(
    wb,
    Dict("applyFont" => "true", "fontId" => "$boldfont"),
)

# This works fine if you open it up in Excel
sheet["A1"] = XLSX.CellValue("This is Bold", boldstyle)

XLSX.writexlsx("./numfmt_issue_working.xlsx", xf)

# Create a number format style
datefmt = XLSX.styles_add_numFmt(wb, "YYYYMMDD")
datestyle = XLSX.styles_add_cell_xf(
    wb,
    Dict("applyNumberFormat" => "1", "numFmtId" => "$datefmt"),
)

# This results in an error when Excel tries to read the file
sheet["A2"] = XLSX.CellValue(42332, datestyle)

XLSX.writexlsx("./numfmt_issue_broken.xlsx", xf)

If you run that snippet it will generate two files. Once the numFmt gets added to the workbook it seems to break compatibility with Excel.

Using Excel for Mac v16.29

@felipenoris felipenoris added the bug Something isn't working label Sep 17, 2019
@fchorney
Copy link
Contributor Author

fchorney commented Sep 17, 2019

After a bunch of digging, it looks like the <numFmts><numFmts/> tag needs to appear in the xl/styles.xml file before it's referenced in the <cellXfs></cellXfs> block, or else Excel throws a fit about it. I'll write a fix and submit it in a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants