所以我有我的功能在js和xul中创建ods或xlsx导出文件。导出ods工作得很好,但问题是当我尝试以excel文件格式导出时。计划是创建xml文件content.xml,它将由treeToXLSX.xsl生成。 Content.xml是生成的,当我提取export.xlsx它在那里,但xlsx是空的。这些都是我的文件导出到excel中xul
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 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>
的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.xml
和template.xlsx
。
你忘了指定这个'content.xml'文件有什么问题。 –
@WladimirPalant content.xml文件没有问题。当我尝试打开它作为xml文件从我的临时文件夹在Excel中打开没有任何问题。真正的问题是将内容从xml文件复制到xlsx文件。当我打开export.xlsx文件时,它总是空的。 – madeye
尝试用文本编辑器打开生成的文件,手动检查是否存在语法错误,缺少xml标签,标签没有正确关闭等类型问题。 – anishsane