讀寫Excel
- Excel檔案的包結構
- Excel檔案的組織形式
- Content Types
- 主要內容(worksheet)
- 關聯(Relationships)
- 第三方庫
- SheetJS
- 作業原理介紹
- 1.github安裝方式
- 2.NPM的安裝方式
- 3.bower的安裝方式
- 如何使用
Office Open XML,也稱為OpenXML或OOXML,是用于辦公室檔案的基于XML的格式,包括文字處理檔案,電子表格,演示文稿以及圖表,圖表,形狀和其他圖形材料,
該部分取自知乎 1,并且根據實際需求做了一些改動,
Excel檔案的包結構
OOXML 2是基于XML的檔案格式標準,但是我們都知道,Excel檔案是以xlsx為后綴的單個檔案,
SpreadsheetML或.xlsx檔案是一個zip檔案(一個包(package)),其中包含許多“組件”(通常是UTF-8或UTF-16編碼)或XML檔案,這個包可能包含圖片等其他媒體檔案,這個結構根據OOXML標準ECMA-376第2部分中概述的開放包裝約定(Open Packaging Conventions)進行組織, 你可以通過簡單地解壓縮.xlsx檔案來查看檔案結構和組成SpreadsheetML檔案的檔案,
簡單來說,一個Excel檔案就是一個zip壓縮檔案,我們稱呼一個Excel檔案為一個"包"(package),包里面不同的XML檔案,我們通常稱呼為“組件”,

