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

Handling of XLTX files? #191

Open
zypA13510 opened this issue Feb 13, 2019 · 2 comments
Open

Handling of XLTX files? #191

zypA13510 opened this issue Feb 13, 2019 · 2 comments

Comments

@zypA13510
Copy link
Contributor

zypA13510 commented Feb 13, 2019

I've spent the last week troubleshooting an issue that Microsoft Excel reports the generated XLSX as corrupted, while it can be opened just fine in other editors like LibreOffice. Turns out the XLTX file I'm using as a template has some special markers in the file that marks it as "template", and Microsoft Excel will refuse to open it unless its extension is XLTX.

So I'm asking if it is possible to add support for handling of the marker on save i.e. change the template marker to normal workbook if saving as *.xlsx? Judging from the git diff of uncompressed XML files, it shouldn't be too hard to fix. (But I'm not sure if you plan to support such feature, because, you know, it's named XLSX populate)

Steps to reproduce

  1. Open an Excel file or create a new Excel file, in Microsoft Excel, select File -> Save / Save As, select file type "Excel Template (*.xltx)" (let's name it example.xltx here)
  2. Run the following code in node.js, make sure example.xltx is in the current working directory.
const app = require('express')()
const path = require('path')
const excel = require('xlsx-populate')

app.get('/', async (req, res, next) => {
	let workbook = await excel.fromFileAsync(path.join(
		// ./example.xltx
		process.cwd(), 'example.xltx'
	))
	let sheet1 = workbook.sheet('Sheet1')
	// do some modification based on the template
	sheet1.find('{{Name}}', 'newName')
	let data = workbook.outputAsync()
	res.attachment('output.xlsx')
	res.send(await data)
}).listen(3000)
  1. Open http://localhost:3000/ in browser and save the downloaded xlsx file as output.xlsx
  2. Try opening output.xlsx in Microsoft Excel

Workaround

Just save the template as normal XLSX file

@dtjohnson
Copy link
Owner

There's likely some piece of data in the workbook XML that marks it as a template. If you can identify that we can add it easily enough.

@zypA13510
Copy link
Contributor Author

I have uploaded the diff to zypA13510/xlsx-xltx, and the complete content of both types as branches.

I believe the only important difference should be this one though:

diff --git [Content_Types].xml [Content_Types].xml
index 9db3057..96b0902 100644
--- [Content_Types].xml
+++ [Content_Types].xml
@@ -8,7 +8,7 @@
     Extension="xml"
     ContentType="application/xml"/><Override
     PartName="/xl/workbook.xml"
-    ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/><Override
+    ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml"/><Override
     PartName="/xl/worksheets/sheet1.xml"
     ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/><Override
     PartName="/xl/theme/theme1.xml"

So maybe we can try to check ContentType either on open or on save, and set it to the correct one?

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

2 participants