C#Interop - 遍历行和删除

时间:2013-10-16 04:19:24

标签: c# excel-vba 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 == null || a == "")
 {
  ((Excel.Range)row).Delete(Type.Missing);
 }
}

这根本不起作用。有没有不同的方法来做到这一点?

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

1 个答案:

答案 0 :(得分:3)

假设你的Excel文件是这样的。

enter image description here

删除行的最佳方法是使用Excel的内置功能Autofilter,它会过滤Col A以获取空白值,然后一次性删除整行。

经过测试和测试(在VS 2010 Ultimate中)

注意:我已经更改了几行,我认为在VS 2008中可能会出错。由于我没有VS 2008,我无法在那里测试。如果您有任何错误,请告诉我,我会纠正它。

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

<强>输出

enter image description here