deleting rows from an excel file using c#

后端 未结 2 814
一向
一向 2020-12-11 06:46

I am opening an excel file like this:

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;

string str;
int rC         


        
相关标签:
2条回答
  • 2020-12-11 07:28

    Once you have a reference to the worksheet say

    for(int i = 1; i <=100; i++)
    {
      if(!worksheet.Cells[i,1].Contains("SomeString"))
      {
         ((Range)worksheet.Rows[i]).Delete(shiftDirection)
      }
    }
    

    where shiftDirection see here: Range.Delete method

    You may have to cast the Cell's content to a string.

    0 讨论(0)
  • 2020-12-11 07:29
     for (int i = dt.Rows.Count - 1; i >= 0; i--)
     {
         if (dt.Rows[i][1].ToString() == "SomeThing")
         {
             dt.Rows.RemoveAt(i);
         }
     }
    
    0 讨论(0)
提交回复
热议问题