我正在尝试在多张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);
}
有谁知道为什么这段代码无效?