在Excel工作表中删除空行

时间:2014-03-14 06:19:03

标签: asp.net vb.net 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")

但是我收到了语法错误。有人知道要执行此操作所需的命名空间吗?

2 个答案:

答案 0 :(得分:0)

尝试

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

包含以下命名空间

using Excel = Microsoft.Office.Interop.Excel;

答案 1 :(得分:0)

试试这个 - 添加引用(从COM部分)Microsoft 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