实用函数
本节中的效用函数分为两类:
¥The utility functions in this section fall into two categories:
数据打包
¥Data Packaging
"输入" 函数从数据行或其他常见 JS 数据表示创建 SheetJS 数据结构(工作表或工作簿)
¥The "Input" functions create SheetJS data structures (worksheets or workbooks) from rows of data or other common JS data representations
这些函数与 write
或 writeFile
配对以创建导出。例如,下图显示了将 HTML TABLE 导出到 XLSX 的步骤:
¥These functions are paired with write
or writeFile
to create exports. For
example, the following diagram shows the steps to export an HTML TABLE to XLSX:
数据提取
¥Data Extraction
"输出" 函数将工作表中的数据提取为更友好的结构。
¥The "Output" functions extract data from worksheets to friendlier structures.
这些函数与 read
或 readFile
配对来处理文件中的数据。下图显示了从 URL 生成 HTML TABLE 的步骤:
¥These functions are paired with read
or readFile
to process data from files.
The following diagram shows the steps to generate an HTML TABLE from a URL:
数组输出
¥Array Output
从工作表生成数据行
¥Generate rows of data from a worksheet
var arr = XLSX.utils.sheet_to_json(ws, opts);
¥This function is described in a dedicated page
数组的数组输入
¥Array of Arrays Input
从数组数组创建工作表
¥Create a worksheet from an array of arrays
var ws = XLSX.utils.aoa_to_sheet(aoa, opts);
将数组中的数据添加到现有工作表
¥Add data from an array of arrays to an existing worksheet
XLSX.utils.sheet_add_aoa(ws, aoa, opts);
¥These functions are described in a dedicated page
对象数组输入
¥Array of Objects Input
从对象数组创建工作表
¥Create a worksheet from an array of objects
var ws = XLSX.utils.json_to_sheet(aoo, opts);
将对象数组中的数据添加到现有工作表
¥Add data from an array of objects to an existing worksheet
XLSX.utils.sheet_add_json(ws, aoo, opts);
¥These functions are described in a dedicated page
HTML 表格输入
¥HTML Table Input
从 TABLE 元素创建工作表或工作簿
¥Create a worksheet or workbook from a TABLE element
var ws = XLSX.utils.table_to_sheet(elt, opts);
var wb = XLSX.utils.table_to_book(elt, opts);
将数据从 TABLE 元素添加到现有工作表
¥Add data from a TABLE element to an existing worksheet
XLSX.utils.sheet_add_dom(ws, elt, opts);
¥These functions are described in a dedicated page
HTML 输出
¥HTML Output
在 HTML 表格中显示工作表数据
¥Display worksheet data in a HTML table
var html = XLSX.utils.sheet_to_html(ws, opts);
¥This function are described in a dedicated page
分隔符分隔的输出
¥Delimiter-Separated Output
从工作表生成 CSV
¥Generate CSV from a Worksheet
var csv = XLSX.utils.sheet_to_csv(ws, opts);
以 "UTF-16 文本" 或制表符分隔值 (TSV) 导出工作表数据
¥Export worksheet data in "UTF-16 Text" or Tab-Separated Values (TSV)
var txt = XLSX.utils.sheet_to_txt(ws, opts);
¥These functions are described in a dedicated page
公式输出
¥Formulae Output
从工作表中提取所有公式
¥Extract all formulae from a worksheet
var fmla_arr = XLSX.utils.sheet_to_formulae(ws);