C# reading an Excel work book, OLEDB or Excel Interop? - c#

I need to read an Excel work book with 2 sheet which have more than 60,000 records. The application is an ASP.Net application so the performance matters.
Which approach should I take? Should I do it using Microsoft.Office.Interop.Excel or should I do it using OLEDB in ADO.Net?
What is the best approach?

Split the book into two worksheet files, convert them to CSV (comma-separated value) form, and process them as text.
I think that would be the best approach perfomance-wise, if you only need to read the data and do not care about formatting, merged cells, formulae etc.

I used always OLEDB in ADO.Net for it.
What the best aproach is, I don't know. But the easiest is like Kaerber says, to read it as 2 .csv files.
Then you can read it like you read a .txt file without 'fancy' libraries.

Use OLEDB for querying (reading) and Interop when modifying Excel.

You can consider using:
gemboxsoftware.com/spreadsheet
or the free alternative:
http://npoi.codeplex.com/

Related

c# store data in Excel file

I am obtaining data from a database that is returned in an XML string. What is the best way to then store this in an Excel file? Is there an XML - Excel tool, or is there an interim step?
Thanks.
The easiest way is probably just to loop through the XML-data either with XMLDocument or XDocument (linq) and write to a csv-file. The advantage with at csv file is that its readable with any text editor or other spreadsheet software.
You can read the data using the XMLDocument and then insert the data in the excel using Microsoft.Excel namespace.
Providing you're talking about Excel 2007 or newer, the format is XML-based. Check out any resources on OpenXML such as http://openxmldeveloper.org/ for details, or there's a video tutorial here that shows it http://www.asp.net/linq/videos/how-do-i-create-excel-spreadsheets-using-linq-to-xml . There's even things like http://code.google.com/p/linqtoexcel/ and an SDK here http://www.microsoft.com/downloads/en/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en .

The most efficient way to export large amount of data to excel

If you had to get large amount data(nearly 230.000 rows) from database(I use oracle) to an excel file with asp.net, how would you do it? I need some advice about this issue.
You should consider using csv(Comma Separated Value) file format. It is very lightweight and supported by excel:
http://creativyst.com/Doc/Articles/CSV/CSV01.htm
I would suggest that you take a look at NPOI library.

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.

Reading and parsing Excel files in C#

What is the best way/library to read Excel 2003 and 2007 files using C#? I need to do some heavy parsing.
Do you need to parse the file, or deal with the contents?
For parsing the file, you'd better hope it's in Open Office XML format, because the previous binary version is not documented at all.
If you just need to deal with the contents, use the Office Interop libraries.
You can try SmartXLS for .Net,it support most features of excel(cell formatting,Charts,formulas,pivot tables etc),and can read/write both the excel97-2003 xls format and the excel2007 openxml format.
I would start by trying to use ADO.NET.
If that doesn't work, I used xlsio by Syncfusion.
If the data is in some kind of table format I'd suggest to try using OleDbConnection and treating the Excel sheet as another data connection. Otherwise Interop is ok if it's not on a server or anything like that.
You can use the MS Office interop assemblies (see here) to access Excel files from .NET applications.
There are a number of 3rd party tools you can use. I would avoid using the Interop libraries as they can be pretty slow. I have used Aspose.Cells before and it works pretty well. It does cost some money though.

how Read and write binary excel file without com library

I want to read and write excel file . it can do with oledb , there is a limitation
maximunm characters < 255
solution is mono data type , now create table become second sheet (when open excel it is shown as hidden.)
please give proper answer
There's twothree (Thanks Behrooz) ways of doing this, the first is using FileHelpers to create a ordinary CSV file which can be read by Excel. The other way is to use a common BIFF file format, see here on CodeProject on how to accomplish this. The third way is to use an ODBC connection to the Excel spreadsheet as shown here on Codeproject also. The fastest way of doing it IMHO is to work with the BIFF format. Also here on CodeProject is a fast Excel reader, similar to the DataReader class but for Excel only.
OpenOffice.org has extensive documentation on the binary format for the excel spreadsheets here, this is a PDF document which outlines the BIFF layout and structure.
Edit: Amended this as Behrooz pointed out a small grammer-typo....
Hope this helps,
Best regards,
TOm.
SpreadsheetGear for .NET will do it.
You can see live ASP.NET samples with C# and VB source here and download a free trial here if you want to try it yourself. The trial is fully functional for 30 days.
Disclaimer: I own SpreadsheetGear LLC

Categories

Resources