【问题标题】:Print in VB.NET 2010在 VB.NET 2010 中打印
【发布时间】:2012-12-26 15:05:31
【问题描述】:

我想使用 VB.NET 2010 使用存折打印机 (Epson PLQ 20) 在存折(用于记录银行交易的纸质存折)上打印。

我目前的mysql表结构是,

1. tblLoanRegistry(LoanID pk, EMPNumber, Date, Amount, NoOfInstallments, Teller)
2. tblLoanAccount(ID pk, LoanID fk, Date, Payment, Interest, Total, Auto bool, Installment int, teller)

我的问题是:

  1. 如何检测打印的最后一行?
  2. 如何在书的正确位置(正确行)上打印第一行未打印的行。

我决定在上面提到的每个表中添加一个“打印”(布尔)字段。打印与否。我可以在 vb.net 中使用同一台打印机打印文本、数字等(例如:首页上的帐户持有人详细信息)。但是我在打印交易时遇到了上述问题。非常感谢您的帮助/意见。

更多信息: 实际上,我为非营利组织开发了一个基于网络的帐户处理系统,使用 php 和 mysql 作为我的学位项目。现在他们想像我之前描述的那样在存折上打印交易。

因此,我正在使用 VB.NET 创建一个应用程序(我对 VB.NET 完全陌生。但在 vb6 方面有经验),而我正在学习它。我设法进行了简单的打印,但这是不同的。

我没有解决上述两个问题的好主意。

更新: 我以不同的(可能是不好的)方式做到了。 打印按钮的点击事件。

    Dim sqlLoan As String

    conn = New MySqlConnection(cnString)

    sqlLoan = "SELECT tblLoanAccount.Date,if(Installment = 0, 'Interest', concat('Installment : ', Installment)) as Description, tblLoanAccount.Payment, tblLoanAccount.Interest, " &
        " tblLoanAccount.Total, tblLoanAccount.Auto, tblLoanAccount.Installment FROM tblLoanAccount join tblloanRegistry on  tblloanRegistry.LoanID = tblLoanAccount.LoanID " &
        " where(tblloanRegistry.EMPNumber = " & cmbEMPN.Text & " And tblLoanAccount.LoanID = tblLoanRegistry.LoanID) AND tblLoanAccount.Total <> 0 ORDER BY tblLoanAccount.ID"

    Using conn As New MySqlConnection(cnString)
        Using cmd As New MySqlCommand(sqlLoan, conn)
            conn.Open()
            Using myReader As MySqlDataReader = cmd.ExecuteReader()
                Using writer As StreamWriter = New StreamWriter("c:\file.txt")
                    While myReader.Read()
                        writer.WriteLine("{0}, {1}, {2}, {3}, {4}", myReader.Item(0), myReader.Item(1), myReader.Item(2), myReader.Item(3), myReader.Item(4))
                    End While
                End Using
                Call Printing()
            End Using
        End Using
    End Using

    ' Print the file. 
Public Sub Printing()
    Try
        streamToPrint = New StreamReader(("c:\file.txt"))
        Try
            printFont = New Font("Arial", 10)
            Dim pd As New PrintDocument()
            AddHandler pd.PrintPage, AddressOf pd_PrintPage
            ' Print the document.
            pd.Print()
        Finally
            streamToPrint.Close()
        End Try
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub 'Printing

其他代码为msdnPrintDocument Class

