2

我正在使用 Excel 互操作来处理一些 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);
 }
}

这根本不起作用。有什么不同的方法可以做到这一点吗?

而且,有没有什么快速的方法可以在工作表中查找和删除所有公式?

4

1 回答 1

3

假设您的 Excel 文件如下所示。

在此处输入图像描述

删除行的最佳方法是使用 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();
    }
}

输出

在此处输入图像描述

于 2013-10-16T05:26:38.420 回答