2017-06-23 195 views
0

我可以在我的工作簿中,在c#windows應用程序中刪除我的工作表。 但如何刪除我的Excel表格選擇與C#Windows行?刪除Excel行

 xlApp.DisplayAlerts = false; 
     string filePath = @"d:\test.xlsx"; 
     Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); 
     Excel.Sheets worksheets = xlWorkBook.Worksheets; 
     worksheets[1].Delete(); 
     xlWorkBook.Save(); 
     xlWorkBook.Close(); 

     releaseObject(worksheets); 
     releaseObject(xlWorkBook); 
     releaseObject(xlApp); 

     MessageBox.Show("Worksheet Deleted!"); 


It will usefull to bets outputs: 

回答

0

一旦你到工作表的引用說

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

其中shiftDirection在這裏看到:Range.Delete method

您可能需要在單元格的內容轉換爲字符串。

0

我期待你想刪除C#在Excel中的行

如果您使用的是Microsoft Interlop服務讀寫Excel文件.... 然後, 這是一個過程,其中你會必須分配(您想刪除行)的範圍內

Excel.Range range = worksheets.get_Range("A3","A20".ToString()); 
//setting the range for deleting the rows 

range.EntireRow.Delete(Excel.XlDirection.xlUp); 
//for deleting the rows; in case of columns use: 'EntireColumn' instead of 'EntireRow' 

xlWorkBook.Save(); 
//save the workbook 

xlWorkBook.Close(false, "", false); 
//close the workbook 

xlApp.Quit(); 
//quit the workbook 

希望它可以幫助!

+0

謝謝兄弟....我會幫助我... –

+0

高興地幫助:-),同時增加投票或選擇打勾我的答案,如果它適合您的問題。 – rshah4u

+0

好吧兄弟....有一個很好的方式.. TQ .. :-) –