下面提供了一個Excel 的檔案供大家參考,解壓之后我們能夠看到類似下面的目錄結構,這就是我們Excel檔案的目錄構成了,
├── [Content_Types].xml (組件描述檔案)
├── _rels (包的關聯組件)
├── docProps (檔案的屬性)
│ ├── app.xml
│ ├── core.xml
│ └── custom.xml
└── xl
├── _rels (作業簿組件的關聯組件)
│ └── workbook.xml.rels
├── charts (圖表組件的目錄)
│ ├── _rels (圖表組件的關聯組件目錄)
│ │ ├── chart1.xml.rels (圖表組件的關聯組件)
│ │ └── chart2.xml.rels
│ ├── chart1.xml (表格組件)
│ ├── chart2.xml (表格組件)
│ ├── colors1.xml (顏色組件)
│ ├── colors2.xml (顏色組件)
│ ├── style1.xml (樣式組件)
│ └── style2.xml (樣式組件)
├── drawings (繪圖組件的目錄)
│ ├── _rels (繪圖組件的關聯組件目錄)
│ │ └── drawing1.xml.rels (繪圖組件的關聯組件)
│ └── drawing1.xml
├── media (多媒體檔案目錄)
│ └── image1.png
├── sharedStrings.xml (共享字串組件)
├── styles.xml (樣式組件)
├── tables (表格組件的目錄)
│ └── table1.xml (表格組件)
├── theme (主題組件的目錄)
│ └── theme1.xml (主題組件)
├── workbook.xml (作業簿組件)
└── worksheets (作業表組件的目錄)
├── _rels (作業表組件的關聯組件目錄)
│ └── sheet1.xml.rels (作業表組件的關聯組件)
└── sheet1.xml (作業表組件)
Excel檔案的組織形式
Excel檔案的包中包含了很多組件,最重要的可以分成:
- Content Types
- 主要內容(worksheet)
- 關聯(Relationships)
基本上理解了這三個東西,你就能明白了Excel包中的檔案,是怎么組織起來,最后成為我們最后能看到的復雜Excel樣式了,
Content Types
每個包都必須在其根目錄下有一個 [Content_Types] .xml 檔案,該檔案包含了包中組件的所有內容型別的串列,每個組件及其型別都必須在[Content_Types] .xml中列出, 以下是主要內容部分的內容型別,
在向包中添加新組件時,請一定記得這里,很重要,
下面是示例Excel檔案中的 [Content_Types] .xml ,ContentType描述了各個組件的檔案型別,PartName描述了組件在包內的名稱(對于部分組件來說,也是XML檔案的路徑):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="png" ContentType="image/png" />
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />
<Default Extension="xml" ContentType="application/xml" />
<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml" />
<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml" />
<Override PartName="/docProps/custom.xml" ContentType="application/vnd.openxmlformats-officedocument.custom-properties+xml" />
<Override PartName="/xl/charts/chart1.xml" ContentType="application/vnd.openxmlformats-officedocument.drawingml.chart+xml" />
<Override PartName="/xl/charts/chart2.xml" ContentType="application/vnd.openxmlformats-officedocument.drawingml.chart+xml" />
<Override PartName="/xl/charts/colors1.xml" ContentType="application/vnd.ms-office.chartcolorstyle+xml" />
<Override PartName="/xl/charts/colors2.xml" ContentType="application/vnd.ms-office.chartcolorstyle+xml" />
<Override PartName="/xl/charts/style1.xml" ContentType="application/vnd.ms-office.chartstyle+xml" />
<Override PartName="/xl/charts/style2.xml" ContentType="application/vnd.ms-office.chartstyle+xml" />
<Override PartName="/xl/drawings/drawing1.xml" ContentType="application/vnd.openxmlformats-officedocument.drawing+xml" />
<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml" />
<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml" />
<Override PartName="/xl/tables/table1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml" />
<Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml" />
<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" />
<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" />
</Types>
主要內容(worksheet)
SpreadsheetML檔案是一個包含許多不同組件(主要是XML檔案)的包, 但是,大多數實際的內容都在一個或多個作業表(worksheet)組件(每個作業表(worksheet)一個)和一個共享字串(sharedStrings)組件, 以Microsoft Excel為例,其內容位于xl檔案夾中,而作業表(worksheet)位于worksheet子檔案夾中,
作業簿(workbook)組件不包含實際內容,而僅包含電子表格的某些屬性,并參考了一個個包含資料的作業表(worksheet)組件, 一個作業表組件可以是一個表格(grid),圖表(chart)或者對話框表(dialog sheet),
作業表(worksheet)組件內容(xl/worksheets/sheet1.xml):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"
xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<sheetPr/>
<dimension ref="A1:F13"/> <!-- 表格的范圍 -->
<sheetViews>
<sheetView tabSelected="1" workbookViewId="0">
<selection activeCell="C14" sqref="C14"/> <!-- activeCell的位置 -->
</sheetView>
</sheetViews>
<!-- 表格的默認cell配置 -->
<sheetFormatPr defaultColWidth="9" defaultRowHeight="14" outlineLevelCol="5"/>
<cols> <!-- 列屬性 -->
<col min="1" max="1" width="4.5625" customWidth="1"/>
<col min="2" max="2" width="10.9296875" customWidth="1"/>
<col min="3" max="3" width="10.546875" customWidth="1"/>
<col min="4" max="4" width="11.71875" customWidth="1"/>
<col min="6" max="6" width="4.5546875" customWidth="1"/>
</cols>
<sheetData>
<row r="1" ht="40" customHeight="1" spans="1:6">
<!-- r:行號, ht:行高(單位pt), customHeight:是否自定義行高, span:跨度, s:樣式(style)索引, customFormat:是否行自定義格式 -->
<c r="A1" s="2" t="s">
<!-- r:單元格編號, s:樣式(style)索引, t:單元格型別 -->
<v>0</v><!-- 字串在共享字串組件中的索引 -->
</c>
<c r="B1" s="3"/>
<c r="C1" s="3"/>
<c r="D1" s="3"/>
<c r="E1" s="3"/>
<c r="F1" s="3"/>
</row>
<row r="2" s="1" customFormat="1" ht="22" customHeight="1" spans="1:6">
<c r="A2" s="4" t="s">
<v>1</v>
</c>
<c r="B2" s="4" t="s">
<v>2</v>
</c>
<c r="C2" s="4" t="s">
<v>3</v>
</c>
<c r="D2" s="4" t="s">
<v>4</v>
</c>
<c r="E2" s="9" t="s">
<v>5</v>
</c>
<c r="F2" s="9"/>
</row>
......
</sheetData>
<mergeCells count="9">
<mergeCell ref="A1:F1"/>
<mergeCell ref="E2:F2"/>
......
</mergeCells>
<pageMargins left="0.75" right="0.75" top="1" bottom="1" header="0.511805555555556" footer="0.511805555555556"/>
<!-- 頁面邊距配置,單位英寸(in) -->
<headerFooter/>
<drawing r:id="rId1"/>
<!-- 圖表和多媒體的顯示,都通過參考繪圖(drawing)組件來實作 -->
<tableParts count="1">
<tablePart r:id="rId2"/>
<!-- 表格(table)組件:用于查找特定的表定義組件 -->
</tableParts>
</worksheet>
因為太長,只截取部分共享字串(sharedStrings)組件內容(xl/sharedStrings.xml):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="36" uniqueCount="36">
<si>
<t>Ttitle</t>
</si>
<si>
<t>A1</t>
</si>
<si>
<t>B1</t>
</si>
......
</sst>
而這些作業表(worksheet)本身只存盤作業表自身行列的屬性和資料,其他與作業表相關的內容,它會通過參考其他型別的組件來實作,比如Excel圖表,多媒體檔案等等,這些要通過關聯(Relationships)組件,讀取對應的資料,接下來我們來說說關聯,
關聯(Relationships)
每個包都包含一個關聯(Relationships)組件,該關聯部件定義了其他組件之間以及與包外部資源之間的關聯,這樣可以將關系與內容分開,并且可以輕松更改關聯,而無需更改參考目標的來源,
OOXML使用這種組織的方式,可以將組件和組件之間解耦,而在實作讀寫邏輯時,我們可以從中獲取極大的便利,比如我們需要修改某個Excel組件時,可以不修改參考他的組件,只需要修改這個組件本身,而不是組件包含在整個XML檔案中,然后修改整個XML檔案,比如我們只需要讀取作業表資料時,可以不讀其他組件的XML資料,提高程式效率,
我們在好幾個包(package)下都能看到一個 _rels 目錄,這個目錄就是Excel檔案的關聯組件了,一般這個關聯組件的目錄下會有一個或多個 .rels 后綴的檔案,檔案基于XML格式描述資源之間的關系,組件之間使用關聯將Excel檔案組織起來,
以示例檔案舉例 xl/_rels 下的 workbook.xml.rels :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"
Target="sharedStrings.xml"/>
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
Target="styles.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"
Target="theme/theme1.xml"/>
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"
Target="worksheets/sheet1.xml"/>
</Relationships>
檔案里面,我們看到了關聯的資源ID(Id)和資源路徑(Target)的對應,以及關聯的型別(Type),
然后我們看看作業簿(xl/workbook.xml)的內容:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<fileVersion appName="xl" lastEdited="3" lowestEdited="5" rupBuild="9302"/>
<workbookPr/>
<bookViews>
<workbookView windowWidth="21500" windowHeight="13180"/>
</bookViews>
<sheets>
<sheet name="Sheet1" sheetId="1" r:id="rId1"/>
</sheets>
<calcPr calcId="144525" concurrentCalc="0"/>
</workbook>
作業簿(workbook)怎么通過作業簿的關聯組件(xl/_rel/workbook.xml.rels)來獲取需要的作業表(sheet)組件?看關聯組件的資料,我們發現,只需要找到關聯ID為rId1的sheet組件的路徑(Target),然后再讀取路徑下的檔案,作業簿就能獲取到對應作業表組件的資料,
我們截取作業表組件(xl/worksheets/sheet1.xml)的部分xml內容,來看作業表又是怎么通過關聯來進一步描述作業表的內容:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"
xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
......
<drawing r:id="rId1"/>
<!-- 圖表和多媒體的顯示,都通過參考繪圖(drawing)組件來實作 -->
<tableParts count="1">
<tablePart r:id="rId2"/>
<!-- 表格(table)組件:用于查找特定的表定義組件 -->
</tableParts>
</worksheet>
XML檔案中drawing和tablePart的 r:Id 屬性,告訴我們,這里作業表組件需要從關聯組件中尋找繪圖組件和表格組件,然后我們繼續看作業表組件的關聯組件 (xl/worksheet/_rel/sheet1.xml.rels):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/table" Target="../tables/table1.xml" />
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing" Target="../drawings/drawing1.xml" />
</Relationships>
我們發現,作業表只要通過自己的關聯組件,就能找到需要的其他組件了,
我們使用 Content Type 檔案登記Excel檔案的組件資訊,然后通過關聯組件,將作業簿組件,作業表組件和繪圖、表格等其他組件組合在一起,最終將Excel檔案組織起來,
第三方庫
SheetJS
SheetJS3是用于多種電子表格格式的決議器和撰寫器,通過官方規范、相關檔案以及測驗檔案實作簡潔的JS方法,SheetJS強調決議和撰寫的穩健,其跨格式的特點和統一的JS規范兼容,并且ES3/ES5瀏覽器向后兼容IE6,
鏈接: https://github.com/SheetJS/sheetjs3
作業原理介紹
在SheetJS之前,處理電子表格檔案的介面只能用于特定的格式,許多第三方庫要么支持一種格式,要么為每一種支持的檔案型別提供一個不同的類集,雖然在Excel 2007里面引入了XLSB,但只有Sheet和Excel支持這種格式,
為了提高不可知格式的顯示,js-xlsx使用了被稱作[“Common Spreadsheet Format”]的純JS的顯示方法(#common-spreadsheet-format),強調一種統一的顯示方式,能夠有一些特點,比如格式轉換和嵌套class tap,通過提取出各種格式的復雜性,工具沒有必要擔心特定的檔案型別,
一個簡單的的物件顯示和細心的代碼練習相結合,能讓示例運行在較老的瀏覽器以及像ExtendScript和Web Workers這樣可選擇的環境里執行,雖然很想使用最新的和最好的特性,不過這些特性需要最新的瀏覽器,用以限制兼容性,
工具函式捕獲通用的使用例子,比如生成JS物件或HTML,大多數簡單例子的操作只要幾行代碼,大多數復雜的普遍的復雜操作應該直截了當的生成,
在Excel 2007種,Excel添加XSLX格式作為默認的起始端,然而,有一些其他格式會更多的出現上述的屬性,例如,XLSB格式XLSX格式相似,不過檔案會使用一半的空間,而且也會更開的打開檔案,雖然XLSX撰寫器可以使用,但是其他格式的撰寫器也可以使用,因此使用者能夠充分利用每一種格式獨特的特點,社區版本的主要關注點在正確的資料轉換,即從任意一個兼容的資料表示中提取資料,匯出適用于任意第三方介面的各種資料格式,
1.github安裝方式
git clone https://github.com/SheetJS/sheetjs.git
2.NPM的安裝方式
npm install xlsx
3.bower的安裝方式
bower install js-xlsx
如何使用
這里有完整的使用方法可以參考一下:https://github.com/rockboom/SheetJS-docs-zh-CN
知乎:https://zhuanlan.zhihu.com/p/386085542 ??
OOXML是基于XML的檔案格式標準: http://officeopenxml.com/anatomyofOOXML-xlsx.php ??
第三方庫:https://github.com/SheetJS/sheetjs ?? ??
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/413466.html
標籤:其他
上一篇:JS實作彈性漂浮廣告代碼
