打印Excel中使用互操作 [英] Printing Excel using Interop

查看:159
本文介绍了打印Excel中使用互操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有任何人有任何想法如何打印Excel文件编程方式使用C#和Excel互操作?如果是这样,你可以请提供code?

Does anybody have any idea how to print an excel file programatically using C# and the Excel Interop? If so, can you please provide code?

推荐答案

为了打印,您可以使用的 Worksheet.PrintOut()方法。你可以通过在 Type.Missing 省略任何或所有的可选参数。如果省略所有的人,将默认为从活动的打印机打印出一份。但是你可以利用的参数来设置打印份数,整理,数量等请参见上的 Worksheet.PrintOut()更多方法。

In order to print, you can make use of the Worksheet.PrintOut() method. You can omit any or all of the optional arguments by passing in Type.Missing. If you omit all of them, it will default to printing out one copy from your active printer. But you can make use of the arguments to set the number of copies to print, collation, etc. See help on the Worksheet.PrintOut() method for more.

他们在帮助文件中显示的示例是:

The example they show in the help file is:

private void PrintToFile()
{
    // Make sure the worksheet has some data before printing.
    this.Range["A1", missing].Value2 = "123";
    this.PrintOut(1, 2, 1, false, missing, true, false, missing);
}

但除非您需要更改默认设置,你可以简单地传递类型。缺少所有的参数。下面是使用自动化打开一个Excel工作簿的例子,打印第一页,然后关闭:

But unless you need to change the default settings, you can simply pass in Type.Missing for all the arguments. Here's an example using automation to open an Excel Workbook, print the first page, and then shut down:

void PrintMyExcelFile()
{
    Excel.Application excelApp = new Excel.Application();

    // Open the Workbook:
    Excel.Workbook wb = excelApp.Workbooks.Open(
        @"C:\My Documents\Book1.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.)
    Excel.Worksheet ws = (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中使用互操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