1、xlsx的概念
xlsx是Microsoft Office EXCEL 2007/2010/2013/2016/2019文檔的擴展名。其基于Office Open XML標準的壓縮文件格式取代了其以前專有的默認文件格式,在串通的文件名后面添加了字母“x”(即".docx"取代".doc"、".xlsx"取代".xls"、".pptx"取代".ppt")。任何能夠打開".xlsx"文件的文字處理軟件都可以將該文檔轉(zhuǎn)換為".xls"文件,".xlsx"文件比".xls"文件所占用空間更小。
2、js-xlsx
js-xlsx將注意力集中到了數(shù)據(jù)轉(zhuǎn)換和導(dǎo)出上,所以它支持相當(dāng)多種類的數(shù)據(jù)解析和導(dǎo)出,不僅僅局限于支持xlsx格式。
支持導(dǎo)入的格式:
| type | expected input |
|---|---|
| "base64" | string:Base64 encoding of the file |
| "binary" | string:binary string(byte n is data.charCodeAt(n)) |
| "string" | string:JS string(characters interpreted as UTF8) |
| "buffer" | nodejs Buffer |
| "array" | array:array of 8-bit unsigned int(byte n is data[n]) |
| "file" | string:path of file that will be read(nodejs only) |
支持的導(dǎo)出格式
| bookType | file ext | container | sheets | Description |
|---|---|---|---|---|
| xlsx | .xlsx | ZIP | multi | Excel 2007+XML Format |
| xlsm | .xlsm | ZIP | multi | Excel 2007+Macro XML Format |
| xlsb | .xlsb | ZIP | multi | Excel 2007+Binary Format |
| biff8 | .xls | CFB | multi | Excel 97-2004 Workbook Format |
| biff5 | .xls | CFB | multi | Excel 5.0/95 Workbook Format |
| biff2 | .xls | none | single | Excel 2.0 Worksheet Format |
| xlml | .xls | none | multi | Excel 2003-2004(SpreadsheetML) |
| ods | .ods | ZIP | multi | OpenDocument Spreadsheet |
| fods | .fods | none | multi | Flat OpenDocument Spreadsheet |
| csv | .csv | none | single | Comma Separated Values |
| txt | .txt | none | single | UTF-16 Unicode Text(TXT) |
| sylk | .sylk | none | single | Symbolic Link(SYLK) |
| html | .html | none | single | HTML Document |
| dif | .dif | none | single | Data Interchange Format(DIF) |
| dbf | .dbf | none | single | dBASE II + VFP Extensions(DBF) |
| rtf | .rtf | none | single | Rich Text Format(RTF) |
| prn | .prn | none | single | Lotus Formatted Text |
| eth | .eth | none | single | Ethercalc Record Format(ETH) |
3、概念
js-xlsx提供了一個中間層用于操作數(shù)據(jù),他將不同類型的文件抽象成同一個js對象,從而規(guī)避了操作不同種類數(shù)據(jù)之間的復(fù)雜性。并且圍繞著這個對象提供了一系列的抽象功能。
4、對應(yīng)關(guān)系
| Excel名詞 | js-xlsx中的抽象類型 |
|---|---|
| 工作薄 | workBook |
| 工作表 | Sheets |
| Excel引用樣式(單元格地址) | cellAddress |
| 單元格 | Cell |
我們在使用Excel的過程中,獲取一個數(shù)據(jù)的流程如下:
(1)打開工作?。?)打開一個工作表(3)選中一片區(qū)域或者一個單元格
(4)針對數(shù)據(jù)進行操作(5)保存(另存為)
那么在js-xlsx中獲取一個單元格內(nèi)容的操作如下:
// 先不要關(guān)心我們的workbook對象是從哪里來的
var first_sheet_name = workbook.SheetNames[0]; // 獲取工作薄中的工作表名字
var address_of_cell='A1'; // 提供一個引用樣式(單元格下標)
var worksheet = workbook.Sheets[first_sheet_name]; // 獲取對應(yīng)的工作表對象
var desired_cell = worksheet[address_of_cell]; // 獲取對用的單元格對象
var desired_value = (desired_cell ? desired_cell.v: undefined); // 獲取對應(yīng)單元格中的數(shù)據(jù)