[英]VB.NET open and print an excel file
我正在为一些印刷业务开发一个简单的VB.NET桌面应用程序。 它具有一个主要的WinForm,用于打开JPG / PDF / Word / Excel文件,打开关联程序,打印文件,捕获假脱机作业并最终根据打印机名称,页数向用户收费的按钮。 ,页面大小和每页成本。 没什么大不了。 主机具有Win7 OS。
然后,当用户想要打印XLS文件时,我希望该应用程序打开Excel 2010,打开先前通过文件对话框选择的文件。 当打开Excel时,直接转到打印对话框,然后当作业完成加载到后台打印程序时,我捕获了该事件并杀死Excel进程。
我的问题是:
我无法直接打开打印对话框打开Excel。 Excel会响应“打印”动词。 但是它仅使用默认打印机进行打印。 我希望它打开并转到打印对话框。 我不想只使用默认打印机进行打印,我确实需要允许用户选择所需的打印机,页面,副本等。
我正在尝试使用以下代码执行此操作:
Dim openFileDialog1 As New OpenFileDialog()
Dim filePath As String = ""
Dim startInfo As ProcessStartInfo
'openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "XLS Files (*.xls)|*.xls|XLSX Files (*.xlsx)|*.xlsx|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True
If (DialogResult.OK) = openFileDialog1.ShowDialog(Me) Then
filePath = openFileDialog1.FileName
Else
Exit Sub
End If
startInfo = New ProcessStartinfo(rutaArchivo)
With startInfo
.FileName = filePath
.WindowStyle = ProcessWindowStyle.Normal
.Verb = "print"
.CreateNoWindow = False
.UseShellExecute = True
End With
Try
System.Diagnostics.Process.Start(startInfo)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
IDE是SharpDevelop 4.3。 框架是.NET 4.0客户端配置文件。 操作系统是Win7。
非常感谢 :)
MS Excel仅具有以下命令行开关: Excel开关
若要做您想做的事,您可能会考虑使用Excel Interop
您应该能够做到这一点,首先打开打印对话框,然后使用带有动词“ PrintTo”的所选打印机,如下所示:
Dim openFileDialog1 As New OpenFileDialog()
Dim filePath As String = ""
Dim startInfo As ProcessStartInfo
'openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "XLS Files (*.xls)|*.xls|XLSX Files (*.xlsx)|*.xlsx|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True
If (DialogResult.OK) = openFileDialog1.ShowDialog(Me) Then
filePath = openFileDialog1.FileName
Else
Exit Sub
End If
Dim printer As String = ""
Dim printDialog As New PrintDialog()
If printDialog.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
printer = printDialog.PrinterSettings.PrinterName
End If
startInfo = New ProcessStartInfo(filePath)
With startInfo
.WindowStyle = ProcessWindowStyle.Normal
.Verb = "PrintTo"
'.Arguments = """\\" & System.Net.Dns.GetHostName() & "\" & printer & """"
.Arguments = """" & printer & """"
.CreateNoWindow = False
.UseShellExecute = True
End With
Try
System.Diagnostics.Process.Start(startInfo)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
您可能必须在“参数”(注释行)中包括服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.