我正在尝试使用ExcelJS
创建多页Excel文件。
我正在遵循ExcelJS
github page中的官方文档。
第一步是创建工作簿。就我而言,我想要一个流,因为我将追加很多数据。
// Create Excel Workbook Stream
const workbookStream = new Excel.stream.xlsx.WorkbookWriter({
filename: path,
useStyles: true,
useSharedStrings: true,
});
然后,按照文档Worksheet Properties所述,将工作表添加到创建的工作簿的流中。
const sheet = workbookStream.addSheet('sheet1'); // Throw here
但是以这种方式,我遇到了以下错误:
'类型错误:workbookStream.addSheet不是函数
我还发现了一个不会抛出但无法正常工作并且不会创建很多工作表的代码。
const header = ['A', 'B', 'C'];
const sheet1 = Excel.addSheetOnWorkbook({
workbook: workbookStream,
name: 'sheet1',
});
const sheet2 = Excel.addSheetOnWorkbook({
workbook: workbookStream,
name: 'sheet2',
});
sheet.addRow(header).commit();
sheet.addRow(header).commit();
await workbookStream.commit();
在这种情况下,仅创建sheet1
(使用 LibreOffice 5.1.6.2 打开)。
用ExcelJS
解决此问题的任何方法吗?
答案 0 :(得分:2)
我不知道这对您有多大帮助,但是在我的环境中:
将worksheet.state
设置为visible
之后,我可以在LibreOffice
中看到它:
const Excel = require('exceljs');
const workbook = new Excel.Workbook();
const fs = require('fs');
const filename = "test.xlsx";
const sheetNames = ["A", "B", "C", "D"];
sheetNames.forEach(sheetName => {
let worksheet = workbook.addWorksheet(sheetName);
// I believe this needs to be set to show in LibreOffice.
worksheet.state = 'visible';
});
const stream = fs.createWriteStream(filename);
workbook.xlsx.write(stream)
.then(function() {
console.log(`File: ${filename} saved!`);
stream.end();
}).catch(error => {
console.err(`File: ${filename} save failed: `, error);
});
使用流式XLSX WorkbookWriter
:
const Excel = require('exceljs');
const sheetNames = ["A", "B", "C", "D"];
const workbook = new Excel.stream.xlsx.WorkbookWriter( { filename: './streamed-workbook.xlsx' } );
sheetNames.forEach(sheetName => {
let worksheet = workbook.addWorksheet(sheetName);
worksheet.state = 'visible';
worksheet.commit();
});
// Finished the workbook.
workbook.commit()
.then(function() {
console.log(`Worksheet committed!`);
});
我也将在Ubuntu计算机上进行测试。
XSLX文件只是包含多个.zip
文件的.xml
文件,因此您可以自己检查XML数据。
显示LibreOffice与exceljs生成的原始Xml(worksheet.xml):
LibreOffice
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<fileVersion appName="Calc"/>
<workbookPr backupFile="false" showObjects="all" date1904="false"/>
<workbookProtection/>
<bookViews>
<workbookView showHorizontalScroll="true" showVerticalScroll="true" showSheetTabs="true" xWindow="0" yWindow="0" windowWidth="16384" windowHeight="8192" tabRatio="500" firstSheet="0" activeTab="1"/>
</bookViews>
<sheets>
<sheet name="A" sheetId="1" state="visible" r:id="rId2"/>
<sheet name="B" sheetId="2" state="visible" r:id="rId3"/>
</sheets>
<calcPr iterateCount="100" refMode="A1" iterate="false" iterateDelta="0.001"/>
<extLst>
<ext xmlns:loext="http://schemas.libreoffice.org/" uri="{7626C862-2A13-11E5-B345-FEFF819CDC9F}">
<loext:extCalcPr stringRefSyntax="CalcA1"/>
</ext>
</extLst>
</workbook>
exceljs
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x15" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">
<fileVersion appName="xl" lastEdited="5" lowestEdited="5" rupBuild="9303"/>
<workbookPr defaultThemeVersion="164011" filterPrivacy="1"/>
<sheets>
<sheet sheetId="1" name="A" state="show" r:id="rId3"/>
<sheet sheetId="2" name="B" state="show" r:id="rId4"/>
<sheet sheetId="3" name="C" state="show" r:id="rId5"/>
<sheet sheetId="4" name="D" state="show" r:id="rId6"/>
</sheets>
<calcPr calcId="171027"/>
</workbook>
设置了某些非字母数字字符后,工作表标题可能会损坏 XML文件结构。
请注意表格标题!