清除所有的手动分页符:Ws.Cells.PageBreak = MSExcel.XlPageBreak.xlPageBreakNone
完毕后重新设置,参考帮助.
下面的例子就是对有内容的区域,每打印5行就换页:
Dim App As New MSExcel.Application
Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open("D:\Report.xls")
Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
'对A列从65536行开始向上查找,直到找到最后一个非空单元格为止,并得到其行号.也就是有内容的结束行
Dim EndRow As Integer = Ws.Range("A65536").End(MSExcel.XlDirection.xlUp).Row
'对A列从第1行开始向下查找,直到找到最后一个非空单元格为止,并得到其行号.也就是有内容的开始行
Dim FirstRow As Integer = Ws.Range("A1").End(MSExcel.XlDirection.xlDown).Row
'对A列从第1行开始向下查找,直到找到最后一个非空单元格为止,并得到其行号.也就是有内容的开始行
Ws.Cells.PageBreak = MSExcel.XlPageBreak.xlPageBreakNone'清除所有分页符
For i As Integer = FirstRow+6 To EndRow Step 5 '每5行就分页
Ws.Rows(i).PageBreak = MSExcel.XlPageBreak.xlPageBreakManual
Next
App.Visible = True
Ws.PrintPreview
App.Quit