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();