创建Excel文件并使用ExcelJS对其进行写入

时间:2019-03-13 00:48:22

标签: javascript node.js excel async.js exceljs

我写了一个脚本,用ExcelJS创建一个新的Excel文件。添加3个标题,并插入2行。然后将该文件保存到磁盘。

在下一步中,应读取先前保存的文件,添加1行,然后以新名称再次保存。我找不到为什么我尝试添加第三行的第二部分永远不会发生的原因。这两个文件看起来相同,并且控制台中没有错误。

const Excel = require('exceljs');

async function exTest(){
  const workbook = new Excel.Workbook();
  const worksheet = workbook.addWorksheet("My Sheet");

worksheet.columns = [
  {header: 'Id', key: 'id', width: 10},
  {header: 'Name', key: 'name', width: 32}, 
  {header: 'D.O.B.', key: 'dob', width: 15,}
];

worksheet.addRow({id: 1, name: 'John Doe', dob: new Date(1970, 1, 1)});
worksheet.addRow({id: 2, name: 'Jane Doe', dob: new Date(1965, 1, 7)});

// save under export.xlsx
await workbook.xlsx.writeFile('export.xlsx');

// load a copy of export.xlsx
const newWorkbook = new Excel.Workbook();
await newWorkbook.xlsx.readFile('export.xlsx');

const newworksheet = newWorkbook.getWorksheet('My Sheet');
newworksheet.addRow(
  {id: 3, name: 'New Guy', dob: new Date(2000, 1, 1)}
);

await newWorkbook.xlsx.writeFile('export2.xlsx');

console.log("File is written");
};

exTest();

2 个答案:

答案 0 :(得分:3)

已连接工作链接

const Excel = require('exceljs');

// Create workbook & add worksheet
const workbook = new Excel.Workbook();
const worksheet = workbook.addWorksheet('ExampleSheet');

// add column headers
worksheet.columns = [
  { header: 'Package', key: 'package_name' },
  { header: 'Author', key: 'author_name' }
];

// Add row using key mapping to columns
worksheet.addRow(
  { package_name: "ABC", author_name: "Author 1" },
  { package_name: "XYZ", author_name: "Author 2" }
);

// Add rows as Array values
worksheet
  .addRow(["BCD", "Author Name 3"]);

// Add rows using both the above of rows
const rows = [
  ["FGH", "Author Name 4"],
  { package_name: "PQR", author_name: "Author 5" }
];


worksheet
  .addRows(rows);

// save workbook to disk
workbook
  .xlsx
  .writeFile('sample.xlsx')
  .then(() => {
    console.log("saved");
  })
  .catch((err) => {
    console.log("err", err);
  });

https://repl.it/@vishwasc/ExcelJs-Example#index.js

图片附件

// add image to workbook by filename
const imageId1 = workbook.addImage({
  filename: 'path/to/image.jpg',
  extension: 'jpeg',
});

// add image to workbook by buffer
const imageId2 = workbook.addImage({
  buffer: fs.readFileSync('path/to.image.png'),
  extension: 'png',
});

// add image to workbook by base64
const myBase64Image = "data:image/png;base64,iVBORw0KG...";
const imageId2 = workbook.addImage({
  base64: myBase64Image,
  extension: 'png',
});

请注意,在两种情况下,都必须指定扩展名。有效的扩展名包括'jpeg','png','gif'。

答案 1 :(得分:2)

通过添加数组来描述代码第二部分中的列来解决此问题。然后,成功添加了第三行。将具有列名称的对象传递给addRow()时,我必须提供对excel文件中已经存在的列的描述。

const Excel = require('exceljs');

async function exTest(){
  const workbook = new Excel.Workbook();
  const worksheet = workbook.addWorksheet("My Sheet");

worksheet.columns = [
 {header: 'Id', key: 'id', width: 10},
 {header: 'Name', key: 'name', width: 32}, 
 {header: 'D.O.B.', key: 'dob', width: 15,}
];

worksheet.addRow({id: 1, name: 'John Doe', dob: new Date(1970, 1, 1)});
worksheet.addRow({id: 2, name: 'Jane Doe', dob: new Date(1965, 1, 7)});

// save under export.xlsx
await workbook.xlsx.writeFile('export.xlsx');

//load a copy of export.xlsx
const newWorkbook = new Excel.Workbook();
await newWorkbook.xlsx.readFile('export.xlsx');

const newworksheet = newWorkbook.getWorksheet('My Sheet');
newworksheet.columns = [
 {header: 'Id', key: 'id', width: 10},
 {header: 'Name', key: 'name', width: 32}, 
 {header: 'D.O.B.', key: 'dob', width: 15,}
];
await newworksheet.addRow({id: 3, name: 'New Guy', dob: new Date(2000, 1, 1)});

await newWorkbook.xlsx.writeFile('export2.xlsx');

console.log("File is written");

};

exTest();