Skip to main content

工作簿助手

许多实用程序函数返回工作表对象。工作表无法直接写入工作簿文件格式。必须将它们添加到工作簿对象中。

¥Many utility functions return worksheet objects. Worksheets cannot be written to workbook file formats directly. They must be added to a workbook object.

创建新工作簿

¥Create a new workbook

var wb_sans_sheets = XLSX.utils.book_new();

如果不带参数,book_new 实用程序函数将创建一个空工作簿。

¥With no arguments, the book_new utility function creates an empty workbook.

电子表格软件通常需要至少一个工作表,并在用户界面中强制执行该要求。例如,如果程序中删除了最后一个工作表,Apple Numbers 将自动创建一个新的空白工作表。

¥Spreadsheet software generally require at least one worksheet and enforce the requirement in the user interface. For example, if the last worksheet is deleted in the program, Apple Numbers will automatically create a new blank sheet.

SheetJS 编写函数 强制执行该要求。尝试导出空工作簿时,它们会抛出错误。

¥The SheetJS write functions enforce the requirement. They will throw errors when trying to export empty workbooks.

单一工作表

¥Single Worksheet

版本 0.20.1 引入了 book_new 的一参数形式和二参数形式。强烈推荐给 upgrade

¥Version 0.20.1 introduced the one and two argument forms of book_new. It is strongly recommended to upgrade.

var wb_with_sheet_named_Sheet1 = XLSX.utils.book_new(worksheet);
var wb_with_sheet_named_Blatte = XLSX.utils.book_new(worksheet, "Blatte");

book_new 可以接受一个或两个参数。

¥book_new can accept one or two arguments.

如果提供,第一个参数应为工作表对象。它将添加到新工作簿中。

¥If provided, the first argument is expected to be a worksheet object. It will be added to the new workbook.

如果提供,第二个参数是工作表的名称。如果省略,将使用默认名称 "Sheet1"。

¥If provided, the second argument is the name of the worksheet. If omitted, the default name "Sheet1" will be used.

将工作表附加到工作簿

¥Append a Worksheet to a Workbook

XLSX.utils.book_append_sheet(workbook, worksheet, sheet_name);

book_append_sheet 实用程序函数将工作表附加到工作簿。第三个参数指定所需的工作表名称。通过多次调用该函数可以将多个工作表添加到工作簿中。如果工作表名称已在工作簿中使用,则会抛出错误。

¥The book_append_sheet utility function appends a worksheet to the workbook. The third argument specifies the desired worksheet name. Multiple worksheets can be added to a workbook by calling the function multiple times. If the worksheet name is already used in the workbook, it will throw an error.

将工作表附加到工作簿并找到唯一的名称

¥Append a Worksheet to a Workbook and find a unique name

var new_name = XLSX.utils.book_append_sheet(workbook, worksheet, name, true);

如果第四个参数是 true,该函数将从指定的工作表名称开始。如果工作簿中存在工作表名称,则将通过查找名称主干并递增计数器来选择新的工作表名称。

¥If the fourth argument is true, the function will start with the specified worksheet name. If the sheet name exists in the workbook, a new worksheet name will be chosen by finding the name stem and incrementing the counter.

XLSX.utils.book_append_sheet(workbook, sheetA, "Sheet2", true); // Sheet2
XLSX.utils.book_append_sheet(workbook, sheetB, "Sheet2", true); // Sheet3
XLSX.utils.book_append_sheet(workbook, sheetC, "Sheet2", true); // Sheet4
XLSX.utils.book_append_sheet(workbook, sheetD, "Sheet2", true); // Sheet5