Microsoft.Office.Interop.Excel.Worksheet不包含' InsertRow'的定义。

时间:2016-02-03 06:32:34

标签: excel winforms c#-4.0

我想在excel表中动态插入几行,但是当我尝试使用下面的代码时,如堆栈中的许多答案中所建议的那样,但是它会抛出一个错误。

//Inserting 10 rows into the worksheet starting from 3rd row

 worksheet.Cells.InsertRows(2,10);

Microsoft.Office.Interop.Excel.Worksheet doesnot contain a definition for 'InsertRow' 
and no extension method 'InsertRow' acceping a 1st argument of type 'Microsoft.Office.Interop.Excel.Worksheet' 

任何人都可以帮助我...

根据以下建议,我尝试更新我的代码

    private void Insert_Empty_Rows()
    {
        Excel.Application Excel_App = new Microsoft.Office.Interop.Excel.Application();
        Excel.Workbook MyWorkBook = Excel_App.Workbooks.Open(textBox1.Text.ToString(), 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);
        Excel.Worksheet MyWorksheet = null;
        MyWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)MyWorkBook.Sheets[(1)];
        Excel.Range Excel_Range;
        Excel_Range = MyWorksheet.UsedRange;
        for (var x = 0; x < 10; x++)
        {
            ((Excel.Range)MyWorksheet.Rows[2]).Insert();
        }

   }

仍然出现错误&#34;没有超载的方法&#39;这个&#39;需要&#39; 1&#39;参数&#34;

2 个答案:

答案 0 :(得分:0)

像这样的东西应该可以解决问题,例如: Excel insert rows (not Add)

        for (var x = 0; x < 10; x++)
        {
            ((Range) worksheet.Rows[2]).Insert();
        }

答案 1 :(得分:0)

private void Insert_Empty_Rows()
    {
        Excel.Application Excel_App = new Microsoft.Office.Interop.Excel.Application();
        Excel.Workbook MyWorkBook = Excel_App.Workbooks.Open(textBox1.Text.ToString(), 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);
        Excel.Worksheet MyWorksheet = null;
        MyWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)MyWorkBook.Sheets[(1)];
        Excel.Range Excel_Range;
        Excel_Range = MyWorksheet.UsedRange;
        for (var x = 0; x < 10; x++)
        {
            ((Excel.Range)MyWorksheet.Rows[2,Type.Missing]).Insert(Excel.XlInsertShiftDirection.xlShiftDown, Type.Missing);
        }
        SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
        SaveFileDialog1.Title = "Browse to Save Excel Files";
        SaveFileDialog1.DefaultExt = "xls";
        SaveFileDialog1.Filter = "Excel files (*.xls or *.xlsx)|*.xls;*.xlsx|All files (*.*)|*.*";
        SaveFileDialog1.FilterIndex = 2;
        if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            Excel_App.DisplayAlerts = false;
            MyWorkBook.SaveAs(SaveFileDialog1.FileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            MyWorkBook.Close(true, misValue, misValue);
            Excel_App.Quit();
        }
        Excel_App.Quit();
        MessageBox.Show("SOTP Updated Successfully");

   }

如果有人有比上面提到的解决方案更好的解决方案,请发布它......因为这段代码对我有用......