1

我正在为小型印刷企业开发一个简单的 VB.NET 桌面应用程序。它有一个主 WinForm、打开 JPG/PDF/Word/Excel 文件的按钮、打开相关程序、打印文件、捕获假脱机作业,最后根据打印机名称、页数向用户收费,页面大小和每页成本。没什么大不了。主机具有 Win7 操作系统。

当用户想要打印 XLS 文件时,我希望应用程序打开 Excel 2010,打开以前使用文件对话框选择的文件。当 Excel 打开时,直接进入打印对话框,然后当作业完成加载到假脱机时,我捕获该事件并 KILL 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。

非常感谢 :)

4

2 回答 2

0

您应该能够通过首先打开打印对话框然后使用带有“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

您可能必须在参数中包含服务器(注释行)。

于 2013-09-24T21:24:21.523 回答
0

MS Excel 只有以下命令行开关:Excel 开关

做你想做的事,你可以看看Excel 互操作

于 2013-09-24T21:16:33.977 回答