打印时发送额外的空白纸张!为什么?

时间:2014-01-11 17:34:54

标签: vb.net excel sqlite printing

在VB.net中有这个算法来操作和打印excel文档,但每次打印我都要在我要打印的文档之前得到一张额外的纸张! excel表上没有额外的页面或类似的东西!任何人都可以在这段代码中找出原因,或者有人知道为什么吗?感谢

Sub Cmd_PrintClick(sender As Object, e As EventArgs)

    'print 1 page
    printDocument.PrinterSettings.Copies = 1
    'print document (see Sub PrintDocumentPrintPage below ...)
    printDocument.Print

End Sub

Sub PrintDocumentPrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs)

    Dim oApp As New Excel.Application
    Dim oWB As Excel.Workbook = oApp.Workbooks.Add()
    Dim oWS As Excel.Worksheet = CType(oWB.Worksheets(1), Excel.Worksheet)

    Dim oRng1 As Excel.Range
    Dim search As String = ""

    SQLcommand = SQLconnect.CreateCommand
    'Create SQL statement
    SQLcommand.CommandText = "SELECT * FROM Bill_Record WHERE Paid = 'F'"
    'Extract data
    SQLreader = SQLcommand.ExecuteReader()

    While SQLreader.Read()

    search = "" 
    search = SQLreader("Pupil_ID")

    oWB = oApp.Workbooks.Open("F:\Kids Club Database\Kids Club Database v2\Backup\ContactsDatabase\bin\Debug\Bill.xlsx")
    oWS = oWB.Worksheets("Sheet1")

    oRng1 = oWS.Range("F34")
    oRng1.Value = Microsoft.VisualBasic.Left(SQLreader("Payment_Due_Date"),10)

    oRng1 = oWS.Range("A19")
    oRng1.Value =  Microsoft.VisualBasic.Left(SQLreader("Bill_Date"),10)

    oRng1 = oWS.Range("A13")
    oRng1.Value = SQLreader("Term")

    oRng1 = oWS.Range("C47")
    oRng1.Value = SQLreader("Term")

    oRng1 = oWS.Range("H28")
    oRng1.Value = SQLreader("Total_Term_Cost")

    oRng1 = oWS.Range("C48")
    oRng1.Value = SQLreader("Total_Term_Cost")

    oRng1 = oWS.Range("F22")
    oRng1.Value = SQLreader("Total_Sessions_Monday")

    oRng1 = oWS.Range("H22")
    oRng1.Value = SQLreader("Total_Monday_Cost")

    oRng1 = oWS.Range("F23")
    oRng1.Value = SQLreader("Total_Sessions_Tuesday")

    oRng1 = oWS.Range("H23")
    oRng1.Value = SQLreader("Total_Tuesday_Cost")

    oRng1 = oWS.Range("F24")
    oRng1.Value = SQLreader("Total_Sessions_Wednesday")

    oRng1 = oWS.Range("H24")
    oRng1.Value = SQLreader("Total_Wednesday_Cost")

    oRng1 = oWS.Range("F25")
    oRng1.Value = SQLreader("Total_Sessions_Thursday")

    oRng1 = oWS.Range("H25")
    oRng1.Value = SQLreader("Total_Thursday_Cost")

    oRng1 = oWS.Range("F26")
    oRng1.Value = SQLreader("Total_Sessions_Friday")

    oRng1 = oWS.Range("H26")
    oRng1.Value = SQLreader("Total_Friday_Cost")



    'Clear SQL command buffer
    SQLcommand.Dispose()

    SQLcommand = SQLconnect.CreateCommand
    'Create SQL statement
    SQLcommand.CommandText = "SELECT Pupil_Name, Pupil_Surname, Form_ID FROM Pupil WHERE Pupil_ID = '" & search &"'"
    'Extract data
    SQLreader = SQLcommand.ExecuteReader()

    oRng1 = oWS.Range("A16")
    oRng1.Value = SQLreader("Pupil_Name") & " " & SQLreader("Pupil_Surname")

    oRng1 = oWS.Range("C46")
    oRng1.Value = SQLreader("Pupil_Name") & " " & SQLreader("Pupil_Surname")

    oRng1 = oWS.Range("A17")
    oRng1.Value = SQLreader("Form_ID")

    'Clear SQL command buffer
    SQLcommand.Dispose()

    SQLcommand = SQLconnect.CreateCommand
    'Create SQL statement
    SQLcommand.CommandText = "SELECT * FROM Price"
    'Extract data
    SQLreader = SQLcommand.ExecuteReader()

    oRng1 = oWS.Range("D22")
    oRng1.Value = SQLreader("Price_Monday")

    oRng1 = oWS.Range("D23")
    oRng1.Value = SQLreader("Price_Tuesday")

    oRng1 = oWS.Range("D24")
    oRng1.Value = SQLreader("Price_Wednesday")

    oRng1 = oWS.Range("D25")
    oRng1.Value = SQLreader("Price_Thursday")

    oRng1 = oWS.Range("D26")
    oRng1.Value = SQLreader("Price_Friday")

    'Clear SQL command buffer
    SQLcommand.Dispose()

    oRng1 = Nothing   ' <-- Don't forget!
    oWB.PrintOut()
    oWB.Close()
    oWB = Nothing

    oWS = Nothing
    oApp.Quit()
    oApp = Nothing

    End While

    End Sub

2 个答案:

答案 0 :(得分:1)

尝试更改:

oWB.PrintOut()

要:

oWB.PrintOut(From:=1, [To]:=1, Copies:=1, Collate:=False)

修改

另外,请务必设置所需的PageSetup(表格):

With oWS.PageSetup
    .Zoom = False
    .PaperSize = XlPaperSize.xlPaperA4
    .Orientation = XlPageOrientation.xlPortrait
    .FitToPagesTall = 1
    .FitToPagesWide = 1
End With

修改2

那为什么要增加一个空白页?就这么简单。您已将excel互操作代码放在PrintPageEvent的{​​{1}}内。因此,额外页面由PrintDocument打印。

PrintDocument

或者将所有内容移动到单独的子站并在需要时调用:

Sub PrintDocumentPrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs)
    e.Cancel = True '< Do not print
    'YOUR CODE...
End Sub

答案 1 :(得分:-1)

Dim oWS As Excel.Worksheet = CType(oWB.Worksheets(1),Excel.Worksheet)

我认为应该是:

Dim oWS As Excel.Worksheet = CType(oWB.Worksheets(0),Excel.Worksheet)

编辑1:

怎么样:

Dim oWS As Excel.Worksheet = oWB.Worksheets.Add(New Excel.Worksheet)

我认为发生的情况是您在工作表位置1而不是工作表位置0添加工作表,因此第一个工作表(空白工作表)首先打印,然后打印您正在尝试使用的工作表。

编辑2:

oWB = oApp.Workbooks.Open("F:\Kids Club Database\Kids Club Database v2\Backup\ContactsDatabase\bin\Debug\Bill.xlsx")
oWS = oWB.Worksheets("Sheet1")

oRng1 = oWS.Range("F34") ' Put a break point on this line and check the oWB.Worksheets collection and see how many worksheets there are in the collection and make sure the first one is named "Sheet 1"