Looking for help on how to read a few cell values from Excel. I thought I'd start with one.
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
var cell = (Excel.Range)excelWorksheet.Cells[1, 1];
Though I am having no luck. I want to be able to read the value of the cell and store it somewhere, and then have it check every few seconds for the value.
You seriously want to take a look at EPPLus: http://epplus.codeplex.com/
It's available from NuGet for usage in your projects. It's by far the most easy and clean way of interfacing between C# and an excel document. It's also free, btw.
Tutorial: http://www.codeproject.com/Articles/680421/Create-Read-Edit-Advance-Excel-Report-in#1
}
This reads all the columns and rows in EPPLus.
You need to retrieve the
.Value
or.Value2
property of the cell:Note the last two
if
's..Value2
will return a double for both numbers and dates, but.Value
will return a double only for numbers. The order that you check is important.Something like:
...should do what you want. Try this and if you get any errors let me know.
The value will either be a
string
,double
,DateTime
,bool
orint
value.Aside from the
int
s, these are quite self-explanatory. Theint
values represent the different error codes that are possible, e.g.#VALUE!
(Incidentally, rather than referencing the Excel PIA, I would recommend usingn NetOffice, which is available free via NuGet, to reference the Excel object model. Advantage being that you are not tied to a specific version of Excel when you deploy)