Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default reading xls file, speed problems

Hi, I am trying to load xls file, but it works VERY slow (~50 rows per
second), maybe it is possible to do it faster?

public class ExcelLoader
{
public static List<DataTable Load(string filename)
{
Excel.Application app = new Excel.Application();

Excel.Workbook workbook = app.Workbooks.Open(filename,
0, true, 5, "", "", true, Excel.XlPlatform.xlWindows,
"\t", false, false, 0, true);

List<DataTable tables = new List<DataTable();

foreach (Excel.Worksheet worksheet in workbook.Worksheets)
{
DataTable dt = LoadDataTable(worksheet);
tables.Add(dt);
}

app.Quit();

return tables;
}

private static DataTable LoadDataTable(Excel.Worksheet worksheet)
{
Excel.Range usedrange = worksheet.UsedRange;
int numrows = usedrange.Rows.Count;
int numcols = usedrange.Columns.Count;

string name = (string)worksheet.Name;
DataTable dt = new DataTable(name);

Program.TraceLine("Loading worksheet " + name);

for (int i = 0; i < numcols; i++)
{
Program.TraceLine("Creating column " + i.ToString());
dt.Columns.Add();
}

int rowid = 0;
foreach (Excel.Range row in worksheet.Rows)
{
Program.TraceLine("Creating row " + rowid.ToString());

DataRow dr = dt.NewRow();

int cellid = 0;
foreach (Excel.Range cell in row.Cells)
{
if (cell.Value != null)
dr[cellid] = cell.Value;

if (++cellid numcols)
break;
}

dt.Rows.Add(dr);

if (++rowid numrows)
break;
}

Program.TraceLine("Worksheet loaded");

return dt;
}
}

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with reading / writing ini-file Youp Excel Programming 4 February 14th 06 10:36 AM
Mircosoft Web Outlook having problems reading my mail Ms Vickie Excel Discussion (Misc queries) 0 November 25th 05 03:51 PM
Problems merging an excel file due to code or file problems? Cindy M -WordMVP- Excel Programming 0 September 14th 04 02:58 PM
Problems with reading files Philipp Oberleitner[_2_] Excel Programming 0 July 9th 04 05:04 PM
Macros i XL 2003 versus 2000 - speed problems Ankan[_2_] Excel Programming 2 December 10th 03 12:08 PM


All times are GMT +1. The time now is 06:54 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"