从C#中find允许用户更改打印机的Excel打印预览对话框

我正在从C#应用程序到Excel的Office自动化。 我正在尝试打开一个打印预览对话框(或带预览选项的打印对话框)。 我也希望用户能够select不同于默认打印的打印。

我努力了

sheet1.PrintPreview(); 

 sheet1.PrintOutEx(1, 1, 1, true); 

但我没有看到用户select其他打印机的方法。

是的,您的Windows应用程序有一个内置的对话框。 如果您有对Excel自动化Application对象的引用,则可以调用Excel中可用的任何内置对话框。

2个链接,你可能会发现有帮助:

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.dialogs.aspx

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.xlbuiltindialog.aspx

例如:为了拉起打印预览对话框,你可以这样做:

 var excelApp = new Excel.Application(); bool dialogResult = excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrintPreview].Show( Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 

您更改应用程序对象的默认打印机,然后打印工作表,如此。 您可以从System.Drawing.Printing.PrinterSettings.InstalledPrinters获得安装在计算机上的打印机,然后以某种对话forms将这些打印机显示给用户,然后像这样设置Excel应用程序实例的ActivePrinter ,然后显示打印预览对话:

 using System.Drawing.Printing; using Excel = Microsoft.Office.Interop.Excel; Excel.Application application = new Excel.Application(); Excel.Workbook workbook = application.Workbooks.Add(); Excel.Worksheet worksheet = workbook.Sheets[1]; var printers = PrinterSettings.InstalledPrinters; Console.WriteLine("Select printer (type the number):"); Console.WriteLine(); for (int i = 0; i < printers.Count; i++) { Console.WriteLine("{0} - {1}", i, printers[i]); } int selection = Convert.ToInt32(Console.ReadLine()); application.ActivePrinter = printers[selection]; worksheet.PrintPreview();