HTML
HTML is a common format for presenting data in the web. While the general read functions (HTML是在网络中显示数据的常见格式。虽然通用读取函数(XLSX.read
and XLSX.readFile
) can parse HTML strings and the write functions (XLSX.write
and XLSX.writeFile
) can generate HTML strings, the utility functions in this section can use DOM features.XLSX.read
和XLSX.readFile
)可以解析HTML字符串,而写入函数(XLSX.write
和XLSX.writeFile
)可以生成HTML字符串,但本节中的实用程序函数可以使用DOM功能。
SheetJS CE primarily focuses on data and number formatting.SheetJS CE主要关注数据和数字格式。
SheetJS Pro supports CSS text and cell styles in the HTML format and HTML table utilities.支持HTML格式和HTML表实用程序中的CSS文本和单元格样式。
HTML Table OutputHTML表格输出
Display worksheet data in a HTML table在HTML表中显示工作表数据
var html = XLSX.utils.sheet_to_html(ws, opts);
As an alternative to the 作为writeFile
HTML type, XLSX.utils.sheet_to_html
also produces HTML output. writeFile
HTML类型的替代方案,XLSX.utils.sheet_to_html
还生成HTML输出。The function takes an options argument:该函数采用一个选项参数:
id | id attribute for the TABLE elementTABLE 元素的id 属性 |
|
editable | false | contenteditable="true" for every TDtrue ,则为每个TD设置contenteditable="true" |
header | ||
footer |
Starting from the sample file 从样本文件pres.numbers
:pres.numbers
开始:
Implementation Details实施细节
The generated table will include special data attributes on each 生成的表格将包括每个TD
element:TD
元素的特殊数据属性:
data-t | |
data-v | |
data-z |
External cell links will be written as 外部单元格链接将被写为包装单元格内容的A
tags wrapping the cell contents.A
标记。
HTML Table InputHTML表格输入
Create New Sheet创建新工作表
Create a worksheet or workbook from a TABLE element从TABLE元素创建工作表或工作簿
var ws = XLSX.utils.table_to_sheet(elt, opts);
var wb = XLSX.utils.table_to_book(elt, opts);
XLSX.utils.table_to_sheet
takes a table DOM element and returns a worksheet resembling the input table. 获取一个表DOM元素并返回一个类似于输入表的工作表。Numbers are parsed. All other data will be stored as strings.对数字进行解析。所有其他数据将存储为字符串。
XLSX.utils.table_to_book
produces a minimal workbook based on the worksheet.基于工作表生成最小工作簿。
Both functions accept options arguments:两个函数都接受选项参数:
raw | true ,则每个单元格都将包含原始字符串 |
|
dateNF | FMT 14 | |
cellDates | false | d (default is n )d (默认为n ) |
sheetRows | 0 | sheetRows rows of the tablesheetRows 行 |
display | false | true ,则不会分析隐藏的行和单元格 |
UTC | false | true ,则日期解释为UTC ** |
UTC option is explained in "Dates"UTC选项在“日期”中有说明
Exporting a table to a spreadsheet file in the web browser involves 3 steps: "find the table", "generate a workbook object", and "export to file".在web浏览器中将表格导出到电子表格文件涉及3个步骤:“查找表格”、“生成工作簿对象”和“导出到文件”。
For example, if the HTML table has 例如,如果HTML表的id
attribute set to sheetjs
:id
属性设置为sheetjs
:
<table id="sheetjs">
<tr><th>Name</th><th>Index</th></tr>
<tr><td>Barack Obama</td><td>44</td></tr>
<tr><td>Donald Trump</td><td>45</td></tr>
<tr><td>Joseph Biden</td><td>46</td></tr>
</table>
document.getElementById("sheetjs")
is a live reference to the table.是对该表的实时引用。
/* find the table element in the page在页面中查找表元素 */
var tbl = document.getElementById('sheetjs');
/* create a workbook创建工作簿 */
var wb = XLSX.utils.table_to_book(tbl);
/* export to file导出到文件 */
XLSX.writeFile(wb, "SheetJSTable.xlsx");
Demo (click to hide)
This HTML table has id set to 此HTML表的id设置为sheetjs
:sheetjs
:
Name | Index |
---|---|
Barack Obama | 44 |
Donald Trump | 45 |
Joseph Biden | 46 |
Add to Sheet添加到工作表
Add data from a TABLE element to an existing worksheet将TABLE元素中的数据添加到现有工作表中
XLSX.utils.sheet_add_dom(ws, elt, opts);
XLSX.utils.sheet_add_dom
takes a table DOM element and updates an existing worksheet object. 获取一个表DOM元素并更新现有的工作表对象。It follows the same process as 它遵循与table_to_sheet
and accepts an options argument:table_to_sheet
相同的过程,并接受一个options
参数:
Default | ||
---|---|---|
raw | true ,则每个单元格都将包含原始字符串 |
|
dateNF | FMT 14 | |
cellDates | false | d (default is n )d (默认为n ) |
sheetRows | 0 | sheetRows rows of the tablesheetRows 行 |
display | false | true ,则不会分析隐藏的行和单元格 |
UTC | false | true ,则日期解释为UTC ** |
UTC option is explained in "Dates"UTC选项在“日期”中有说明
origin
is expected to be one of:预计是以下其中之一:
origin | |
---|---|
(cell object) | |
(string) | |
(number >= 0) | |
-1 | |
(default) | Start from cell A1 |
A common use case for sheet_add_dom
involves adding multiple tables to a single worksheet, usually with a few blank rows in between each table:sheet_add_dom
的一个常见用例涉及将多个表添加到一个工作表中,通常每个表之间有几行空白:
/* get "table1" and create worksheet */
const table1 = document.getElementById('table1');
const ws = XLSX.utils.table_to_sheet(table1);
/* get "table2" and append to the worksheet */
const table2 = document.getElementById('table2');
XLSX.utils.sheet_add_dom(ws, table2, {origin: -1});
Multi-table Export Example (click to show)
This demo creates a worksheet that should look like the screenshot below:这个演示创建了一个工作表,看起来应该像下面的屏幕截图:
The create_gap_rows
helper function expands the worksheet range, adding blank rows between the data tables.create_gap_rows
辅助函数扩展工作表范围,在数据表之间添加空行。
HTML Strings
Create a worksheet or workbook from HTML string从HTML字符串创建工作表或工作簿
table_to_book
/ table_to_sheet
/ sheet_add_dom
act on HTML DOM elements. Starting from an HTML string, there are two parsing approaches:对HTML DOM元素执行操作。从HTML字符串开始,有两种解析方法:
A) Table Phantasm: create a DIV whose 表Phantasm:创建一个DIV,其innerHTML
is set to the HTML string, generate worksheet using the DOM element, then remove the DIV:innerHTML
设置为HTML字符串,使用DOM元素生成工作表,然后删除DIV:
/* create element from the source */
var elt = document.createElement("div");
elt.innerHTML = html_source;
document.body.appendChild(elt);
/* generate worksheet */
var ws = XLSX.utils.table_to_sheet(elt.getElementsByTagName("TABLE")[0]);
/* remove element */
document.body.removeChild(elt);
Phantasm Demo (click to show)
The 演示中的html
variable in the demo is an editable HTML stringhtml
变量是一个可编辑的html字符串
B) Raw HTML: use 原始HTML:使用XLSX.read
to read the text in the same manner as CSV.XLSX.read
以与CSV相同的方式读取文本。
var wb = XLSX.read(html_source, { type: "string" });
var ws = wb.Sheets[wb.SheetNames[0]];
Raw HTML Demo (click to show)
The 演示中的html
variable in the demo is an editable HTML stringhtml
变量是一个可编辑的html字符串
Value Override值覆盖
When the 当指定了raw: true
option is specified, the parser will generate text cells. When the option is not specified or when it is set to false, the parser will try to interpret the text of each TD element.raw:true
选项时,解析器将生成文本单元格。当未指定该选项或将其设置为false
时,解析器将尝试解释每个TD元素的文本。
To override the conversion for a specific cell, the following data attributes can be added to the individual TD elements:要覆盖特定单元格的转换,可以将以下数据属性添加到各个TD元素:
data-t | |
data-v | |
data-z |
For example:例如
<!-- Parser interprets value as `new Date("2012-12-03")` default date formatParser将值解释为`new Date("2012-12-03")`默认日期格式 -->
<td>2012-12-03</td>
<!-- String cell "2012-12-03" -->
<td data-t="s">2012-12-03</td>
<!-- Numeric cell with the correct date code and General format具有正确日期代码和常规格式的数字单元格 -->
<td data-t="n" data-v="41246">2012-12-03</td>
<!-- Traditional Excel Date 2012-12-03 with style yyyy-mm-dd传统Excel日期2012-12-03,样式为yyyy-mm-dd -->
<td data-t="n" data-v="41246" data-z="yyyy-mm-dd">2012-12-03</td>
HTML Value Examples (click to hide)
Synthetic DOM合成DOM
table_to_book
/ table_to_sheet
/ sheet_add_dom
act on HTML DOM elements. Traditionally there is no DOM in server-side environments including NodeJS.对HTML DOM元素执行操作。传统上,在包括NodeJS在内的服务器端环境中没有DOM。
The simplest approach for server-side processing is to automate a headless web browser. "Browser Automation" covers some browsers.服务器端处理最简单的方法是自动化无头web浏览器。“浏览器自动化”涵盖了一些浏览器。
Some ecosystems provide DOM-like frameworks that are compatible with SheetJS. 一些生态系统提供了与SheetJS兼容的类似DOM的框架。Examples are included in the "Synthetic DOM" demo示例包含在“Synthetic DOM”演示中