Skip to main content

Parsing Options解析选项

XLSX.read(data, read_opts) attempts to parse data.尝试解析data

XLSX.readFile(filename, read_opts) attempts to read filename and parse.尝试读取filename并进行解析。

The read functions accept an options argument:read函数接受options参数:

Option Name选项名称Default默认值Description描述
typeInput data encoding (see Input Type below)输入数据编码(参见下面的输入类型)
rawfalseIf true, plain text parsing will not parse values如果为true,纯文本解析将不会解析值 **
codepageIf specified, use code page when appropriate如果指定,请在适当时使用代码页 **
cellFormulatrueSave formulae to the .f field将公式保存到.f字段
cellHTMLtrueParse rich text and save HTML to the .h field解析富文本并将HTML保存到.h字段
cellNFfalseSave number format string to the .z field将数字格式字符串保存到.z字段
cellStylesfalseSave style/theme info to the .s field将样式/主题信息保存到.s字段
cellTexttrueGenerated formatted text to the .w field生成格式化文本到.w字段
cellDatesfalseStore dates as type d (default is n)将日期存储为类型d(默认值为n
dateNFIf specified, use the string for date code 14如果指定,则使用日期代码14的字符串 **
sheetStubsfalseCreate cell objects of type z for stub cells为存根单元格创建z类型的单元格对象
sheetRows0If >0, read the first sheetRows rows如果>0,则读取sheetRows第一行 **
bookDepsfalseIf true, parse calculation chains如果为true,则分析计算链
bookFilesfalseIf true, add raw files to book object如果为true,则将原始文件添加到book对象 **
bookPropsfalseIf true, only parse enough to get book metadata如果为true,则仅解析足够获取书籍元数据 **
bookSheetsfalseIf true, only parse enough to get the sheet names如果为true,则只进行足够的解析以获取工作表名称
bookVBAfalseIf true, copy VBA blob to vbaraw field如果为true,则将VBA blob复制到vbaraw字段 **
password""If defined and file is encrypted, use password如果已定义并且文件已加密,请使用密码 **
WTFfalseIf true, throw errors on unexpected file features如果为true,则在意外的文件功能上引发错误 **
sheetsIf specified, only parse specified sheets如果指定,则仅解析指定的工作表 **
PRNfalseIf true, allow parsing of PRN files如果为true,则允许分析PRN文件 **
xlfnfalseIf true, preserve _xlfn. prefixes in formulae如果为true,则公式中保留_xlfn.的前缀 **
FSDSV Field Separator overrideDSV字段分隔符覆盖
  • Even if cellNF is false, formatted text will be generated and saved to .w即使cellNFfalse,也会生成格式化文本并保存到.w
  • In some cases, sheets may be parsed even if bookSheets is false.在某些情况下,即使bookSheetsfalse,也可以解析书页。
  • Excel aggressively tries to interpret values from CSV and other plain text. Excel积极尝试从CSV和其他纯文本中解释值。This leads to surprising behavior! 这会导致令人惊讶的行为!The raw option suppresses value parsing.raw选项抑制值解析。
  • bookSheets and bookProps combine to give both sets of informationbookSheetsbookProps结合在一起提供了这两组信息
  • Deps will be an empty object if bookDeps is false如果bookDepsfalse,则Deps将为空对象
  • bookFiles behavior depends on file type:行为取决于文件类型:
    • keys array (paths in the ZIP) for ZIP-based formats基于ZIP格式的keys数组(ZIP中的路径)
    • files hash (mapping paths to objects representing the files) for ZIPZIP的files哈希(映射路径到表示文件的对象)
    • cfb object for formats using CFB containers使用CFB容器的格式的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对象输出时将生成sheetRows-1行(因为在解析数据时,标题行被计为一行)
  • By default all worksheets are parsed. 默认情况下,分析所有工作表。sheets restricts based on input type:基于输入类型的图纸sheets
    • number: zero-based index of worksheet to parse (0 is first worksheet)数字:要分析的工作表的从零开始的索引(0是第一个工作表)
    • string: name of worksheet to parse (case insensitive)字符串:要分析的工作表的名称(不区分大小写)
    • 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 xl/vbaProject.bin. XLSM和XLSB将VBA CFB对象存储在xl/vbaProject.bin中。BIFF8 XLS mixes the VBA entries alongside the core Workbook entry, so the library generates a new XLSB-compatible blob from the XLS CFB container.BIFF8 XLS将VBA条目与核心工作簿条目混合在一起,因此库从XLS CFB容器生成一个新的与XLSB兼容的blob。
  • codepage is applied to BIFF2 - BIFF5 files without CodePage records and to CSV files without BOM in type:"binary". 适用于没有CodePage记录的BIFF2-BIFF5文件和类型为type:"binary"的没有BOM的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. 目前只支持异或加密。Unsupported error will be thrown for files employing other encryption methods.对于使用其他加密方法的文件,将抛出不支持的错误。
  • Newer Excel functions are serialized with the _xlfn. prefix, hidden from the user. 较新的Excel函数用_xlfn.前缀序列化,对用户隐藏。SheetJS will strip _xlfn. normally. SheetJS将正常地剥离_xlfn.The xlfn option preserves them.xlfn选项保留了它们。
  • WTF is mainly for development. WTF主要用于开发。 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将强制抛出这些错误。

Input Type输入类型

Strings can be interpreted in multiple ways. 字符串可以用多种方式解释。The type parameter for read tells the library how to parse the data argument:readtype参数告诉库如何解析数据参数:

typeexpected input
"base64"string: Base64 encoding of the file字符串:文件的Base64编码
"binary"string: binary string (byte n is data.charCodeAt(n))字符串:二进制字符串(字节ndata.charCodeAt(n)
"string"string: JS string (characters interpreted as UTF8)字符串:JS string(解释为UTF8的字符)
"buffer"nodejs Buffer
"array"array: array of 8-bit unsigned int (byte n is data[n])数组:8位无符号整数的数组(字节ndata[n]
"file"string: path of file that will be read (nodejs only)字符串:将读取的文件路径(仅限Node.js)

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. 这将启用文件类型双关:使用.xls扩展名重命名文件将告诉您的计算机使用Excel打开文件,但Excel将知道如何处理它。This library applies similar logic:该库应用了类似的逻辑:

Byte 0Raw File TypeSpreadsheet Types电子表格类型
0xD0CFB ContainerBIFF 5/8 or protected XLSX/XLSB or WQ3/QPW or XLR
0x09BIFF StreamBIFF 2/3/4/5
0x3CXML/HTMLSpreadsheetML / Flat ODS / UOS1 / HTML / plain text
0x50ZIP ArchiveXLSB or XLSX/M or ODS or UOS2 or NUMBERS or text
0x49Plain TextSYLK or plain text
0x54Plain TextDIF or plain text
0xEFUTF8 EncodedSpreadsheetML / Flat ODS / UOS1 / HTML / plain text
0xFFUTF16 EncodedSpreadsheetML / Flat ODS / UOS1 / HTML / plain text
0x00Record StreamLotus WK* or Quattro Pro or plain text
0x7BPlain textRTF or plain text
0x0APlain textSpreadsheetML / Flat ODS / UOS1 / HTML / plain text
0x0DPlain textSpreadsheetML / Flat ODS / UOS1 / HTML / plain text
0x20Plain textSpreadsheetML / 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文件的工作

Plain text format guessing follows the priority order:纯文本格式猜测遵循优先级顺序:

FormatTest
XML<?xml appears in the first 1024 characters出现在前1024个字符中
HTMLstarts with < and HTML tags appear in the first 1024 characters<开头,HTML标记出现在前1024个字符中 *
XMLstarts with < and the first tag is valid<开头,第一个标记有效
RTFstarts with {\rt{\rt开头
DSVstarts with /sep=.$/, separator is the specified character/sep=.$/开头,分隔符是指定的字符
DSVmore unquoted `更多未引用`
DSVmore unquoted ; chars than \t or , in the first 1024前1024个字符中,;字符比\t,字符多
TSVmore unquoted \t chars than , chars in the first 1024前1024个字符中,\t字符比,字符多
CSVone of the first 1024 characters is a comma 前1024个字符之一是逗号","
ETHstarts with socialcalc:version:socialcalc:version:开头
PRNPRN option is set to true选项设置为true
CSV(fallback)
  • HTML tags include: html, table, head, meta, script, style, divHTML标记包括:htmltableheadmetascriptstylediv
Why are random text files valid?为什么随机文本文件有效? (click to show)

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! 将XLS扩展名添加到任何显示文本文件(其中唯一的字符是ANSI显示字符)会诱使Excel认为该文件可能是CSV或TSV文件,即使它只是一列!This library attempts to replicate that behavior.这个库试图复制这种行为。

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;