尝试读取.xlsm文件时出错

时间:2011-05-12 23:18:52

标签: c# excel-2007 office-interop

现在我有扩展名.xlsm的excel文件,我需要使用C#进行读写。 我尝试使用Microsoft.Office.Interop.Excel,

Microsoft.Office.Interop.Excel.Application ObjExcel = new Microsoft.Office.Interop.Excel.Application();
Workbook ObjWorkBook;
Worksheet ObjWorkSheet;

ObjWorkBook = ObjExcel.Workbooks.Open(excelFilename,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
ObjWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet) ObjWorkBook.Sheets[1];

if (ObjWorkSheet != null)
{
    Range usedRange = ObjWorkSheet.UsedRange;
    if (usedRange != null)
    {
        for (int rowIndex = 1; rowIndex < usedRange.Rows.Count; rowIndex++)
        {
            for (int colIndex = 1; colIndex < usedRange.Columns.Count; colIndex++)
            {
                string firstName = ((Microsoft.Office.Interop.Excel.Range)ObjWorkSheet.Cells[rowIndex, colIndex]).Value2.ToString();
            }
        }
    }
}

然而,我发现在读取单元格时没有将错误的Object引用设置为对象的实例。尽管我替换了另一个新的excel file.xlsm并且它读取时没有错误... 另外,我正在尝试阅读的文件有宏......

任何解决方案?!

提前致谢:)

0 个答案:

没有答案