导出到Excel中的Excel

所以我有我在js和xul的function是创buildods或xlsx导出文件。 导出在ods工作得很好,但问题是当我尝试以excel文件格式导出。 计划是创buildxml文件content.xml,它将由treeToXLSX.xsl生成。 Content.xml生成,当我提取export.xlsx它在那里,但xlsx是空的。 这些是我的文件

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> 

奇怪的是,出口到ods文件完美的作品,所以我真的卡住了。 我在我的目录中有所有需要的文件, treeToXLSX.xsl生成content.xmltemplate.xlsx

TreeToXLSX.xslcontent.xml文件中缺less结束标记,但在创build问题时可能会出现复制粘贴错误。

此外,在您的XSL文件中,尝试将<xsl:template match="*">更改为<xsl:template match="/Workbook">以匹配XMLinput的根元素。

最后,您在这里创build的文件看起来像Excel XML Spreadsheet格式,而不是Office Open XML格式。 XML电子表格文件应具有文件扩展名“.xml”或“.xls”,而不是“.xslx”。