471,348 Members | 1,316 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

best way to read specific record from Excel file in C#

Greetings all,

What is the best way to access specific records in an Excel file?

I have an Excel file, I want to randomly and repeatedly (maybe around up to
50 times) draw some rows of data from this Excel file and store them in a
struct/class for further processing. (mostly just display the info in
graphical format)

This is not some sort of database application, but rather I am
drawing some data for display in a series of experiments. (A
user evaluates how data is displayed)

I am not sure my current approach is the best way to do this, this is
what I do right now:

Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(fileName,0, false, 5,
"", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);

// get the collection of sheets in the workbook
Excel.Sheets sheets = theWorkbook.Worksheets;

// get the first and only worksheet from the collection of worksheets
worksheet = (Excel.Worksheet)sheets.get_Item(1);

(more of this code below)

I would appreciate suggestions/web pointers/sample code on how to do
this best.
Thanks!

Esmail

------------------

what I am doing now:

private Excel.Application ExcelObj = null;
ExcelObj = new Excel.Application();

OpenExcelFile(_fileName);
openedFile = true;
----

private void OpenExcelFile(string fileName)
{
Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(fileName,0,
false, 5,
"", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);

// get the collection of sheets in the workbook
Excel.Sheets sheets = theWorkbook.Worksheets;

// get the first and only worksheet from the collection of worksheets
worksheet = (Excel.Worksheet)sheets.get_Item(1);

max_rec = 1000;

// loop through the rows of the spreadsheet and place each row in the
list view
for (int i = 1; i <= max_rec; i++)
{
Excel.Range range = worksheet.get_Range("A"+i.ToString(), "J" +
i.ToString());
System.Array myvalues = (System.Array)range.Cells.Value2;
string strArray = ConvertToString(myvalues);
listView1.Items.Add(new ListViewItem(strArray));
}
}

---

// to close up the program/excel file

ExcelObj.Quit();
Application.Exit();
Jan 31 '06 #1
1 4476
anyone? somehow i can't believe i'm doing this the best
way already .. so if you have anything to share i'd love
to hear it.

thanks,
esmail
Feb 3 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Ronald S. Cook | last post: by
1 post views Thread by colleen1980 | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.