类型' System.Runtime.InteropServices.COMException'未处理的异常尝试在多张Excel中获取数据

时间:2016-12-19 09:56:42

标签: c# excel interop

我正在尝试在多张Excel中获取数据。现在我得到的代码我得到了错误:unhandled exception of type 'System.Runtime.InteropServices.COMException occurred in mscorlib.dll' Additional information: Select method of Range class failed。我在第二次在for循环中运行时得到错误。

        Microsoft.Office.Interop.Excel.Application xlexcel;
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;
        xlexcel = new Microsoft.Office.Interop.Excel.Application();
        xlexcel.Visible = true;
        xlWorkBook = xlexcel.Workbooks.Add();
        for (int i = 0; i < comboBox1.Items.Count; i++)
        {
            xlWorkBook.Sheets.Add();
        }
        for (int i = 0; i < comboBox1.Items.Count; i++)
        {
            copyAlltoClipboard(); //this copies data to the clipboard
            xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(i+1);
            Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
            CR.Select(); //error is here
            xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
        }

有谁知道为什么这段代码无效?

0 个答案:

没有答案