简体   繁体   English

VB.NET打开并打印Excel文件

[英]VB.NET open and print an excel file

I am developing a simple VB.NET desktop app for a little printing business. 我正在为一些印刷业务开发一个简单的VB.NET桌面应用程序。 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. 它具有一个主要的WinForm,用于打开JPG / PDF / Word / Excel文件,打开关联程序,打印文件,捕获假脱机作业并最终根据打印机名称,页数向用户收费的按钮。 ,页面大小和每页成本。 No big deal. 没什么大不了。 Hosts machines have Win7 OS. 主机具有Win7 OS。

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. 然后,当用户想要打印XLS文件时,我希望该应用程序打开Excel 2010,打开先前通过文件对话框选择的文件。 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. 当打开Excel时,直接转到打印对话框,然后当作业完成加载到后台打印程序时,我捕获了该事件并杀死Excel进程。

My problem is: 我的问题是:

I cant open Excel directly going to the print dialog. 我无法直接打开打印对话框打开Excel。 Excel DOES respond to the "print" verb. Excel会响应“打印”动词。 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. 我不想只使用默认打印机进行打印,我确实需要允许用户选择所需的打印机,页面,副本等。

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. IDE是SharpDevelop 4.3。 Framework is .NET 4.0 client profile. 框架是.NET 4.0客户端配置文件。 OS is Win7. 操作系统是Win7。

Many thanks :) 非常感谢 :)

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

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). 您可能必须在“参数”(注释行)中包括服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM