所以我在 js 和 xul 中有我的函数,它在 ods 或 xlsx 中创建导出文件。以 ods 导出工作正常,但问题是当我尝试以 excel 文件格式导出时。计划是创建将由 treeToXLSX.xsl 生成的 xml 文件 content.xml。生成了 Content.xml,当我提取 export.xlsx 时,它就在那里,但 xlsx 是空的。这些是我的文件
树.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 a c 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>
内容.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.xml
和template.xlsx
.