Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading data from excel 2010 using Microsoft.Office.Interop.Excel

I am not able to read data in Excel. Here is the code I am using:

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"Book1.xlsx", 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;

int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;

for (int i = 1; i <= rowCount; i++)
{
    for (int j = 1; j <= colCount; j++)
    {
        MessageBox.Show(xlWorksheet.Cells[i,j].ToString());
    }
}

I get a message box that says something about System.__ComObject instead of a value.
How can I fix this?

like image 344
Coolenough Avatar asked Apr 08 '13 07:04

Coolenough


1 Answers

I found the solution for above, here is the code:

string temp = (string)(xlRange.Cells[i, j] as Excel.Range).Value2;
MessageBox.Show(temp);
like image 191
Coolenough Avatar answered Nov 15 '22 16:11

Coolenough