How to read the xls and xlsx files using c#

廉价感情. 提交于 2019-12-01 15:13:06
Paul Grimshaw

You'll want the OpenXml SDK for the xlsx:

http://www.microsoft.com/en-gb/download/details.aspx?id=30425

But for the XLS, you won't be able to use this the XLS format is not based on xml.

I use the NPOI library for accessing older files:

http://npoi.codeplex.com/

The NPOI library also supports xlsx, so this would give you a consistent way of accessing them. Downside is you'll have to loop through sheets/rows/columns manually, and build up the dataset which will probably affect performance if you have large workbooks. If you want to use queries to access the data, OLEDB is the only method I've found.

If you have Excel installed, you could use Microsoft.Office.Interop.Excel;

http://support.microsoft.com/kb/302084

Vivek

Remove \ from the connection string. You can give as below.

conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+filepath.ToString() + ";Extended Properties=Excel 12.0 Xml;");
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!