从Excel 2010中使用的Microsoft.Office.Interop.Excel读取数据 [英] Reading data from excel 2010 using Microsoft.Office.Interop.Excel

查看:1042
本文介绍了从Excel 2010中使用的Microsoft.Office.Interop.Excel读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能够在Excel中读取数据。这里是code我使用的:

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

我得到了一些关于系统.__ ComObject一个消息框,,而不是一个值。
我该如何解决这个问题?

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

推荐答案

我发现上面的解决方案,这里是code:

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

string temp = (string)(xlRange.Cells[i, j] as Excel.Range).Value2;
MessageBox.Show(temp);

这篇关于从Excel 2010中使用的Microsoft.Office.Interop.Excel读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