By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,882 Members | 928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,882 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.