Reading Files读取文件
XLSX.read(data, options)
read
attempts to parse 尝试分析data
and return a workbook objectdata
并返回工作簿对象
The type of the options
object determines how data
is interpreted. For string data, the default interpretation is Base64.options
对象的type决定了如何解释data
。对于字符串数据,默认解释为Base64。
XLSX.readFile(filename, options)
readFile
attempts to read a local file with specified 尝试读取具有指定filename
.filename
的本地文件。
This method only works in specific environments. It does not work in browsers!此方法仅适用于特定环境。它在浏览器中不起作用!
The NodeJS installation note includes additional instructions for non-standard use cases.NodeJS安装说明包括非标准用例的附加说明。
Parsing Options分析选项
The read functions accept an options argument:read
函数接受一个options
参数:
type | ||
raw | false | true ,则纯文本解析将不解析值 ** |
dense | false | true ,请使用密集工作表表示 ** |
codepage | ||
cellFormula | true | .f 字段 |
cellHTML | true | .h field.h 字段 |
cellNF | false | .z field.z 字段 |
cellStyles | false | .s field.s 字段 |
cellText | true | .w field.w 字段生成格式化文本 |
cellDates | false | d (default is n )d (默认为n ) |
dateNF | ||
sheetStubs | false | z for stub cellsz 类型的单元格对象 |
sheetRows | 0 | sheetRows rowssheetRows 行 ** |
bookDeps | false | true ,则解析计算链 |
bookFiles | false | true ,则将原始文件添加到book对象 ** |
bookProps | false | true ,则只进行足够的解析以获取书籍元数据 ** |
bookSheets | false | true ,则仅进行足够的解析以获取图纸名称 |
bookVBA | false | vbaraw fieldtrue ,则将VBA blob复制到vbaraw 字段 ** |
password | "" | |
WTF | false | true ,则在意外的文件功能上引发错误 ** |
sheets | ||
PRN | false | true ,则允许解析PRN文件 ** |
xlfn | false | _xlfn. prefixes in formulaetrue ,则保留公式中的_xlfn. 前缀 ** |
FS | ||
UTC | true | false ,则以本地时间解析文本日期 |
Even if即使cellNF
is false, formatted text will be generated and saved to.w
cellNF
为false
,也会生成格式化文本并将其保存到.w
In some cases, sheets may be parsed even if在某些情况下,即使bookSheets
is false.bookSheets
为false
,也可能解析图纸。Excel aggressively tries to interpret values from CSV and other plain text. This leads to surprising behavior!Excel积极尝试解释CSV和其他纯文本中的值。这导致了令人惊讶的行为!Theraw
option suppresses value parsing.raw
选项禁止进行值解析。bookSheets
andbookProps
combine to give both sets of informationbookSheets
和bookProps
结合起来提供两组信息如果Deps
will be an empty object ifbookDeps
is falsebookDeps
为false
,Deps
将为空对象bookFiles
behavior depends on file type:行为取决于文件类型:基于ZIP格式的keys
array (paths in the ZIP) for ZIP-based formatskeys
数组(ZIP中的路径)files
hash (mapping paths to objects representing the files) for ZIPZIP的hash(将路径映射到表示文件的对象)cfb
object for formats using CFB containers使用CFB容器的格式的对象
sheetRows-1
rows will be generated when looking at the JSON object output (since the header row is counted as a row when parsing the data)在查看JSON对象输出时将生成行(因为解析数据时头行被算作一行)By default all worksheets are parsed.默认情况下,将解析所有工作表。sheets
restricts based on input type:根据输入类型进行限制:number: zero-based index of worksheet to parse (number:要分析的工作表的从零开始的索引(0
is first worksheet)0
是第一个工作表)string: name of worksheet to parse (case insensitive)string:要分析的工作表的名称(不区分大小写)array of numbers and strings to select multiple worksheets.数字和字符串的数组,以选择多个工作表。
bookVBA
merely exposes the raw VBA CFB object.仅公开原始VBA CFB对象。It does not parse the data. XLSM and XLSB store the VBA CFB object in它不解析数据。XLSM和XLSB将VBA CFB对象存储在xl/vbaProject.bin
.xl/vbaProject.bin
中。BIFF8 XLS mixes the VBA entries alongside the core Workbook entry, so the library generates a new blob from the XLS CFB container that works in XLSM and XLSB files.BIFF8XLS将VBA条目与核心工作簿条目混合在一起,因此库从XLSCFB容器生成一个新的blob,该容器可在XLSM和XLSB文件中工作。codepage
is applied to BIFF2 - BIFF5 files without应用于没有CodePage
records and to CSV files without BOM intype:"binary"
.CodePage
记录的BIFF2-BIFF5文件和没有BOM的type:"binary"
的CSV文件。BIFF8 XLS always defaults to 1200.BIFF8 XLS始终默认为1200。PRN
affects parsing of text files without a common delimiter character.影响对没有公共分隔符的文本文件的解析。Currently only XOR encryption is supported.目前仅支持XOR加密。Unsupported error will be thrown for files employing other encryption methods.对于使用其他加密方法的文件,将引发不支持的错误。Newer Excel functions are serialized with the较新的Excel函数使用_xlfn.
prefix, hidden from the user._xlfn.
前缀进行序列化,对用户隐藏。SheetJS will stripSheetJS将正常地剥离_xlfn.
normally. Thexlfn
option preserves them._xlfn.
。xlfn
选项将保留它们。WTF
is mainly for development.主要用于发展。By default, the parser will suppress read errors on single worksheets, allowing you to read from the worksheets that do parse properly.默认情况下,解析器将抑制单个工作表上的读取错误,允许您从正确解析的工作表中读取。Setting设置WTF:true
forces those errors to be thrown.WTF:true
将强制抛出这些错误。By default, "sparse" mode worksheets are generated. Individual cells are accessed by indexing the worksheet object with an A1-Style address. "dense" worksheets store cells in an array of arrays at默认情况下,会生成“稀疏”模式的工作表。通过使用A1样式地址对工作表对象进行索引,可以访问各个单元格。“密集”工作表将单元格存储在sheet["!data"]
.sheet["!data"]
的数组中。UTC
applies to CSV, Text and HTML formats.适用于CSV、文本和HTML格式。When explicitly set to当显式设置为false
, the parsers will assume the files are specified in local time.false
时,解析器将假定文件是在本地时间指定的。By default, as is the case for other file formats, dates and times are interpreted in UTC.默认情况下,与其他文件格式一样,日期和时间以UTC表示。
Input Type输入类型
Strings can be interpreted in multiple ways. 字符串可以用多种方式解释。The type
parameter for read
tells the library how to parse the data argument:read
的type
参数告诉库如何解析数据参数:
type | |
---|---|
"base64" | |
"binary" | n is data.charCodeAt(n) )n 为data.charCodeAt(n) ) |
"string" | |
"buffer" | nodejs Buffer |
"array" | n is data[n] )n 为data[n] ) |
"file" |
Some common types are automatically deduced from the data input type, including NodeJS 一些常见的类型是从数据输入类型自动推导出来的,包括NodeJS Buffer
objects, Uint8Array
and ArrayBuffer
objects, and arrays of numbers.Buffer
对象、Uint8Array
和ArrayBuffer
对象以及数字数组。
When a JS 当传递的JSstring
is passed with no type
, the library assumes the data is a Base64 string. string
没有type
时,库会假定数据是Base64字符串。FileReader#readAsBinaryString
or ASCII data requires 或ASCII数据需要"binary"
type. "binary"
类型。DOM strings including 包括FileReader#readAsText
should use type "string"
.FileReader#readAsText
在内的DOM字符串应使用类型"string"
。
Guessing File Type猜测文件类型
Implementation Details实施细节 (click to show)
Excel and other spreadsheet tools read the first few bytes and apply other heuristics to determine a file type. Excel和其他电子表格工具读取前几个字节,并应用其他启发式方法来确定文件类型。This enables file type punning: renaming files with the 这允许文件类型双关:重命名扩展名为.xls
extension will tell your computer to use Excel to open the file but Excel will know how to handle it. This library applies similar logic:.xls
的文件会告诉你的计算机使用Excel打开文件,但Excel会知道如何处理它。这个库应用了类似的逻辑:
Byte 0 | Raw File Type | |
---|---|---|
0xD0 | CFB Container | |
0x09 | BIFF Stream | BIFF 2/3/4/5 |
0x3C | XML/HTML | |
0x50 | ZIP Archive | |
0x49 | Plain Text | SYLK or plain text |
0x54 | Plain Text | DIF or plain text |
0xEF | UTF-8 Text | SpreadsheetML / Flat ODS / UOS1 / HTML / plain text |
0xFF | UTF-16 Text | SpreadsheetML / Flat ODS / UOS1 / HTML / plain text |
0x00 | Record Stream | Lotus WK* or Quattro Pro or plain text |
0x7B | Plain text | RTF or plain text |
0x0A | Plain text | SpreadsheetML / Flat ODS / UOS1 / HTML / plain text |
0x0D | Plain text | SpreadsheetML / Flat ODS / UOS1 / HTML / plain text |
0x20 | Plain text | SpreadsheetML / Flat ODS / UOS1 / HTML / plain text |
DBF files are detected based on the first byte as well as the third and fourth bytes (corresponding to month and day of the file date)DBF文件是基于第一个字节以及第三个和第四个字节(对应于文件日期的月份和日期)来检测的
Works for Windows files are detected based on the 基于类型为BOF
record with type 0xFF
0xFF
的BOF
记录检测到适用于Windows的Works文件
Plain text format guessing follows the priority order:纯文本格式猜测遵循优先级顺序:
Format | Test |
---|---|
XML | <?xml |
HTML | < and HTML tags appear in the first 1024 characters< 开头,HTML标记出现在前1024个字符中 * |
XML | < and the first tag is valid< 开头,第一个标记有效 |
RTF | {\rt {\rt 开头 |
DSV | /sep=.$/ , separator is the specified character/sep=.$/ 开头,分隔符是指定的字符 |
DSV | |
DSV | ; chars than \t or , in the first 1024; 字符多于\t 或, |
TSV | \t chars than , chars in the first 1024\t 多于, |
CSV | "," "," |
ETH | socialcalc:version: socialcalc:version: 开始 |
PRN | PRN true |
CSV | (fallback) |
HTML tags include:HTML标签包括:html
,table
,head
,meta
,script
,style
,div
html
、table
、head
、meta
、script
、style
、div
Why are random text files valid?为什么随机文本文件有效? (click to hide)
Excel is extremely aggressive in reading files. Excel在读取文件方面极为激进。Adding an XLS extension to any display text file (where the only characters are ANSI display chars) tricks Excel into thinking that the file is potentially a CSV or TSV file, even if it is only one column! This library attempts to replicate that behavior.向任何显示文本文件添加XLS扩展名(其中唯一的字符是ANSI显示字符)会诱使Excel认为该文件可能是CSV或TSV文件,即使它只有一列!此库试图复制该行为。
The best approach is to validate the desired worksheet and ensure it has the expected number of rows or columns. Extracting the range is extremely simple:最好的方法是验证所需的工作表,并确保它具有预期的行数或列数。提取范围非常简单:
var range = XLSX.utils.decode_range(worksheet['!ref']);
var ncols = range.e.c - range.s.c + 1, nrows = range.e.r - range.s.r + 1;