【问题标题】:Using Excel Interop and getting a print dialog使用 Excel 互操作并获取打印对话框
【发布时间】:2010-10-25 23:49:06
【问题描述】:

我有以下代码:[谢谢 Mike Rosenblum!]

使用系统; 使用 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 文件。
我正在为 Excel 使用 12.0.0.0 .NET 互操作。 有人有什么想法吗?

提前致谢

【问题讨论】:

    标签: c# interop printing


    【解决方案1】:

    打印对话框可从 .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) 键,因此没有发生任何操作。

    希望这能让你继续前进......

    【讨论】:

    • 再次感谢您的救命之恩!!非常感谢您的帮助
    • 我已经完成了上面的编码。我在类似的过程中也有问题,即打印预览选项。 Excel.Application excelApp = new Excel.Application(); Excel.Workbook wb = excelApp.Workbooks.Open(@"C:\\Documents and Settings \\Admin \\Desktop \\DoCoMo\\ news5.xls",Type.Missing, Type.Missing, Type.Missing, Type.缺失,类型缺失,类型缺失,类型缺失,类型缺失,类型缺失,类型缺失,类型缺失,类型缺失,类型缺失,类型缺失); Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
    • 尝试解决方案后,打印机对话框没有弹出。我的网站永远挂起。你有可能的方法来纠正它吗?谢谢!
    • @john - 网站挂起?我不会从服务器端代码尝试这个。这意味着打印对话框可能会显示在服务器上,是的,它会挂起。
    猜你喜欢
    • 2010-10-25
    • 2010-10-27
    • 2012-06-09
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    相关资源
    最近更新 更多