2017-06-23 194 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 .. :-) –