Excel COM in C# Visual Studio

本文关键字:Visual Studio in COM Excel | 更新日期: 2023-09-27 18:20:43

所以我在visual studio 2013中工作,并试图返回工作表中给定单元格中的字符串。

    public StringBuilder MyCellData(int rCnt, int cCnt)
    {
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        Excel.Range range;
        StringBuilder str = new StringBuilder("");
        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open(@"C:'Users'james.naughton'Documents'Visual Studio 2013'Projects'QA_Automation'QA_Automation'copyChangeReportWAccountID.xlsx", 0, true, 5, "Landeck1", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "'t", false, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets.get_Item(1);

        range = xlWorkSheet.UsedRange[rCnt,cCnt];
        str.Append((range.Item[rCnt, cCnt]).ToString());
   return str;

现在,每当我出于某种原因运行程序时,range和xlWorkSheet的值总是COM对象,所以当它没有返回我想要的值时。关于如何访问正确的工作表并从单元格返回数据,有什么建议吗?

谢谢,James

Excel COM in C# Visual Studio

您需要获取单元格的值:

str.Append((range.Item[rCnt, cCnt]).Value.ToString());