[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]使用Excel另存为xml文件,替换内容动态生成Excel文件的一点注意事项

上一篇:[ASP]网页后退一步时,表单中select下拉框的值的恢复的解决方法
下一篇:[备忘]查询一个服务器上放了多少个虚拟主机站点……

添加日期:2007/8/28 17:11:46 快速返回   返回列表 阅读5866次
如果Excel的格式是固定的,那么直接另存为xml文件即可。

生成时,读入xml文件,直接将对应的标记替换为数据,然后输出即可。

但遇到一个格式不固定的,需要根据记录条数,输出表格的行……
----------------------------
首先,将模板的数据行只保留一行,另存。

随便弄了个,看看样子:


<?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">
  <Created>1996-12-17T01:32:42Z</Created>
  <LastSaved>2007-04-11T01:31:33Z</LastSaved>
  <Version>11.5703</Version>
 </DocumentProperties>
 <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
  <RemovePersonalInformation/>
 </OfficeDocumentSettings>
 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>4530</WindowHeight>
  <WindowWidth>8505</WindowWidth>
  <WindowTopX>480</WindowTopX>
  <WindowTopY>120</WindowTopY>
  <AcceptLabelsInFormulas/>
  <ProtectStructure>False</ProtectStructure>
  <ProtectWindows>False</ProtectWindows>
 </ExcelWorkbook>
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="宋体" x:CharSet="134" ss:Size="12"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="m21118956">
   <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:FontName="宋体" x:CharSet="134" ss:Size="16" ss:Color="#FF0000"/>
  </Style>
  <Style ss:ID="m21118966">
   <Alignment ss:Vertical="Center"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
  </Style>
  <Style ss:ID="m21118976">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
  </Style>
  <Style ss:ID="s43">
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
  </Style>
 </Styles>
 <Worksheet ss:Name="Sheet1">
  <Table ss:ExpandedColumnCount="7" ss:ExpandedRowCount="9" x:FullColumns="1"
   x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
   <Row ss:AutoFitHeight="0" ss:Span="3"/>
   <Row ss:Index="5" ss:AutoFitHeight="0">
    <Cell ss:Index="3" ss:MergeAcross="4" ss:MergeDown="1" ss:StyleID="m21118956"><Data
      ss:Type="String">娃哈哈</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0"/>
   <Row ss:AutoFitHeight="0">
    <Cell ss:Index="3" ss:MergeDown="1" ss:StyleID="m21118966"><Data
      ss:Type="String">序号</Data></Cell>
    <Cell ss:MergeAcross="3" ss:StyleID="m21118976"><Data ss:Type="String">销售额</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell ss:Index="4" ss:StyleID="s43"><Data ss:Type="String">1月</Data></Cell>
    <Cell ss:StyleID="s43"><Data ss:Type="String">2月</Data></Cell>
    <Cell ss:StyleID="s43"><Data ss:Type="String">3月</Data></Cell>
    <Cell ss:StyleID="s43"><Data ss:Type="String">4月</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell ss:Index="3"><Data ss:Type="Number">1</Data></Cell>
    <Cell><Data ss:Type="Number">100</Data></Cell>
    <Cell><Data ss:Type="Number">200</Data></Cell>
    <Cell><Data ss:Type="Number">300</Data></Cell>
    <Cell><Data ss:Type="Number">400</Data></Cell>
   </Row>
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <PageSetup>
    <Header x:Margin="0.51200000000000001"/>
    <Footer x:Margin="0.51200000000000001"/>
    <PageMargins x:Bottom="0.98399999999999999" x:Left="0.78700000000000003"
     x:Right="0.78700000000000003" x:Top="0.98399999999999999"/>
   </PageSetup>
   <Unsynced/>
   <Print>
    <ValidPrinterInfo/>
    <PaperSizeIndex>9</PaperSizeIndex>
    <HorizontalResolution>300</HorizontalResolution>
    <VerticalResolution>300</VerticalResolution>
   </Print>
   <Selected/>
   <Panes>
    <Pane>
     <Number>3</Number>
     <ActiveRow>11</ActiveRow>
     <ActiveCol>6</ActiveCol>
    </Pane>
   </Panes>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
</Workbook>



咋看挺晕,细看很规则,一层一层的关系,很清楚。

找到其中的:


   <Row ss:AutoFitHeight="0">
    <Cell ss:Index="3"><Data ss:Type="Number">1</Data></Cell>
    <Cell><Data ss:Type="Number">100</Data></Cell>
    <Cell><Data ss:Type="Number">200</Data></Cell>
    <Cell><Data ss:Type="Number">300</Data></Cell>
    <Cell><Data ss:Type="Number">400</Data></Cell>
   </Row>



它就是要重复的行,在前后做标记,如{{RowBegin}},{{RowEnd}}

读取后,就可以根据前后标记,将重复行模板分离出来。

然后循环记录,不断替换行模板即可。

行模板之前部分 + 若干行 + 行模板之后的部分 = 最后的结果

-------------------------------------------------------
欢欣鼓舞的运行,生成的文件Excel报错,然后请看....log文件。

按提示,到IE临时文件夹找到log文件,一看,类似于:


表 の XML エラー
理由 :    無効な値です
ファイル :    D:\xxx_RISEleoFs0mIz9Au4QqO.xml
グループ :    Table
タグ :    Row
属性 :    Index
値 :    11



这个行的Index无效,(这个错不是上面例子中的)。

查看xml文件,发现数据行之后,某个行有
<Row ss:Index=11...

搜索,得知:

如果某行有ss:Index=xx,则这一行是绝对定位的,也就是这行数据就是在这一行。

如果无,表示这行是相对定位的,也就是挨着上一行往下排……

所以,通常,一个表格,只有第一行有ss:Index,后面的没有,

这样,第一行一变,下面的行都跟着过去了。


接上面的错误:

一想,为什么报这个Index无效呢,应该是动态增加行后,这个11已经被用了,再用就重复了。

难道后面的ss:Index的值每次还得动态修改一下,真麻烦……

还好,只有一个,直接把ss:Index删除了。
------------------------------------
但还是不行……

有类似这么一行:

 <Worksheet ss:Name="Sheet1">
  <Table ss:ExpandedColumnCount="7" ss:ExpandedRowCount="9" x:FullColumns="1"

这个ss:ExpandedRowCount就是sheet页中的所有数据占用的行数,必须一致。

另外,每个sheet页都有一个该值,只有一个,是Table的属性

没法子,查找它,替换后面的数字。
----------------------------------------
最后OK了。
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved