Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.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
C#互操作-遍历行并删除_C#_Excel_Interop_Excel Interop_Vba - Fatal编程技术网

C#互操作-遍历行并删除

C#互操作-遍历行并删除,c#,excel,interop,excel-interop,vba,C#,Excel,Interop,Excel Interop,Vba,我正在使用Excel Interop处理一些Excel工作表。在工作表中,我需要遍历行,如果行中的第一个单元格为空,则需要删除整行。我尝试了以下方法- Excel.Range excelRange = sheet.UsedRange; foreach (Excel.Range row in excelRange.Rows) { String a = ((Excel.Range)row.Cells[Type.Missing, 1]).Value2 as String; if (a == nul

我正在使用Excel Interop处理一些Excel工作表。在工作表中,我需要遍历行,如果行中的第一个单元格为空,则需要删除整行。我尝试了以下方法-

Excel.Range excelRange = sheet.UsedRange;
foreach (Excel.Range row in excelRange.Rows)
{
 String a = ((Excel.Range)row.Cells[Type.Missing, 1]).Value2 as String;
 if (a == null || a == "")
 {
  ((Excel.Range)row).Delete(Type.Missing);
 }
}
这根本不起作用。有什么不同的方法可以做到这一点吗


还有,有没有快速的方法来查找和删除工作表中的所有公式?

假设您的Excel文件如下所示

删除行的最佳方法是使用Excel的内置功能Autofilter,该功能将过滤A列中的空白值,然后一次性删除整行

经过尝试和测试(VS 2010 Ultimate)

注意:我更改了几行,我觉得在VS 2008中可能会出错。因为我没有VS2008,所以我无法在那里测试它。如果你有任何错误,一定要让我知道,我会纠正它

//~~> Open File
private void button1_Click(object sender, EventArgs e)
{

    Microsoft.Office.Interop.Excel.Application xlexcel;
    Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
    Microsoft.Office.Interop.Excel.Range xlRange;
    Microsoft.Office.Interop.Excel.Range xlFilteredRange;

    xlexcel = new Excel.Application();

    xlexcel.Visible = true;

    //~~>  Open a File
    xlWorkBook = xlexcel.Workbooks.Open("C:\\MyFile.xlsx",  0,  false, 5, "", "", true,
    Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

    //~~> Work with Sheet1
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

    //~~> Get last row in Col A
    int _lastRow = xlWorkSheet.Cells.Find(
                                  "*",
                                  xlWorkSheet.Cells[1,1],
                                  Excel.XlFindLookIn.xlFormulas,
                                  Excel.XlLookAt.xlPart,
                                  Excel.XlSearchOrder.xlByRows,
                                  Excel.XlSearchDirection.xlPrevious,
                                  misValue,
                                  misValue,
                                  misValue
                                  ).Row ;

    //~~> Set your range
    xlRange =  xlWorkSheet.Range["A1:A" + _lastRow];

    //~~> Remove any filters if there are
    xlWorkSheet.AutoFilterMode=false;

    //~~> Filter Col A for blank values
    xlRange.AutoFilter(1, "=", Excel.XlAutoFilterOperator.xlAnd, misValue, true);

    //~~> Identigy the range
    xlFilteredRange = xlRange.Offset[1,0].SpecialCells(Excel.XlCellType.xlCellTypeVisible,misValue);

    //~~> Delete the range in one go
    xlFilteredRange.EntireRow.Delete(Excel.XlDirection.xlUp);

    //~~> Remove filters
    xlWorkSheet.AutoFilterMode = false;

    //~~> Close and cleanup
    xlWorkBook.Close(true, misValue, misValue);
    xlexcel.Quit();

    releaseObject(xlRange);
    releaseObject(xlFilteredRange);    
    releaseObject(xlWorkSheet);
    releaseObject(xlWorkBook);
    releaseObject(xlexcel);
}

private void releaseObject(object obj)
{
    try
    {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        obj = null;
    }
    catch (Exception ex)
    {
        obj = null;
        MessageBox.Show("Unable to release the Object " + ex.ToString());
    }
    finally
    {
        GC.Collect();
    }
}
输出


第一件事第一。。。你用的是哪个VS版本?@SiddharthRout-我用的是VS2008.Ok。我有VS 2010,我将要给出的代码可能在2008年无法使用。此代码与VS 08不兼容。我们试图获取属性“End”的第11行(//~~~>Get last row in Col A)不可用。已更新帖子。现在试试看/