2013-10-16 56 views
2

我正在使用Excel Interop來處理一些Excel表。在工作表中,我需要遍歷行,如果行中的第一個單元格爲空,那麼我需要刪除整行iteself。我試過以下 -C#互操作 - 遍歷行並刪除

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

首先第一件事情......這VS版本您使用的? –

+0

@SiddharthRout - 我正在使用VS 2008. – user2531191

+0

好的。我有VS 2010和我將要提供的代碼在2008年可能無法工作。 –

回答

3

比方說,你的Excel文件看起來像這樣。

enter image description here

刪除行是使用Excel的所謂自動篩選功能內置其中將過濾柱A空白值,然後一氣呵成刪除整行的最好方法。

嘗試和測試(2010年VS旗艦版)

注意:我已經改變了幾行字,我覺得可以在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

+0

此代碼與VS 08不兼容。行號11(// ~~>獲取列A中的最後一行)我們嘗試得到一個屬性「結束」不可用。 – user2531191

+0

更新了帖子。現在嘗試一下/ –