0
点赞
收藏
分享

微信扫一扫

用XML操作Excel文件的一些属性说明

泠之屋 2022-11-01 阅读 203


在利用velocity导出excel中遇到了一个坑,理论上讲是没有问题的,看了vm文件也没有问题,但是打开生成的vm文件时会提示文件已损坏。
经研究,Excel在生成xml的时候为了不浪费资源,在生成时候,会指定Excel有多少行,如果超出了这个长度的话,它就会编译不通过并报错,也就是遇到的这个问题。

解决方法:
修改它的属性值​​​"ss:ExpandedRowCount"​​,把它设置大一点,或者通过程序它传值

顺便整理了一些值得注意的地方:
一 .文件头部有以下信息:

<?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> <Author>xRoy</Author> <LastAuthor>xRoy</LastAuthor> <Created>2009-02-25T03:21:56Z</Created> <Company>SunnySec</Company> <Version>11.9999</Version> </DocumentProperties>

其中,一下几个部分可以修改,其意思很简单,懂点E文就知道了.

<Author>xRoy</Author> <LastAuthor>xRoy</LastAuthor> <Created>2009-02-25T03:21:56Z</Created> <Company>SunnySec</Company> <Version>11.9999</Version>

二 .接下来你将看到​​<Styles>​​部分,大概就像这样:

<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/><Borders/>
<Font ss:FontName="Arial" x:Family="Swiss"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s21">
<Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
<Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"
ss:Color="#000000"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"
ss:Color="#000000"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"
ss:Color="#000000"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"
ss:Color="#000000"/>
</Borders>
<Font ss:FontName="微软雅黑" x:CharSet="134" x:Family="Swiss"/>
<Interior ss:Color="#FFCC99" ss:Pattern="Solid"/>
</Style>
</Styles>

​<Styles>​​​部分定义了一个样式的集合,每一种样式都在​​<Style></Style>​​​中描述,我的理解是,每一个样式都是代表了一个单元格的形态,注意是一个,能不能在一个单元格上组合多个此处定义的样式,还没测试.在​​<Style>​​​中:
​​​ss:ID​​​定义该样式的名称,要使用一个样式的时候需要用到.
​​​<Borders><Borders/>​​​这个东西就说明当前单元格的四个边的样式(有没有线条?线条多粗…)
​​​Font​​​定义单元格上文字的样式
​​​Interior​​​: 指的是底色,​​ss:Color​​​用于描述颜色,​​ss:Pattern​​​ 描述如何绘制
三 .数据部分
在这部分,你会看见类似这样的内容:

<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="9" ss:ExpandedRowCount="6" x:FullColumns="1" x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
<Column ss:AutoFitWidth="0" ss:Width="50.25"/>
<Column ss:AutoFitWidth="0" ss:Width="92.25"/>
<Column ss:AutoFitWidth="0" ss:Width="102.75"/>
<Column ss:AutoFitWidth="0" ss:Width="80.25"/>
<Column ss:AutoFitWidth="0" ss:Width="83.25"/>
<Column ss:AutoFitWidth="0" ss:Width="59.25"/>
<Column ss:AutoFitWidth="0" ss:Width="58.5"/>
<Column ss:AutoFitWidth="0" ss:Width="133.5"/>
<Column ss:AutoFitWidth="0" ss:Width="198.75"/>

说明:
​​​<Worksheet ss:Name="Sheet1">​​​ 这里说明有一个叫做Sheet1的表单
​<Table ss:ExpandedColumnCount="9" ss:ExpandedRowCount="6" x:FullColumns="1" x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">​​ 对于这个表单的总体数据描述:
​ss:ExpandedColumnCount="9"​​ 数据表格有9列
​ss:ExpandedRowCount="6"​​ 数据表格有6行
后面的就是设置一些默认属性,如果你在后面具体的表格上没有指定样式,就会用到这些.
​<Column ss:AutoFitWidth="0" ss:Width="50.25"/>​​ 依次定义ss:ExpandedColumnCount个列的宽和高
接下来就是数据部分了,完整的一行大概是这样的:

<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s22"><Data ss:Type="String" x:Ticked="1">交易类型</Data></Cell>
<Cell ss:StyleID="s22"><Data ss:Type="String" x:Ticked="1">产品名称</Data></Cell>
<Cell ss:StyleID="s22"><Data ss:Type="String" x:Ticked="1">收方单位</Data></Cell>
<Cell ss:StyleID="s22"><Data ss:Type="String" x:Ticked="1">产品开始序号</Data></Cell>
<Cell ss:StyleID="s22"><Data ss:Type="String" x:Ticked="1">产品结束序号</Data></Cell>
<Cell ss:StyleID="s22"><Data ss:Type="String" x:Ticked="1">数量</Data></Cell>
<Cell ss:StyleID="s22"><Data ss:Type="String" x:Ticked="1">操作员</Data></Cell>
<Cell ss:StyleID="s22"><Data ss:Type="String" x:Ticked="1">时间</Data></Cell>
<Cell ss:StyleID="s22"><Data ss:Type="String" x:Ticked="1">备注</Data></Cell>
</Row>

​<Row ></Row>​​​部分描述的这一行的默认属性,它也可以指定样式,比如​​<Row ss:AutoFitHeight="0" ss:StyleID="s22">​​​,这样的话,在这一行上面,任何没有特殊指定样式的表格都是这个默认样式.
​​​<Cell><Cell/>​​​部分描述具体一个表格的样式,必须使用前面你已经定义好的​​<Style>​​​.
四 .数据尾部
就是这个样子:

<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> 
<Unsynced/>
<Selected/>
<Panes> <Pane>
<Number>3</Number>
<ActiveRow>6</ActiveRow> </Pane> </Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions> </Worksheet>


举报

相关推荐

0 条评论