关键词搜索

源码搜索 ×
×

基于 DocumentFormat.OpenXml 操作 Excel (2)-- 文档结构

发布2021-01-06浏览859次

详情内容

上一节初步使用了OpenXML 的SDK , DocumentFormat.OpenXml 来操作生成一个Excel ,为了更好的理解这个类库的使用,需要大致了解一个Excel文档的基本结构。 而操作这个以及这个类vb.net教程库里面大致常用类的组织方式。

在OpenXML 的SDK 中,SpreadsheetDocument 类,表示 Excel 文档包。要操作Excel,都需要基于它来创建 的一个c#教程实例,然后来操作。其中操作Excel的时候,大多数要用到的类型及其关系如下图所示:
在这里插入图片描述

以上只是展示部分类型,还有一些其它的不怎么用的类型没有展示如 代表主题的 ThemePart,代表对话框表格的DialogsheetPart 等等。 另外我们新建一个Excel文档【TestExcelFile.xlsx】,输入几个单元格数据, 表头姓名和班级,微软雅黑字体,加粗和居中,同时增加三行数据。保存后,修改后缀名为zip, 并且解压来对比查看。 这里需要注意,用office excel软件 和 wps 软件,创建的excel文件,会不大一样的,因为软件初始化的时候是不一样,给默认excel保存的python基础教程东西也是不一样。

具体如下图所示:

在这里插入图片描述

接下来,通过上面第一张关于OpenXML SDK 中类的关系图,和第二张图所展示的解压缩后的Excel信息 来一起对比看看。

–》Excel 文件 与 SpreadsheetDocument 类型
  SpreadsheetDocument 就表示一个Excel文档包(不止是xlsx,也可以是xlsm等其它excel文件类型), 一个Excel文档,它内部是由多个部件来组成的。而[Content_Types].xml 这个文件,包含了这个Excel文档包里所有相关部件的关联信息,这个里面包含了有什么部件,每个部件放在什么文件,而它的内容类型是什么。

具体内容如下:

 1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 2 <Types xmlns="http://schemas.openxmlformats.org/packagehttps://cdn.jxasp.com:9143/image/2006/content-types">
 3     <Default Extension="bin" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings"/>
 4     <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
 5     <Default Extension="xml" ContentType="application/xml"/>
 6     <Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>
 7     <Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
 8     <Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>
 9     <Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>
