我正在从 C# 应用程序到 Excel 进行 Office 自动化。我正在尝试调出打印预览对话框(或带有预览选项的打印对话框)。我还希望用户能够选择与默认打印不同的打印。
我努力了
sheet1.PrintPreview();
和
sheet1.PrintOutEx(1, 1, 1, true);
但我看不到用户选择其他打印机的方法。
我正在从 C# 应用程序到 Excel 进行 Office 自动化。我正在尝试调出打印预览对话框(或带有预览选项的打印对话框)。我还希望用户能够选择与默认打印不同的打印。
我努力了
sheet1.PrintPreview();
和
sheet1.PrintOutEx(1, 1, 1, true);
但我看不到用户选择其他打印机的方法。
是的,您的 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);
You change the default printer of the Application object, and then print the worksheet, like so. You can get the printers installed on the computer from System.Drawing.Printing.PrinterSettings.InstalledPrinters
and then display these to the user in some sort of dialogue, and then set the ActivePrinter
of the Excel Application instance like so, and then display the Print Preview Dialogue:
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();