Export to Excel

So, I have my function in js and xul, which creates an export file in ods or xlsx. Exporting to ods works very well, but the problem is that I'm trying to export in excel format. It was planned to create an xml file content.xml, which will be generated by treeToXLSX.xsl. Content.xml is created, and when I extract export.xlsx, it is there, but xlsx is empty. These are my files.

TREE.JS

if(exportType == 'excel'){ xslFile = "treeToXLSX.xsl"; tempExportFile = "export.xlsx.tmp"; exportTemplate = "template.xlsx"; exportedFileName = "export.xlsx"; }else{ xslFile = "treeToODS.xsl"; tempExportFile = "export.ods.tmp"; exportTemplate = "template.ods"; exportedFileName = "export.ods"; } var newdoc = this.prep(xslFile); // save newdoc as /tmp/content.xml var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"] .createInstance(Components.interfaces.nsIFileOutputStream); var file = Components.classes["@mozilla.org/file/directory_service;1"] .getService(Components.interfaces.nsIProperties) .get("TmpD", Components.interfaces.nsIFile); file.append("content.xml"); var file2 = Components.classes["@mozilla.org/file/directory_service;1"] .getService(Components.interfaces.nsIProperties) .get("TmpD", Components.interfaces.nsIFile); file2.append(tempExportFile); file2.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666); file2.remove(false); var serializer = new XMLSerializer(); // use 0x02 | 0x10 to open file for appending. foStream.init(file, 0x02 | 0x08 | 0x20, -1, 0); // write, // create, // truncate // In ac file operation, we have no need to set file mode with or // operation, directly using "r" or "w" usually. serializer.serializeToStream(newdoc, foStream, "UTF-8"); foStream.close(); var template = Components.classes["@mozilla.org/file/directory_service;1"] .getService(Components.interfaces.nsIProperties) .get("AChrom", Components.interfaces.nsIFile); template.append("<path>"); template.append("<to_file>"); template.append(exportTemplate); // copy export.ods template file into temp, BLOCKING OPERATION template.copyTo(file2.parent, file2.leafName); var zipWriter = Components.Constructor("@mozilla.org/zipwriter;1", "nsIZipWriter"); var zipW = new zipWriter(); zipW.open(file2, 0x04); zipW.addEntryFile(file.leafName, Components.interfaces.nsIZipWriter.COMPRESSION_DEFAULT, file, false); zipW.close(); var homedir = Components.classes["@mozilla.org/file/directory_service;1"] .getService(Components.interfaces.nsIProperties) .get("Home", Components.interfaces.nsIFile); // open a save as dialog box var fp = Components.classes["@mozilla.org/filepicker;1"] .createInstance(Components.interfaces.nsIFilePicker); fp.init(window, "Export as", Components.interfaces.nsIFilePicker.modeSave); fp.appendFilters(Components.interfaces.nsIFilePicker.filterAll | Components.interfaces.nsIFilePicker.filterText); fp.displayDirectory = homedir; fp.defaultString = exportedFileName; var rv = fp.show(); if (rv == Components.interfaces.nsIFilePicker.returnOK || rv == Components.interfaces.nsIFilePicker.returnReplace) { file2.moveTo(fp.file.parent, fp.file.leafName); } else { file2.remove(false); } 

TREETOXLSX.XSL

 <?xml version='1.0'?> <?mso-application progid="Excel.Sheet"?> <xsl:stylesheet version="1.0" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"> <xsl:output method="xml" encoding="utf-8" indent="yes" /> <xsl:param name="title" /> <xsl:param name="skipfields" /> <xsl:param name="decimals" /> <xsl:param name="numerics" /> <xsl:template match="*"> <Workbook> <Worksheet ss:Name="Test"> <Table x:FullColumns="1" x:FullRows="1"> <Row ss:Height="12.1032"> <xsl:for-each select="child::*[1]/@*"> <xsl:if test="not(contains($skipfields, concat('|', name(), '|')))"> <Cell> <Data ss:Type="String"> <xsl:value-of select="translate(name(), '_', '')"/> </Data> </Cell> </xsl:if> </xsl:for-each> </Row> <xsl:for-each select="./*"> <Row> <xsl:for-each select="@*"> <xsl:if test="not(contains($skipfields, concat('|', name(), '|')))"> <xsl:choose> <xsl:when test="contains($decimals, concat('|', name(), '|'))"> <Cell> <Data ss:Type="Number"> <xsl:value-of select="." /> </Data> </Cell> </xsl:when> <xsl:when test="contains($numerics, concat('|', name(), '|'))"> <Cell> <Data ss:Type="Number"> <xsl:value-of select="." /> </Data> </Cell> </xsl:when> <xsl:otherwise> <Cell> <Data ss:Type="String"> <xsl:value-of select="." /> </Data> </Cell> </xsl:otherwise> </xsl:choose> </xsl:if> </xsl:for-each> </Row> </xsl:for-each> </Table> </Worksheet> 

content.xml

 <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"> <Worksheet xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:Name="Test"> <Table xmlns:x="urn:schemas-microsoft-com:office:excel" x:FullColumns="1" x:FullRows="1"> <Row ss:Height="12.1032"> <Cell> <Data ss:Type="String">ID</Data> </Cell> <Cell> <Data ss:Type="String">Name</Data> </Cell> <Cell> <Data ss:Type="String">Type</Data> </Cell> <Cell> <Data ss:Type="String">Group</Data> </Cell> <Cell> <Data ss:Type="String">Totaltickets</Data> </Cell> <Cell> <Data ss:Type="String">Wontickets</Data> </Cell> <Cell> <Data ss:Type="String">Moneyin</Data> </Cell> <Cell> <Data ss:Type="String">Moneyout</Data> </Cell> <Cell> <Data ss:Type="String">Percentage</Data> </Cell> <Cell> <Data ss:Type="String">Average</Data> </Cell> <Cell> <Data ss:Type="String">Moneyleft</Data> </Cell> </Row> <Row> <Cell> <Data ss:Type="Number">999</Data> </Cell> <Cell> <Data ss:Type="String">test</Data> </Cell> <Cell> <Data ss:Type="String"/> </Cell> <Cell> <Data ss:Type="String">a</Data> </Cell> <Cell> <Data ss:Type="Number">0</Data> </Cell> <Cell> <Data ss:Type="Number">0</Data> </Cell> <Cell> <Data ss:Type="Number">0,00</Data> </Cell> <Cell> <Data ss:Type="Number">0,00</Data> </Cell> <Cell> <Data ss:Type="Number">0</Data> </Cell> <Cell> <Data ss:Type="Number">0,00</Data> </Cell> <Cell> <Data ss:Type="Number">0,00</Data> </Cell> </Row> </Table> 

It is strange that exporting to an ods file works fine, so I'm really stuck. I have all the necessary files in my directory, treeToXLSX.xsl , which generates content.xml and template.xlsx .

+4
source share
1 answer

Your TreeToXLSX.xsl and content.xml files TreeToXLSX.xsl not have closing tags, but this may be a copy-paste error when creating the question.

Also, in your XSL file, try changing <xsl:template match="*"> to <xsl:template match="/Workbook"> to match the root XML input element.

Finally, the file you create looks like the Excel XML Spreadsheet format , not the Office Open XML format. XML table files must have the file extension ".xml" or ".xls" and not ".xslx".

+2
source

Source: https://habr.com/ru/post/1441529/


All Articles