【问题讨论】:

    标签: vb.net printing


    【解决方案1】:

    这个问题有很多未知数。例如,当您将某个项目标记为已打印时,您是希望将其标记为通用(针对所有应用程序、所有事务和所有时间)还是仅在有限的上下文中(特定应用程序、事务或时间范围)?

    事务是在单个 .NET 线程中启动和完成的,还是此应用程序是多线程的,还是事务跨越多个独立执行?您是否需要打印超出实际纸张的项目的一些记录?

    假设您希望仅在特定事务中打印项目的记录,您可能需要创建第三个表,称为 tblPrintTransaction,其中包含主键或其他标识符列以及您想要的任何其他列(事务开始日期、结束日期、用户 ID、上下文信息等)。当您启动应用程序时,在此表中创建一个新行并获取行 ID。

    现在,创建第四个表,称为 tblPrintTransactionArtifact,至少包含两列。一列将是标识事务的外键(来自 tblPrintTransaction 表),一列或多列将用于标识已打印的项目。例如,您的表可以包含两列来标识打印的项目:一列指定“注册表”或“帐户”,另一列指定项目的 ID。

    当然,所有这些信息都可以在应用程序本身中创建和维护(使用变量等),但是将它们存储在一个表中意味着它们将在应用程序执行之后持续存在,从而为您提供永久记录。我建议您在应用程序中跟踪打印页面上的当前“行”,因为我认为在您的数据库中这几乎没有用处。

    【讨论】:

    • 我决定使用临时表来临时存储交易记录直到打印。 vb.net 应用程序连接到这个临时表并打印数据(例如:所有未打印的与 EMPNumber 1000 相关的事务记录是最新的。对于下一个成员,它会从临时表中删除所有记录并插入新记录以进行打印)。这些记录由 php mysql 系统和 vb.net 应用程序生成,用于打印它们。
    • 我能够打印出我想要的东西。但我觉得这是一种肮脏的方式。请有任何意见。
    • 我认为将查询保存到临时文本文件,然后打印文本文件是个好主意。如果它对你有用,那么我建议坚持下去。
    • 谢谢。我认为在 vb.net 中打印比在 vb 6 中要难得多。
    【解决方案2】:

    将 sqlLoan 调暗为字符串

    conn = New MySqlConnection(cnString)
    
    sqlLoan = "SELECT tblLoanAccount.Date,if(Installment = 0, 'Interest', concat('Installment : ', Installment)) as Description, tblLoanAccount.Payment, tblLoanAccount.Interest, " &
        " tblLoanAccount.Total, tblLoanAccount.Auto, tblLoanAccount.Installment FROM tblLoanAccount join tblloanRegistry on  tblloanRegistry.LoanID = tblLoanAccount.LoanID " &
        " where(tblloanRegistry.EMPNumber = " & cmbEMPN.Text & " And tblLoanAccount.LoanID = tblLoanRegistry.LoanID) AND tblLoanAccount.Total <> 0 ORDER BY tblLoanAccount.ID"
    
    Using conn As New MySqlConnection(cnString)
        Using cmd As New MySqlCommand(sqlLoan, conn)
            conn.Open()
            Using myReader As MySqlDataReader = cmd.ExecuteReader()
                Using writer As StreamWriter = New StreamWriter("c:\file.txt")
                    While myReader.Read()
                        writer.WriteLine("{0}, {1}, {2}, {3}, {4}", myReader.Item(0), myReader.Item(1), myReader.Item(2), myReader.Item(3), myReader.Item(4))
                    End While
                End Using
                Call Printing()
            End Using
        End Using
    End Using
    
        ' Print the file. 
    Public Sub Printing()
        Try
            streamToPrint = New StreamReader("c:\file.txt")
            Try
                printFont = New Font("Arial", 10)
                Dim pd As New PrintDocument()
                AddHandler pd.PrintPage, AddressOf pd_PrintPage
                ' Print the document.
                pd.Print()
            Finally
                streamToPrint.Close()
            End Try
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub 'Printing
    

    其他代码为msdn PrintDocument 类。

    【讨论】:

      【解决方案3】:

      您可以尝试使用我在 MySql 和 VB.Net 中使用的易于使用、轻量级的报告编写器http://www.jcl.vdtec.net

      【讨论】:

      • 单独的链接被认为是一个糟糕的答案(请参阅faq),因为它本身没有意义,并且不能保证目标资源在未来仍然存在It would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2014-01-27
      • 2017-11-26
      • 1970-01-01
      • 2012-09-07
      相关资源
      最近更新 更多