2013-09-24 52 views
1

我正在開發一個簡單的VB.NET桌面應用程序,用於一點印刷業務。它有一個主要的WinForm,用於打開JPG/PDF/Word/Excel文件的按鈕,打開相關程序,打印文件,捕獲卷軸作業,並最終根據打印機名稱,打印頁數,頁面大小和每頁成本。沒什麼大不了。主機有Win7操作系統。VB.NET打開並打印一個excel文件

當用戶想要打印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。

非常感謝:)

回答

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 

您可能必須在參數(註釋行)中包含服務器。