Excel导出方法总结篇

有时候会时不时碰到这个问题
虽然已经交过作业了,但每次温习居然都有新的感受和发现。
于是,再写一篇总结(也不知道是不是以后还会再写总结?),与大家分享一些所得。

我这里简单复习一下有哪几种可以导出Excel的方法:
1。把excel作为数据库,导入数据
这里分成两种:
一种是把excel作为connection,通过insert数据加入;
另一种,则是利用COM对象,调用Excel的QueryTables的方法。

2。直接引用COM组件,直接通过接口调用,并生成相应的Excel文件
这种方法对于性能要求要高一些。
对B/S结构的程序,基本不考虑这种方法。
对于无法关闭Excel进程的问题,今天看到一篇《立即释放.net下的com组件》
http://jjstar.cnblogs.com/archive/2004/11/08/61316.html
我要试试看


3。利用生成HTML代码,保存为Excel文件
这个方法经常在B/S使用。


前三种方法的使用可以参考
Powerlc的BLog 的《asp.net里导出excel表方法汇总 》
http://powerlc.cnblogs.com/archive/2005/01/06/87512.aspx

浪漫十一狼的Net日志 的《导出excel的另外一种方法》
http://elevenwolf.cnblogs.com/archive/2004/08/21/35324.aspx




4。不引用Excel接口,直接生成Excel文件的。
这里灵感之源写了一篇Blog
特别推荐:纯VB.NET代码直接生成Excel文件(不需要Excel)
http://www.cnblogs.com/unruledboy/archive/2004/07/07/22093.aspx
这里是C#版本
与灵感之源的vb.net对应的SmartExcel的C#版本

这个东西,我引用作者的一句话
引用不过说实话,没有一个WYSWYG的ide(如excel的designer),这段代码没什么作用。写一个excel文件,太复杂了。。。

5。用MIME格式描述多内容Excel工作表
有点象生成HTML,但不完全是。
这是由鸟食轩写的《用MIME格式描述多内容Excel工作表》
http://www.cnblogs.com/birdshome/archive/2005/07/12/190988.html
(鸟食轩是javascript应用的高手,他在自己Blog上面的百度搜索帖子功能,让我对javascript有了更多的理解)

这里已经说到有5种方法,难道还有?

是啊,是啊

我再说两种,嘿嘿,

6。Excel是可以存成xml
看看这个文件

在看看保存出来的Excel文件:
  1<?xml version="1.0"?>
  2<?mso-application progid="Excel.Sheet"?>
  3<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  4 xmlns:o="urn:schemas-microsoft-com:office:office"
  5 xmlns:x="urn:schemas-microsoft-com:office:excel"
  6 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  7 xmlns:html="http://www.w3.org/TR/REC-html40">
  8 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  9  <Created>1996-12-17T01:32:42Z</Created>
 10  <LastSaved>2006-02-17T13:16:06Z</LastSaved>
 11  <Version>11.5606</Version>
 12 </DocumentProperties>
 13 <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
 14  <RemovePersonalInformation/>
 15 </OfficeDocumentSettings>
 16 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
 17  <WindowHeight>4530</WindowHeight>
 18  <WindowWidth>8505</WindowWidth>
 19  <WindowTopX>480</WindowTopX>
 20  <WindowTopY>120</WindowTopY>
 21  <AcceptLabelsInFormulas/>
 22  <ProtectStructure>False</ProtectStructure>
 23  <ProtectWindows>False</ProtectWindows>
 24 </ExcelWorkbook>
 25 <Styles>
 26  <Style ss:ID="Default" ss:Name="Normal">
 27   <Alignment ss:Vertical="Bottom"/>
 28   <Borders/>
 29   <Font ss:FontName="宋体" x:CharSet="134" ss:Size="12"/>
 30   <Interior/>
 31   <NumberFormat/>
 32   <Protection/>
 33  </Style>
 34  <Style ss:ID="s32">
 35   <Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
 36   <Font x:Family="Swiss" ss:Size="16"/>
 37  </Style>
 38  <Style ss:ID="s34">
 39   <Borders>
 40    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
 41    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
 42    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
 43    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
 44   </Borders>
 45   <Font x:Family="Swiss" ss:Size="11" ss:Bold="1"/>
 46   <Interior ss:Color="#CCFFCC" ss:Pattern="Solid"/>
 47  </Style>
 48  <Style ss:ID="s35">
 49   <Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/>
 50   <Borders>
 51    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
 52    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
 53    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
 54    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
 55   </Borders>
 56   <Font x:Family="Swiss" ss:Size="11"/>
 57  </Style>
 58 </Styles>
 59 <Worksheet ss:Name="Sheet1">
 60  <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="22" x:FullColumns="1"
 61   x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
 62   <Column ss:AutoFitWidth="0" ss:Width="92.25"/>
 63   <Column ss:AutoFitWidth="0" ss:Width="123"/>
 64   <Row ss:AutoFitHeight="0" ss:Height="37.5">
 65    <Cell ss:MergeAcross="1" ss:StyleID="s32"><Data ss:Type="String">test excel Xml</Data></Cell>
 66   </Row>
 67   <Row ss:Height="15.75">
 68    <Cell ss:StyleID="s34"><Data ss:Type="String">testCol1</Data></Cell>
 69    <Cell ss:StyleID="s34"><Data ss:Type="String">testCol2</Data></Cell>
 70   </Row>
 71   <Row ss:Height="15">
 72    <Cell ss:StyleID="s35"><Data ss:Type="Number">3432</Data></Cell>
 73    <Cell ss:StyleID="s35"><Data ss:Type="Number">42343223</Data></Cell>
 74   </Row>
 75   <Row ss:Height="15">
 76    <Cell ss:StyleID="s35"><Data ss:Type="Number">3432</Data></Cell>
 77    <Cell ss:StyleID="s35"><Data ss:Type="Number">42343223</Data></Cell>
 78   </Row>
 79   <Row ss:Height="15">
 80    <Cell ss:StyleID="s35"><Data ss:Type="Number">3432</Data></Cell>
 81    <Cell ss:StyleID="s35"><Data ss:Type="Number">42343223</Data></Cell>
 82   </Row>
 83   <Row ss:Height="15">
 84    <Cell ss:StyleID="s35"><Data ss:Type="Number">3432</Data></Cell>
 85    <Cell ss:StyleID="s35"><Data ss:Type="Number">42343223</Data></Cell>
 86   </Row>
 87   <Row ss:Height="15">
 88    <Cell ss:StyleID="s35"><Data ss:Type="Number">3432</Data></Cell>
 89    <Cell ss:StyleID="s35"><Data ss:Type="Number">42343223</Data></Cell>
 90   </Row>
 91   <Row ss:Height="15">
 92    <Cell ss:StyleID="s35"><Data ss:Type="Number">3432</Data></Cell>
 93    <Cell ss:StyleID="s35"><Data ss:Type="Number">42343223</Data></Cell>
 94   </Row>
 95  
 96  </Table>
 97  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
 98   <Print>
 99    <ValidPrinterInfo/>
