积极答复者
.net 读取Excel文件的最好方式

问题
答案
-
- 已建议为答案 ThankfulHeartModerator 2013年8月23日 9:47
- 已标记为答案 Barry WangModerator 2013年9月3日 2:21
-
这要看你的需求是什么样的。
1) 如果你机器上面安装了Microsoft Excel, 可以使用Microsoft.Office.Interop.Excel,不同版本的excel操作的代码稍有不同,但是方法基本一致。这种方法操作excel速度相对来说比较慢。
2) 如果机器上面没有安装Microsoft Excel, 但是安装了Microsoft.Jet.OLEDB,那么可以用OleDb的方式读写Excel文件,跟操作数据库是一样的。但是这种方法对excel的操作时有限的,只能进行简单的读写,设置excel的单元格格式啊,对齐啊,设置密码保护啊都是实现不了的,不是最好的选择。
http://blog.csdn.net/vs2008aspnet/article/details/8457238
3) 以上2种方法excel 2003及以后的版本都支持,如果是excel 2007及以后的版本,那么还可以使用OpenXml SDK. 这个我没有使用过,但是据说速度不是太理想,而且需要下载安装。
4)如果既没有安装excel,又没有Microsoft.Jet.OLEDB驱动,而且想要支持excel 2003,2007,2010,那么NPOI应该是不错的选择,仅仅是一个1M多的dll,而且处理大批量数据速度也是很不错的。但是excel 2013目前好像还不支持,但是基本上可以满足大部分需求了。而且有一点比较好的就是它的文档是比较齐全的,网络上的资料也比较多。
小歐ou给的链接其实已经很不错了。
Caillen
<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.
- 已编辑 CaillenModerator 2013年8月23日 2:23 exit
- 已建议为答案 ThankfulHeartModerator 2013年8月23日 9:47
- 已标记为答案 Barry WangModerator 2013年9月3日 2:21
全部回复
-
-
- 已建议为答案 ThankfulHeartModerator 2013年8月23日 9:47
- 已标记为答案 Barry WangModerator 2013年9月3日 2:21
-
这要看你的需求是什么样的。
1) 如果你机器上面安装了Microsoft Excel, 可以使用Microsoft.Office.Interop.Excel,不同版本的excel操作的代码稍有不同,但是方法基本一致。这种方法操作excel速度相对来说比较慢。
2) 如果机器上面没有安装Microsoft Excel, 但是安装了Microsoft.Jet.OLEDB,那么可以用OleDb的方式读写Excel文件,跟操作数据库是一样的。但是这种方法对excel的操作时有限的,只能进行简单的读写,设置excel的单元格格式啊,对齐啊,设置密码保护啊都是实现不了的,不是最好的选择。
http://blog.csdn.net/vs2008aspnet/article/details/8457238
3) 以上2种方法excel 2003及以后的版本都支持,如果是excel 2007及以后的版本,那么还可以使用OpenXml SDK. 这个我没有使用过,但是据说速度不是太理想,而且需要下载安装。
4)如果既没有安装excel,又没有Microsoft.Jet.OLEDB驱动,而且想要支持excel 2003,2007,2010,那么NPOI应该是不错的选择,仅仅是一个1M多的dll,而且处理大批量数据速度也是很不错的。但是excel 2013目前好像还不支持,但是基本上可以满足大部分需求了。而且有一点比较好的就是它的文档是比较齐全的,网络上的资料也比较多。
小歐ou给的链接其实已经很不错了。
Caillen
<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.
- 已编辑 CaillenModerator 2013年8月23日 2:23 exit
- 已建议为答案 ThankfulHeartModerator 2013年8月23日 9:47
- 已标记为答案 Barry WangModerator 2013年9月3日 2:21
-
用NPOI操作起来很简单的,NPOI官方教程:http://tonyqus.sinaapp.com/
示例代码:
//读取excel模板 HSSFWorkbook workbook = null; using (FileStream fs = new FileStream("E:\\test.xlsx", FileMode.Open, FileAccess.Read)) { workbook = new HSSFWorkbook(fs); } HSSFSheet sheet = workbook.GetSheetAt(0) as HSSFSheet; IEnumerator enumerator = sheet.GetRowEnumerator(); while (enumerator.MoveNext()) { HSSFRow row = enumerator.Current as HSSFRow; for (int i = 0; i < row.LastCellNum; i++) { HSSFCell cell = row.GetCell(i) as HSSFCell; if (cell != null) { string cellValue = cell.StringCellValue; Console.WriteLine(cellValue); //修改记录,插入到数据库 } } }
这儿有一篇文章供你参考:http://www.cnblogs.com/downmoon/archive/2011/04/16/2017603.html
Caillen
<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.- 已建议为答案 ThankfulHeartModerator 2013年8月23日 9:47
-