VB.NET 打开并打印一个excel文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18989392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET open and print an excel file
提问by Leandro Alsina
I am developing a simple VB.NET desktop app for a little printing business. It has a main WinForm, buttons for opening JPG/PDF/Word/Excel files, open associated program, print the file, capture the spool job, and finally charge the user for what is being printed, based on printer name, number of pages, size of pages and cost per page. No big deal. Hosts machines have Win7 OS.
我正在为小型印刷业务开发一个简单的 VB.NET 桌面应用程序。它有一个主要的 WinForm,用于打开 JPG/PDF/Word/Excel 文件、打开相关程序、打印文件、捕获假脱机作业、最后根据打印机名称、页数向用户收取打印费用的按钮,页面大小和每页成本。没什么大不了。主机有 Win7 操作系统。
When then user wants to print a XLS file, I Want the app to open Excel 2010, opening a file previously selected with a file dialog. And when Excel opens, go directly to the print dialog, then when the job finishes to load in the spool, I capture that event and KILL the Excel process.
当用户想要打印 XLS 文件时,我希望应用程序打开 Excel 2010,打开以前使用文件对话框选择的文件。当 Excel 打开时,直接转到打印对话框,然后当作业完成加载到后台处理时,我捕获该事件并终止 Excel 进程。
My problem is:
我的问题是:
I cant open Excel directly going to the print dialog. Excel DOES respond to the "print" verb. But it just prints with default printer. I want it to open and go to the print dialog. I do not want to just print with default printer, I do need allow the user to select desired printer, pages, copies, etc.
我无法直接打开 Excel 进入打印对话框。Excel 确实响应“打印”动词。但它只是使用默认打印机打印。我希望它打开并转到打印对话框。我不想只使用默认打印机打印,我确实需要允许用户选择所需的打印机、页面、副本等。
I'm trying to do this with the following code:
我正在尝试使用以下代码执行此操作:
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 is SharpDevelop 4.3. Framework is .NET 4.0 client profile. OS is Win7.
IDE 是 SharpDevelop 4.3。框架是 .NET 4.0 客户端配置文件。操作系统是Win7。
Many thanks :)
非常感谢 :)
采纳答案by Steve
MS Excel only has the following command line switches: Excel Switches
MS Excel 只有以下命令行开关:Excel 开关
To do what you want, you might look into Excel Interop
为了做你想做的事,你可以看看Excel Interop
回答by DSway
You should be able to do this by opening the print dialog first and then using the printer selected with a verb of "PrintTo" like this:
您应该可以通过首先打开打印对话框然后使用带有“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
You may have to include the server in the Arguments (commented line).
您可能必须在参数(注释行)中包含服务器。

