Excel导出方法总结篇

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

我这里简单复习一下有哪几种可以导出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
我要试试看 icon14.gif


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应用的高手 47.gif,他在自己Blog上面的百度搜索帖子功能,让我对javascript有了更多的理解)

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

是啊,是啊 laf.gif

我再说两种,嘿嘿,

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


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


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

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

这篇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

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值