Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Microsoft.Office.Interop.Excel.Worksheet不包含';InsertRow';_Excel_Winforms_C# 4.0 - Fatal编程技术网

Microsoft.Office.Interop.Excel.Worksheet不包含';InsertRow';

Microsoft.Office.Interop.Excel.Worksheet不包含';InsertRow';,excel,winforms,c#-4.0,Excel,Winforms,C# 4.0,我想在excel工作表中动态插入几行,但当我尝试使用下面的代码时,正如stack over flow中的许多答案中所建议的那样,但它抛出了一个错误 //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 'Ins

我想在excel工作表中动态插入几行,但当我尝试使用下面的代码时,正如stack over flow中的许多答案中所建议的那样,但它抛出了一个错误

//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();
        }

   }
private void Insert\u Empty\u Rows()
{
Excel.Application Excel_App=新的Microsoft.Office.Interop.Excel.Application();
Excel.Workbook MyWorkBook=Excel\u App.Workbooks.Open(textBox1.Text.ToString(),类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少);
Excel.Worksheet MyWorksheet=null;
MyWorksheet=(Microsoft.Office.Interop.Excel.Worksheet)MyWorkBook.Sheets[(1)];
Excel.Range Excel\u Range;
Excel\u Range=MyWorksheet.UsedRange;
对于(变量x=0;x<10;x++)
{
((Excel.Range)MyWorksheet.Rows[2]).Insert();
}
}

仍然会出现错误“方法'this'没有重载接受'1'参数”

类似的情况应该可以解决问题,例如,这里建议:

for(变量x=0;x<10;x++)
{
((范围)工作表.Rows[2]).Insert();
}
私有无效插入\u空\u行()
{
Excel.Application Excel_App=新的Microsoft.Office.Interop.Excel.Application();
Excel.Workbook MyWorkBook=Excel\u App.Workbooks.Open(textBox1.Text.ToString(),类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少,类型.缺少);
Excel.Worksheet MyWorksheet=null;
MyWorksheet=(Microsoft.Office.Interop.Excel.Worksheet)MyWorkBook.Sheets[(1)];
Excel.Range Excel\u Range;
Excel\u Range=MyWorksheet.UsedRange;
对于(变量x=0;x<10;x++)
{
((Excel.Range)MyWorksheet.Rows[2,Type.Missing]).Insert(Excel.XlInsertShiftDirection.xlShiftDown,Type.Missing);
}
SaveFileDialog SaveFileDialog1=新建SaveFileDialog();
SaveFileDialog1.Title=“浏览以保存Excel文件”;
SaveFileDialog1.DefaultExt=“xls”;
SaveFileDialog1.Filter=“Excel文件(*.xls或*.xlsx)|*.xls;*.xlsx |所有文件(*.*)|*.”;
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);
MyWorkBook.Close(true、misValue、misValue);
Excel_App.Quit();
}
Excel_App.Quit();
MessageBox.Show(“SOTP更新成功”);
}

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

我厌倦了这段代码,但它抛出了错误“没有重载方法”,此方法接受“1”参数。只需在此处尝试,它就可以完美地工作。。。您使用的是什么版本的interops?我使用的是12.0.0.0这与我使用的版本相同。您必须发布您的代码您是否使用了旧版本的visual studio?我看到您正在为.Open方法中的所有可选参数提供Type.Missing。如果是这样,请尝试为Insert方法提供两个可选参数
.Insert(XlInsertShiftDirection.xlShiftDown,Type.Missing)
        for (var x = 0; x < 10; x++)
        {
            ((Range) worksheet.Rows[2]).Insert();
        }
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");

   }