我有以下代码:[谢谢Mike Rosenblum!]
使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Text; 使用Microsoft.Office.Interop.Excel; 使用System.Runtime.InteropServices;
命名空间ConsoleApplication17 { 课程 {
static void Main(string[] args) {
//public void PrintMyExcelFile()
//{
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
// Open the Workbook:
Microsoft.Office.Interop.Excel.Workbook wb = excelApp.Workbooks.Open(
@"C:\hello.xls",
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);
// Get the first worksheet.
// (Excel uses base 1 indexing, not base 0.)
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1];
// Print out 1 copy to the default printer:
ws.PrintOut(
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Cleanup:
GC.Collect();
GC.WaitForPendingFinalizers();
Marshal.FinalReleaseComObject(ws);
wb.Close(false, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(wb);
excelApp.Quit();
Marshal.FinalReleaseComObject(excelApp);
}
}
}
我想要完成的是,不是立即打印我的excel文件,我想要一个打印对话框,以便我可以根据需要选择特定的打印机。
我正在使用12.0.0.0 .NET interop for Excel。
有人有什么想法吗?
提前致谢
答案 0 :(得分:8)
可以从.NET访问打印对话框,并且可以使用12.0 PIA在Excel 2007上正常运行。但是,Dialog.Show()命令有30个可选参数。将来,C#4.0将允许省略可选参数(谢天谢地),VB.NET不需要它们,但如果使用C#3.0或更低版本,我们必须为可选参数提供Type.Missing。全部30个:
bool userDidntCancel =
excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrint].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);
Show()方法返回'true'表示操作成功;它返回'false'表示用户点击了取消按钮或转义(esc)键,因此没有动作。
希望这能帮到你......