开发者

c# read excel worksheet

Could someone help me read a simple excel worksheet in 开发者_开发知识库c# app? I'd like to be able to iterate each row and have a handle on each of the columns.

Thanks, rod.


This one is the easiest method I have found:

Create Excel (.XLS and .XLSX) file from C#


The general method is to use Excel COM Interop. A quick google will find plenty of tutorials. Here's one for creating a sheet - it should point you in the direction (reading is pretty much the same).

An alternative method is to use ADO.Net. This is only really viable if your Excel sheet is well formed as a table ( ie. Database), but is easier than the interop approach.


Here is a sample using OLEDB

http://www.techiesweb.net/2009/12/reading-records-excel-file-insert-database-aspnet/


If you are going to open Excel 2007 or 2010 workbook (ooxml format), you can download Open XML SDK 2.0 for Microsoft Office (which doesn't require you to have MS office installed).


While Excel COM Interop works, it requires Excel to be installed on the client machine. If that isn't an issue then all good, but if it is you might consider looking at the Aspose.Cells library (no affiliation, just used them before). They're simple and powerful, although do carry a commercial license cost.


I've used ADO.NET and Jet in the past. Be warned that if you have columns that aren't obviously of one type you will see weird things happen. Jet tries to assign a datatype to a column based on the first several vales. The nice thing is that you can query the spreadsheet like it is a table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