简体   繁体   中英

How to import the data from an Excel spreadsheet so it can be manipulated in C#

I have an excel spread sheet (well, hundreds of them) which I need importing into a database. If the excel data was in a nice uniform format I would simply save them out to CSV, read them in using something like LINQ to CSV and save the required data away.

However, the excel spread sheet is 'uneven' in that different groups of cells contain different data. I need a way of grabbing the data and then working with cell references to grab the bits I need and save them to the database.

What's the best way to achieve this?

Thanks

UPDATE some more information

I have numerous spread sheets, all identical in structure that need to be imported into a database. The import is not simple in that different chunks of data from the spread sheet will go into different tables. The excel document itself contains a few sections (basically question / answer) type data. For each different section I need to grab the data, shape it into a form that makes sense in terms of the database and save it.

Ideally I would like to create a quick little WPF app that will let me select a spread sheet hit a button and perform the import.

You could use the Excel Object Model to read the data if you do it in a non web environment. See for example How to automate Microsoft Excel from Microsoft Visual C#.NET . If it has to be inside a web application. I suggest to use Aspose Cells .

Turn the Excel Spread sheet into an ODBC (Open Database Connectivity) Data Source so you can access it just like you would any database:

http://www.datamystic.com/datapipe/excel_odbc.html

Then access it just like any database using ODBC:

http://msdn.microsoft.com/en-us/library/system.data.odbc.odbcconnection(v=vs.71).aspx

When the data is not uniform, it is often better to keep your approach as simple as possible in the first instance. Start with vba and the "Range" object (which is part of the excel object heirarchy). From there you can increase the level of automation and in most instances reuse this "Range" work.

  avariable = Range("A2:A5")

That notiation is not going to change very much. It wont matter what final target language you use (language: C# / vba / etc).

There are a number of other ways of going about this -- java based / xml based / c# based / and a few other really cool ones that only apply to certain niche situations. If you can provide more information about your use case, then perhaps I can suggest some more things to try.


Q & A

example link for automation from C#: http://support.microsoft.com/kb/302084

You should probably take a look at Microsoft's Visual Studio Tools For Office (VSTO) , which handles a lot of the unpleasant COM/interop stuff for you.

To those who may be interested I ended up using LinqToExcel: http://code.google.com/p/linqtoexcel/

Did exactly what I was after with minimal fuss. Excellent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM