faster Excel file access in C#

My C# application takes alot of data from an excel file and process it then write it back on the file.

The problem is when loading the data from the Excel file, it takes some time (30 seconds for about 7000 field of Excel)

I want to make it faster, is there's any method to make it faster by significant amount of time?

The code I use to fetch the data is:

streamReader = new StreamReader(@"ExcelPath.txt");
string path = streamReader.ReadLine();

//An Excel Application ==(contains)==> [Many] Excel WorkBook ==(contains)==>[Many] Excel WorkSheets==(contains)==>[Many] Ranges
Microsoft.Office.Interop.Excel.Application excelapp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook myworkbook = excelapp.Workbooks.Open(path, 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true);

Microsoft.Office.Interop.Excel.Sheets myworksheets = myworkbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet myworksheet =myworksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range range = myworksheet.UsedRange;

string[] data = new string[range.Rows.Count];
int start = 0;
for (start = 3; start < range.Rows.Count; start++)
{
    Microsoft.Office.Interop.Excel.Range myrange = myworksheet.get_Range("O" + start.ToString(), System.Reflection.Missing.Value);
    data[start] = myrange.Text;
    }

Answers


I have not done anything with Excel in a loooong time...but a few years ago we were able to speed up a few of our processes that read data from Excel reading ranges of cells at a time into an array (rather than going cell by cell).

Each read operation from C# to Excel has to go through COM and that's very expensive. If you manage to reduce how many times you need to read from Excel (e.g. by reading multiple values at once) you should see a significant improvement in speed.


Need Your Help

PHP using a variable in a mysql query with magic_quotes

php mysql security magic-quotes

I have a mysql query that uses a value in an array as part of the WHERE statement. How am I supposed to include this variable?

Friend Referal Script

php api contacts friend

I want to know how to write a script that connect to different mail servers and load the list of friends, something like that is found on facebook and other sites.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.