gpt4 book ai didi

javascript - 导出到 xul 中的 excel

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:19:58 24 4
gpt4 key购买 nike

所以我在 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.xmltemplate.xlsx

最佳答案

您的 TreeToXLSX.xsl 中缺少结束标记和 content.xml文件,但这可能是您创建问题时的复制粘贴错误。

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

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

关于javascript - 导出到 xul 中的 excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13029017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com