100    <PaperSizeIndex>9</PaperSizeIndex>
101    <HorizontalResolution>300</HorizontalResolution>
102    <VerticalResolution>300</VerticalResolution>
103   </Print>
104   <Zoom>89</Zoom>
105   <Selected/>
106   <Panes>
107    <Pane>
108     <Number>3</Number>
109     <RangeSelection>R1C1:R22C2</RangeSelection>
110    </Pane>
111   </Panes>
112   <ProtectObjects>False</ProtectObjects>
113   <ProtectScenarios>False</ProtectScenarios>
114  </WorksheetOptions>
115 </Worksheet>
116 <Worksheet ss:Name="Sheet2">
117  <Table ss:ExpandedColumnCount="0" ss:ExpandedRowCount="0" x:FullColumns="1"
118   x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25"/>
119  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
120   <ProtectObjects>False</ProtectObjects>
121   <ProtectScenarios>False</ProtectScenarios>
122  </WorksheetOptions>
123 </Worksheet>
124 <Worksheet ss:Name="Sheet3">
125  <Table ss:ExpandedColumnCount="0" ss:ExpandedRowCount="0" x:FullColumns="1"
126   x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25"/>
127  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
128   <ProtectObjects>False</ProtectObjects>
129   <ProtectScenarios>False</ProtectScenarios>
130  </WorksheetOptions>
131 </Worksheet>
132</Workbook>
133


yes,就是生成这么一个文件就可以了。
你可以把xml后缀名改成.xls,用Excel打开是一模一样的东西。
这个文件可以很快得到,只需将目前手里的Excel文件另存为xml(excel 2000也支持),


7。使用vbScript来生成Excel
以前我有个同事写了一个非常复杂的asp程序导出excel,实际上使用的是asp+vbScript来生成Excel,
(相比C#操作Excel,vsscript更有效率优势。)
但后来,我们过渡到.net程序了,我们也在没有管vbscript来导出Excel,因为它过于繁杂而不可维护和重用(其他项目中)。

再后来,通过对象编程的实践,才发现抽象能力是OPP思想中重要的能力,提炼问题核心建立相应模型,再来谈论解决方法。
 单用vbscript来生成的确很麻烦,而且不好维护。
我们需要加入自己的模型。
 

这篇blog的地址《作一个Excel exporter的组件》
http://www.cnblogs.com/king_astar/archive/2005/12/25/304306.aspx

第三方方法
Eunge
《报表的开发利器-ExcelQuicker》
http://lovinger2000.cnblogs.com/archive/2005/06/27/181853.aspx

 


 

posted on 2006-02-17 21:10  一望无际的南  阅读(22064)  评论(11编辑  收藏  举报

导航