在Excel工作表中删除空白行

我试图使用代码从Excel中删除空行

Dim wb As New Workbook("d:\test\book1.xls") Dim sheets As WorksheetCollection = wb.Worksheets Dim sheet As Worksheet = sheets(0) sheet.Cells.DeleteBlankRows() wb.Save("d:\test\mybook.xls") 

但是我得到的语法错误。任何人都知道命名空间要求做到这一点?

尝试

 ApplicationClass excel = new ApplicationClass(); Microsoft.Office.Interop.Excel.Range cellToBeDeleted = (Range)excel.Cells[rowIndex, columnIndex]; cellToBeDeleted.Delete(); 

包含以下命名空间

 using Excel = Microsoft.Office.Interop.Excel; 

试试这个 – 添加引用(从COM部分)Micsrosoft Excel对象库到您的项目

  Imports Microsoft.Office.Interop.Excel Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click delBlankRows("d:\test\book1.xls", 1) End Sub Private Sub delBlankRows(ByVal excelFileName As String, sheetIndex As Integer) Dim excel As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application Dim fileName = excelFileName Dim w As Workbook = excel.Workbooks.Open(fileName) Dim r As Range = w.Worksheets(sheetIndex).UsedRange.EntireRow.SpecialCells(XlCellType.xlCellTypeBlanks) r.Delete() w.Save() w.Close() End Sub End Class