XML to Excel

网页中很多地方会用到【导出到Excel】这个功能,方法有很多:

1. Html 中的table导出到Excel

<script language="javascript">
function
ToExcel(id){
try{
var table;
table = window.frames[id].InforTable;
var hang = table.rows.length;
var lie = table.rows(0).cells.length;
var oXL = new ActiveXObject("Excel.Application");
oXL.Visible = true;
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
// Add table headers going cell by cell.
for (i = 0; i < hang; i++) {
lie = table.rows(i).cells.length; //这种情况是每一行单元格数不一样的时候
for (j = 0; j < lie; j++) {
oSheet.Cells(i + 1, j + 1).value = table.rows(i).cells(j).innerText;
}
}
oSheet.ptintout;
oXL.UserControl = true;
}
catch (e) {
alert("浏览器安全性太高或禁止了弹出的ActiveX请求");
}
}
</script>

2.利用OWC导出Excel

3.一个简单的html页面,就像下面这个

<html>
<
body>
<
table>
<
tr>
<
td>Cell1</td><td>Cell2</td>
</
tr>
<
tr>
<
td>Cell3</td><td>Cell4</td>
</
tr>
</
table>
</
body>
</
html>

     将上面这个保存成txt,然后将扩展名修改为“xls”,再打开看时已经正常显示在Excel中,这样我们又有了一个方法,通过拼一个html的串,写入.xls扩展名的文件,这样就生成了一个Excel,而不受是否安装了Office的限制。当然,你也可以直接在页面的顶端加上(下面的代码是asp的)

Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "inline; filename=abc.xls"

     这样,当你打开这个界面的时候就会提示你是否保存,省去了些文件的麻烦。不过你也发现了它的美中不足,导出的Excel样式和平时直接在Excel中写的不一样。下面就介绍今天的重头戏,下面是一段xml,将它保存到txt,修改后缀名,就成了一个更高级一点的Excel,至少相对于前面的方法更高级。

    先对其中各个元素的作用进行一下解释:

Workbook工作簿  DocumentProperties文档属性  ExcelWorkbook Excel工作簿  Styles 格式    Style格式  Worksheet工作表    Tables表格    Row行

Cell单元          Data数据    WorksheetOptions工作表选项      Print打印        ValidPrinterInfo 有效打印机信息        HorizontalResolution 水平解决方案

VerticalResolution 垂直解决方案    Selected 被选中    Panes 面      Pane 面        Number 数字        ActiveRow 激活行

LeftColumnVisible 默认左边隐藏的列数          ExpandedRowCount 行数(值要大于等于下面是用的值)

  现在已经有很多值不起作用了,至少在office2007中已经不起作用,通过下面的xml和刚才可以输出文档的头,就能做出一个更加完美的导出Excel。

 