10     <Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>
11     <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
12     <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
13     <Override PartName="/docProps/custom.xml" ContentType="application/vnd.openxmlformats-officedocument.custom-properties+xml"/>
14 </Types>

    在DocumentFormat.OpenXml 这个SDK里面,包含了很多类后面会有一个Part结尾,比如WorkbookPart, WorksheetPart ,WorkbookStylesPart 等等,这种就表示一个Excel文档里面的其中一个部件,而部件是有层级关联的,部件包含子部件。比如 WorkbookPart 是基于 SpreadsheetDocument , 而 WorksheetPart 是基于 WorkbookPart; 而每一个Part都有一个对应的XML文件来描述这个部件的整体信息,通过xml元素来描述部件的内容,而这些部件最终结合起来,成为我们看到的Excel文档。

    –》workbook.xml 文件 与 WorkbookPart 类型
      SpreadsheetDocument 下一般会创建一个关键部件,就是WorkbookPart ,代表工作簿,对应的就是在解压后的文件里面的xl文件夹里面的workbook.xml文件。而通常WorkbookPart 里面的还需要创建一个Workbook对象( workbookpart.Workbook = new Workbook() )。那么Workbook 和 WorkbookPart 之间的联系是什么呢? WorkbookPart 有一个对应的xml文件来描述它,那么Workbook 就是这个xml文件的根节点。

    打开workbook.xml文件,如下

     1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
     2 <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetmlhttps://cdn.jxasp.com:9143/image/2006/main"
     3     xmlns:r="http://schemas.openxmlformats.org/officeDocumenthttps://cdn.jxasp.com:9143/image/2006/relationships"
     4     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibilityhttps://cdn.jxasp.com:9143/image/2006" mc:Ignorable="x15"
     5     xmlns:x15="http://schemas.microsoft.com/office/spreadsheetmlhttps://cdn.jxasp.com:9143/image/2010/11/main">
     6     <fileVersion appName="xl" lastEdited="6" lowestEdited="6" rupBuild="14420"/>
     7     <workbookPr filterPrivacy="1" defaultThemeVersion="164011"/>
     8     <bookViews>
     9         <workbookView xWindow="0" yWindow="0" windowWidth="22260" windowHeight="12645"/>
    10     </bookViews>
    11     <sheets>
    12         <sheet name="Sheet1" sheetId="1" r:id="rId1"/>
    13     </sheets>
    14     <calcPr calcId="162913"/>
    15     <extLst>
    16         <ext uri="{140A7094-0E35-4892-8432-C4D2E57EDEB5}"
    17             xmlns:x15="http://schemas.microsoft.com/office/spreadsheetmlhttps://cdn.jxasp.com:9143/image/2010/11/main">
    18             <x15:workbookPr chartTrackingRefBase="1"/>
    19         </ext>
    20     </extLst>
    21 </workbook>
    
      15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    workbook.xml文件,根节点就是对应代码中的Workbook类对象。每个元素节点,都会有对应的类型,比如sheets节点元素,对应 Sheets 类型,而 sheet节点元素,对应Sheet 类型,一般元素节点名称 和 对应的对象类型名称都一致,但是也不一定是,有时候元素节点是缩写,简写之类的,比如上面的 calcPr 节点元素,对应则是 CalculationProperties 类。

    workbook 元素下面,很多子元素,每一个元素作用,则需要查询微软的文档,和对应的Open XML 标准说明,才能知道。 但是我们做功能导入导出,一般涉及到的,无非是 sheets节点元素和其下的sheet节点元素 。 上一节中,已经针对节点对应的 Sheets 类型 和 Sheet 类型做过了说明,下面再详细说明一次。

    Sheets 类型对应XML里面 sheets节点元素,它作用就是类似数组和列表;而Sheet 类型,对应sheet节点元素 , 表示一个工作簿中用到的表,一般情况下,我们常用的一个是 工作表(WorkSheet),还有可能会用到的就是图表(ChartSheet)了。而由于这些表的定义比较复杂,所以Excel中,都是一个独立的部件(有单独的xml来描述)存在。 而这里的sheet 就只是一个关联关系,有对应表展示的名字,表的Id,所关联的代表具体表的部件,和表的类型是什么,默认情况下没有type属性,就是工作表(WorkSheet)。

    –》Excel 文件 与 SpreadsheetDocument 类型
      既然说了Sheet,就来看看这个WorksheetPart 类型,这个部件属于WorkbookPart 的子部件,它需要通过WorksheetPart 类型对象来创建(语法:workbookPart.AddNewPart();),并且可以创建多个。在上图中所示,xl文件夹中,有一个worksheets文件夹,里面会存在多个XML文件,每一个XML文件描述一个WorksheetPart部件的信息, 而 打开Sheet1.xml查阅其信息, XML根节点元素是 worksheet ,对应我们的类型就是 Worksheet类,其子元素有 sheetViews,sheetFormatPr ,sheetData等等。

    具体的XML可以看看下面代码:

     1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
     2 <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetmlhttps://cdn.jxasp.com:9143/image/2006/main"
     3     xmlns:r="http://schemas.openxmlformats.org/officeDocumenthttps://cdn.jxasp.com:9143/image/2006/relationships"
     4     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibilityhttps://cdn.jxasp.com:9143/image/2006" mc:Ignorable="x14ac"
     5     xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetmlhttps://cdn.jxasp.com:9143/image/2009/9/ac">
     6     <dimension ref="A1:B4"/>
     7     <sheetViews>
     8         <sheetView tabSelected="1" workbookViewId="0"/>
     9     </sheetViews>
    10     <sheetFormatPr defaultRowHeight="14.25" x14ac:dyDescent="0.2"/>
    11     <sheetData>
    12         <row r="1" spans="1:2" ht="15" x14ac:dyDescent="0.25">
    13             <c r="A1" s="1" t="s">
    14                 <v>0</v>
    15             </c>
    16             <c r="B1" s="1" t="s">
    17                 <v>1</v>
    18             </c>
    19         </row>
    20         <row r="2" spans="1:2" x14ac:dyDescent="0.2">
    21             <c r="A2" t="s">
    22                 <v>2</v>
    23             </c>
    24             <c r="B2" t="s">
    25                 <v>3</v>
    26             </c>
    27         </row>
    28         <row r="3" spans="1:2" x14ac:dyDescent="0.2">
    29             <c r="A3" t="s">
    30                 <v>5</v>
    31             </c>
    32             <c r="B3" t="s">
    33                 <v>3</v>
    34             </c>
    35         </row>
    36         <row r="4" spans="1:2" x14ac:dyDescent="0.2">
    37             <c r="A4" t="s">
    38                 <v>6</v>
    39             </c>
    40             <c r="B4" t="s">
    41                 <v>4</v>
    42             </c>
    43         </row>
    44     </sheetData>
    45     <phoneticPr fontId="1" type="noConversion"/>
    46     <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
    47     <pageSetup paperSize="9" orientation="portrait" horizontalDpi="300" verticalDpi="300" r:id="rId1"/>
    48 </worksheet>
    
      15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    从上面的这个XML文档, worksheet根节点元素,表示一个工作表,其子元素就是构造这个工作表的主要部分。

    简单叙述下几个节点的意义:

    (1) dimension 节点元素: 对应类型SheetDimension类型,表示维度,工作表维度,工作表中使用的单元格的行和列的边界。一般可以理解为你在工作表中,输入的所有数据中,使用到最大的列,使用的最大的行数,所形成的一个四方形区间。

    (2) sheetViews 节点元素: 对应类型SheetViews类型,工作表视图定义,一般用它表示当前工作表选择的部分,比如你光标所在的位置,选中的部分等等。 比如下方代码片段,结合selection 节点,activeCell属性表示选择框在哪个单元格,sqref属性表示选择的部分;当然,Excel里面是可以选中多个区域,那么会变成是: 这样子的。

    <sheetViews>
        <sheetView tabSelected="1" workbookViewId="0">
            <selection activeCell="A1" sqref="A1"/>
        </sheetView>
    </sheetViews>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    不过在 sheetViews 节点元素下,还有其它的功能,涉及多窗口窗格子的选择等等,具体可以再细细研究。

    (3) sheetFormatPr 节点元素:对应类型 SheetFormatProperties 类型,工作表的格式属性,一般可以设置默认行高,默认的列宽度等等。

    (4) sheetData 节点元素:对应类型 SheetData 类型,这个表示单元格表的数据信息的集合,里面包含了每个单元格的信息。这个基本上是我们做导入导出等功能的时候,最需要打交道的类型之一了。从上面XML代码中,看起来还是很容易理解的。 在这个sheetData节点下,有节点元素row (对应类型Row类型),表示一行; 而节点元素row的下面,有节点元素c(对应类型Cell类型),表示一单元格;其节点元素c的下面,有节点元素v(对应类型CellValue类型), 表示单元格的内容值,不过这个值的内容,和其节点元素c的一个t属性有关系, 后续再来探讨。

    (5) phoneticPr 节点元素: 对应类型 PhoneticProperties类型,这个和拼音文本的有关。

    (6) pageMargins 节点元素: 对应类型 PageMargins类型,定义工作表的页边距,对应Excel功能里面的页面布局-页边距功能。

    (7) pageSetup 节点元素: 对应类型 PageSetup类型,涉及到Excel页面设置的一些功能内容,包含纸张大小,纸张方向,纸张高度,宽度等等。

    根据上述提及的 XML 元素节点,和最上面的图 对比来看,WorksheetPart 类型下方,还有几个类型没有出现。 其中 Columns类型 和 Column类型, 这2个类型一般是用来定义列宽。而MergeCells 类型 和 MergeCell 类型主要是合并单元格相关。

    –》sharedStrings.xml 文件 与 SharedStringTablePart 类型
      sharedStrings.xml 文件,对应类型 SharedStringTablePart 类型,这部分主要表示,共享字符串部分,用来减少文件大小的。

    具体的XML可以看看下面代码:

     1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
     2 <sst xmlns="http://schemas.openxmlformats.org/spreadsheetmlhttps://cdn.jxasp.com:9143/image/2006/main" count="8" uniqueCount="7">
     3     <si>
     4         <t>姓名</t>
     5         <phoneticPr fontId="1" type="noConversion"/>
     6     </si>
     7     <si>
     8         <t>班级</t>
     9         <phoneticPr fontId="1" type="noConversion"/>
    10     </si>
    11     <si>
    12         <t>王同学</t>
    13         <phoneticPr fontId="1" type="noConversion"/>
    14     </si>
    15     <si>
    16         <t>一班</t>
    17         <phoneticPr fontId="1" type="noConversion"/>
    18     </si>
    19     <si>
    20         <t>二班</t>
    21         <phoneticPr fontId="1" type="noConversion"/>
    22     </si>
    23     <si>
    24         <t>陈同学</t>
    25         <phoneticPr fontId="1" type="noConversion"/>
    26     </si>
    27     <si>
    28         <t>林同学</t>
    29         <phoneticPr fontId="1" type="noConversion"/>
    30     </si>
    31 </sst>
    
      15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    根据以上的XML代码,结合我们Excel文件输入的数据,Excel默认把这些数据存放在这个共享字符串变量里面,而单元格的值是纯粹引用这里而已,后续再探讨。

    –》styles.xml 文件 与 WorkbookStylesPart 类型
      styles.xml 文件,对应类型 WorkbookStylesPart 类型。 这个类型相对复杂很多,主要涵盖的内容是Excel里面的样式,包含字体的定义,填充色定义,边框定义等等。

    样式部分暂时不在这里探讨了,后续单独说明。可以先看看具体的XML代码:

     1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
     2 <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetmlhttps://cdn.jxasp.com:9143/image/2006/main"
     3     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibilityhttps://cdn.jxasp.com:9143/image/2006" mc:Ignorable="x14ac x16r2"
     4     xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetmlhttps://cdn.jxasp.com:9143/image/2009/9/ac"
     5     xmlns:x16r2="http://schemas.microsoft.com/office/spreadsheetmlhttps://cdn.jxasp.com:9143/image/2015/02/main">
     6     <fonts count="3" x14ac:knownFonts="1">
     7         <font>
     8             <sz val="11"/>
     9             <color theme="1"/>
    10             <name val="等线"/>
    11             <family val="https://cdn.jxasp.com:9143/image/2"/>
    12             <scheme val="minor"/>
    13         </font>
    14         <font>
    15             <sz val="9"/>
    16             <name val="等线"/>
    17             <family val="3"/>
    18             <charset val="134"/>
    19             <scheme val="minor"/>
    20         </font>
    21         <font>
    22             <b/>
    23             <sz val="11"/>
    24             <color theme="1"/>
    25             <name val="微软雅黑"/>
    26             <family val="https://cdn.jxasp.com:9143/image/2"/>
    27             <charset val="134"/>
    28         </font>
    29     </fonts>
    30     <fills count="https://cdn.jxasp.com:9143/image/2">
    31         <fill>
    32             <patternFill patternType="none"/>
    33         </fill>
    34         <fill>
    35             <patternFill patternType="gray125"/>
    36         </fill>
    37     </fills>
    38     <borders count="1">
    39         <border>
    40             <left/>
    41             <right/>
    42             <top/>
    43             <bottom/>
    44             <diagonal/>
    45         </border>
    46     </borders>
    47     <cellStyleXfs count="1">
    48         <xf numFmtId="0" fontId="0" fillId="0" borderId="0"/>
    49     </cellStyleXfs>
    50     <cellXfs count="https://cdn.jxasp.com:9143/image/2">
    51         <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
    52         <xf numFmtId="0" fontId="https://cdn.jxasp.com:9143/image/2" fillId="0" borderId="0" xfId="0" applyFont="1" applyAlignment="1">
    53             <alignment horizontal="center"/>
    54         </xf>
    55     </cellXfs>
    56     <cellStyles count="1">
    57         <cellStyle name="常规" xfId="0" builtinId="0"/>
    58     </cellStyles>
    59     <dxfs count="https://cdn.jxasp.com:9143/image/2">
    60         <dxf>
    61             <font>
    62                 <b/>
    63                 <i val="0"/>
    64             </font>
    65             <fill>
    66                 <patternFill>
    67                     <bgColor rgb="FFD7D7D7"/>
    68                 </patternFill>
    69             </fill>
    70         </dxf>
    71         <dxf>
    72             <font>
    73                 <b val="0"/>
    74                 <i val="0"/>
    75             </font>
    76             <fill>
    77                 <patternFill patternType="none">
    78                     <bgColor indexed="65"/>
    79                 </patternFill>
    80             </fill>
    81         </dxf>
    82     </dxfs>
    83     <tableStyles count="1" defaultTableStyle="TableStyleMedium2" defaultPivotStyle="PivotStyleLight16">
    84         <tableStyle name="MySqlDefault" pivot="0" table="0" count="https://cdn.jxasp.com:9143/image/2">
    85             <tableStyleElement type="wholeTable" dxfId="1"/>
    86             <tableStyleElement type="headerRow" dxfId="0"/>
    87         </tableStyle>
    88     </tableStyles>
    89     <extLst>
    90         <ext uri="{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}"
    91             xmlns:x14="http://schemas.microsoft.com/office/spreadsheetmlhttps://cdn.jxasp.com:9143/image/2009/9/main">
    92             <x14:slicerStyles defaultSlicerStyle="SlicerStyleLight1"/>
    93         </ext>
    94         <ext uri="{9260A510-F301-46a8-8635-F512D64BE5F5}"
    95             xmlns:x15="http://schemas.microsoft.com/office/spreadsheetmlhttps://cdn.jxasp.com:9143/image/2010/11/main">
    96             <x15:timelineStyles defaultTimelineStyle="TimeSlicerStyleLight1"/>
    97         </ext>
    98     </extLst>
    99 </styleSheet>
    
      15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99

    –》小结
      本节大概说明了下常用类型和Excel文档包中关联的XML的对应关系,大概总结为如下对应图:

    在这里插入图片描述

    相关技术文章

    点击QQ咨询
    开通会员
    返回顶部
    ×
    微信扫码支付
    微信扫码支付
    确定支付下载
    请使用微信描二维码支付
    ×

    提示信息

    ×

    选择支付方式

    • 微信支付
    • 支付宝付款
    确定支付下载