2014-03-14 76 views
2

我试图删除从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") 

但我正在逐渐语法error.Any人知道需要选用命名空间来做到这一点?

回答

0

尝试

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

包括以下命名空间

using Excel = Microsoft.Office.Interop.Excel; 
+0

我需要删除所有空行。如果行中有值的任何单元格,那么我需要保留该行 – Cronaldo

0

尝试这个 - 添加参考(从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