开发者

C# Reading an Excel file by column Index

开发者 https://www.devze.com 2023-02-23 06:13 出处:网络
I need to read an excel file using OLEDB by specifying column indexes. For an example I need to red data in Column A,B,C,AA,BC,ect... (the column indexes in Excel)

I need to read an excel file using OLEDB by specifying column indexes. For an example I need to red data in Column A,B,C,AA,BC,ect... (the column indexes in Excel)

开发者_如何学编程

How can Ido that?

Thanks in advance.


Did you tried a query like this SELECT A,B,C,AA,BC FROM Sheet1$? Make sure you put Extended Properties="Excel 12.0;HDR=YES in the OLEDB connection string, it will help you to treat the first row as header.


Thanushka,

I've used the excel 2007 openxml sdk (DocumentFormat.OpenXml) to great effect in this scenario. It's basically a LINQ library that takes the excel docs into objects and allows you to query inside c# just like any other LINQ object. Microsoft actually do have (after a quick search) a pretty good 'idiots guide' on this topic. You can find good resources here:

http://msdn.microsoft.com/en-us/library/dd920313%28v=office.12%29.aspx

http://blogs.msdn.com/b/johnrdurant/archive/2010/02/19/excel-open-xml-linq-part-i.aspx

http://www.briankeating.net/blog/post/2010/04/26/Linq-to-Xlsx.aspx

if you use LINQ, it's a no-brainer and is definitely the only way that I would go with this type of task. For me, it's much more intuitive to use practices akin to the way that other objects are used in your application and thus this LINQ approach works well if that's your bag.


First just write a little function to convert A,B,C,AA... to 1,2,3,27,... Then use the numbered index to locate the column. There should be plenty of examples online to help you do this bit.

0

精彩评论

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