2、安装串口程序,这个打印机是通过串口传输数据的。
3、安装USB转串口线的驱动程序。
4、通过excel的vba 开发自动计算功能及 批量打印算法。
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub print_onlyone()
Dim MSComm1
Set MSComm1 = CreateObject("MSCOMMLib.MSComm") '首先创建一对象
MSComm1.CommPort = CInt(Sheet2.Cells(1, 2)) '占用的串口号,1表示COM1
MSComm1.Settings = "9600,N,8,1" '这个自己根据自己的情况设置
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeText
MSComm1.RTSEnable = True
MSComm1.InBufferCount = 0
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True '这句话放到连接按钮事件中去
End If
Dim tmp As String
'开始打印
MSComm1.Output = Chr$(27) & Chr$(64) '重新初始化打印机
MSComm1.Output = Chr$(27) & Chr$(49) & Chr$(5) ' 没年月日的行距 5 line
'一行 16个字符
MSComm1.Output = Space16(Sheet2.Cells(2, 2)) ' 向端口发送指令 电话:
MSComm1.Output = Convert(Sheet2.Cells(2, 2))
MSComm1.Output = Chr$(13)
MSComm1.Output = Space16(Sheet2.Cells(3, 2)) ' 向端口发送指令 车号:
MSComm1.Output = Convert(Sheet2.Cells(3, 2))
MSComm1.Output = Chr$(13)
MSComm1.Output = Space16(Sheet2.Cells(4, 2)) ' 向端口发送指令 证号:
MSComm1.Output = Convert(Sheet2.Cells(4, 2))
MSComm1.Output = Chr$(13)
MSComm1.Output = Space16(Sheet2.Cells(5, 2)) ' 向端口发送指令 日期:
MSComm1.Output = Convert(Sheet2.Cells(5, 2))
MSComm1.Output = Chr$(13)
MSComm1.Output = Space16(Sheet2.Cells(6, 2)) ' 向端口发送指令 上车时间:
MSComm1.Output = Convert(Sheet2.Cells(6, 2))
MSComm1.Output = Chr$(13)
MSComm1.Output = Space16(Sheet2.Cells(7, 2)) ' 向端口发送指令 下车时间:
MSComm1.Output = Convert(Sheet2.Cells(7, 2))
MSComm1.Output = Chr$(13)
'''''''''''''''''''''''''''''''''''''''''''''''''''''
MSComm1.Output = Space16(Sheet2.Cells(8, 2)) '单价
MSComm1.Output = Convert(Sheet2.Cells(8, 2))
MSComm1.Output = Chr$(13) ''换行
MSComm1.Output = Space16(Sheet2.Cells(9, 2)) '向端口发送指令 里程
MSComm1.Output = Convert(Sheet2.Cells(9, 2))
MSComm1.Output = Chr$(13)
MSComm1.Output = Space16(Sheet2.Cells(10, 2)) '向端口发送指令 时间 00:09:39
MSComm1.Output = Convert(Sheet2.Cells(10, 2))
MSComm1.Output = Chr$(13)
MSComm1.Output = Space16(Sheet2.Cells(11, 2)) '打印总额
MSComm1.Output = Convert(Sheet2.Cells(11, 2))
MSComm1.Output = Chr$(13)
MSComm1.Output = Space16(Sheet2.Cells(12, 2))
MSComm1.Output = Convert(Sheet2.Cells(12, 2))
MSComm1.Output = Chr$(13)
MSComm1.Output = Space16(Sheet2.Cells(12, 2))
MSComm1.Output = Convert(Sheet2.Cells(12, 2))
MSComm1.Output = Chr$(13)
'打印完,跳过21行不打印
For i = 1 To 15
MSComm1.Output = Chr$(13)
Next i
If MSComm1.PortOpen = True Then '如果串口被打开
MSComm1.PortOpen = False '关闭按钮
End If
End Sub
。。。。。。。。
其他略去,自己研究研究
开发效果图如下:
1、VBA 代码截图
2、自动计算,自动匹配信息库,批量打印界面
3、信息库保存真实有效的信息