<?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>zhoudh</Author>
<
LastAuthor>zhoudh</LastAuthor>
<
Created>2009-06-08T01:36:00Z</Created>
<
LastSaved>2009-06-08T02:37:34Z</LastSaved>
<
Company></Company>
<
Version>10</Version>
</
DocumentProperties>
<
ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<
WindowHeight>9000</WindowHeight>
<
WindowWidth>14940</WindowWidth>
<
WindowTopX>240</WindowTopX>
<
WindowTopY>15</WindowTopY>
<
ProtectStructure>False</ProtectStructure>
<
ProtectWindows>False</ProtectWindows>
</
ExcelWorkbook>
<
Styles>
<
Style ss:ID="Default" ss:Name="Normal">
<
Alignment ss:Vertical="Center"/>
<
Borders/>
<
Font ss:FontName="宋体" x:CharSet="134" ss:Size="12"/>
<
Interior/>
<
NumberFormat/>
<
Protection/>
</
Style>
</
Styles>
<
Worksheet ss:Name="Sheet1">
<
Table ss:ExpandedColumnCount="8" ss:ExpandedRowCount="26" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
<
Column ss:Index="4" ss:Width="69.75"/>
<
Column ss:Index="7" ss:Width="216.75"/>
<
Row>
<
Cell>
<
Data ss:Type="String">姓名</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String">性别</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String">年龄</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String">职称</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String">薪资</Data>
</
Cell>
</
Row>
<
Row>
<
Cell>
<
Data ss:Type="String">张1</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String"></Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">20</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String">助理工程师</Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">2000</Data>
</
Cell>
<
Cell ss:Index="7">
<
Data ss:Type="String">    1.所有员工的个数。</Data>
</
Cell>
<
Cell ss:Formula="=COUNTA(RC[-6]:R[24]C[-6])">
<
Data ss:Type="Number">25</Data>
</
Cell>
</
Row>
<
Row>
<
Cell>
<
Data ss:Type="String">张2</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String"></Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">75</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String">高级工程师</Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">8000</Data>
</
Cell>
<
Cell ss:Index="7">
<
Data ss:Type="String">    2.男员工的个数。</Data>
</
Cell>
<
Cell ss:Formula="=COUNTIF(R[-1]C[-6]:R[23]C[-6],&quot;=男&quot;)">
<
Data ss:Type="Number">17</Data>
</
Cell>
</
Row>
<
Row>
<
Cell>
<
Data ss:Type="String">张3</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String"></Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">60</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String">高级工程师</Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">4600</Data>
</
Cell>
<
Cell ss:Index="7">
<
Data ss:Type="String">    3.有职称的员工个数。</Data>
</
Cell>
<
Cell ss:Formula="=COUNTA(R[-2]C[-4]:R[22]C[-4])">
<
Data ss:Type="Number">17</Data>
</
Cell>
</
Row>
<
Row>
<
Cell>
<
Data ss:Type="String">张4</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String"></Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">33</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String">工程师</Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">4800</Data>
</
Cell>
<
Cell ss:Index="7">
<
Data ss:Type="String">    4.无职称的员工个数。</Data>
</
Cell>
<
Cell ss:Formula="=COUNTBLANK(R[-3]C[-4]:R[21]C[-4])">
<
Data ss:Type="Number">8</Data>
</
Cell>
</
Row>
<
Row>
<
Cell>
<
Data ss:Type="String">张5</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String"></Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">16</Data>
</
Cell>
<
Cell ss:Index="5">
<
Data ss:Type="Number">1200</Data>
</
Cell>
<
Cell ss:Index="7">
<
Data ss:Type="String">    5.所有30岁以上男员工的个数。</Data>
</
Cell>
<
Cell ss:Formula="=SUMPRODUCT((R[-4]C[-6]:R[20]C[-6]=&quot;&quot;)*(R[-4]C[-5]:R[20]C[-5]&gt;=30))">
<
Data ss:Type="Number">10</Data>
</
Cell>
</
Row>
<
Row>
<
Cell>
<
Data ss:Type="String">张6</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String"></Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">19</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String">助理工程师</Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">2300</Data>
</
Cell>
<
Cell ss:Index="7">
<
Data ss:Type="String">    6.所有员工的薪资总额。</Data>
</
Cell>
<
Cell ss:Formula="=SUM(R[-5]C[-3]:R[19]C[-3])">
<
Data ss:Type="Number">85600</Data>
</
Cell>
</
Row>
<
Row>
<
Cell>
<
Data ss:Type="String">张7</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String"></Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">45</Data>
</
Cell>
<
Cell ss:Index="5">
<
Data ss:Type="Number">1500</Data>
</
Cell>
<
Cell ss:Index="7">
<
Data ss:Type="String">    7.所有男工程师的薪资总额。</Data>
</
Cell>
<
Cell ss:ArrayRange="RC" ss:Formula="=SUM((R[-6]C[-6]:R[18]C[-6]=&quot;&quot;)* (R[-6]C[-4]:R[18]C[-4]=&quot;工程师&quot;)* (R[-6]C[-3]:R[18]C[-3]))">
<
Data ss:Type="Number">23600</Data>
</
Cell>
</
Row>
<
Row>
<
Cell>
<
Data ss:Type="String">张8</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String"></Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">33</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String">工程师</Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">3200</Data>
</
Cell>
</
Row>
<
Row>
<
Cell>
<
Data ss:Type="String">张9</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String"></Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">64</Data>
</
Cell>
<
Cell>
<
Data ss:Type="String">高级工程师</Data>
</
Cell>
<
Cell>
<
Data ss:Type="Number">8500</Data>
</
Cell>
</
Row>
</
Table>
<
WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<
Selected/>
<
LeftColumnVisible>1</LeftColumnVisible>
<
Panes>
<
Pane>
<
Number>3</Number>
<
ActiveRow>5</ActiveRow>
<
ActiveCol>7</ActiveCol>
</
Pane>
</
Panes>
<
ProtectObjects>False</ProtectObjects>
<
ProtectScenarios>False</ProtectScenarios>
</
WorksheetOptions>
</
Worksheet>
<
Worksheet ss:Name="Sheet2">
<
Table ss:ExpandedColumnCount="0" ss:ExpandedRowCount="0" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25"/>
<
WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<
ProtectObjects>False</ProtectObjects>
<
ProtectScenarios>False</ProtectScenarios>
</
WorksheetOptions>
</
Worksheet>
<
Worksheet ss:Name="Sheet3">
<
Table ss:ExpandedColumnCount="0" ss:ExpandedRowCount="0" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25"/>
<
WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<
ProtectObjects>False</ProtectObjects>
<
ProtectScenarios>False</ProtectScenarios>
</
WorksheetOptions>
</
Worksheet>
</
Workbook>

注意:使用上面这个xml时最好指明编码方式为gb2312,否则容易出莫名其妙的错误!

本来想好好整理一下的,感觉有点懒,先放上来吧,下面引用的地方还有很多有意义的东西,如果谁用到可以好好的读一下。

感谢:

http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats

http://msdn.microsoft.com/en-us/library/bb226687%28office.11%29.aspx

http://www.oschina.net/discuss/thread/2431

你可能感兴趣的:(Excel)