简体   繁体   English

使用 Interop 打印 Excel

[英]Printing Excel using Interop

Does anybody have any idea how to print an excel file programatically using C# and the Excel Interop?有人知道如何使用 C# 和 Excel Interop 以编程方式打印 excel 文件吗? If so, can you please provide code?如果是这样,你能提供代码吗?

In order to print, you can make use of the Worksheet.PrintOut() method.为了打印,您可以使用Worksheet.PrintOut()方法。 You can omit any or all of the optional arguments by passing in Type.Missing .您可以通过传入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.但是您可以使用参数来设置要打印的份数、整理等。有关更多信息,请参阅Worksheet.PrintOut()方法的帮助。

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);
}

But unless you need to change the default settings, you can simply pass in Type.Missing for all the arguments.但是除非您需要更改默认设置,否则您可以简单地为所有参数传入Type.Missing Here's an example using automation to open an Excel Workbook, print the first page, and then shut down:以下是使用自动化打开 Excel 工作簿、打印第一页然后关闭的示例:

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);
}

Hope this helps!希望这可以帮助!

Mike麦克风

Important improvement is the code for select the Printer, for example:重要的改进是选择打印机的代码,例如:

var printers = System.Drawing.Printing.PrinterSettings.InstalledPrinters;

int printerIndex = 0;

foreach(String s in printers)
{
    if (s.Equals("Name of Printer"))
    {
        break;
    }
    printerIndex++;
}

xlWorkBook.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing,printers[printerIndex], Type.Missing, Type.Missing, Type.Missing);

All the answers already given are good but I just thought I keep it much simpler with more options for showing a dialogue box and defining the orientation of the page for printing.已经给出的所有答案都很好,但我只是想我让它更简单,有更多选项来显示对话框和定义打印页面的方向。

private void PrintExcel()
{
    string filePath = "C:\file\location\here\";
            Excel.Application excelApp = new Excel.Application();

    // Open Workbook:
    Excel.Workbook wb = excelApp.Workbooks.Open(filePath);

    // Define the orientation for the page
    ((Excel._Worksheet)wb.ActiveSheet).PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;

    //Decide which worksheet to print
    Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];

    // Option to print with or to show dialogue box
    bool userDidntCancel = excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrint].Show();

    // Option to print out wihtout the dialogue box. 
    // WARNING: Do not use Dialogue option and this at the same time. 
    // It will print the page even if you cancel the dialogue print option.
    ws.PrintOut();

    // Cleanup your code
    GC.Collect();
    GC.WaitForPendingFinalizers();

    Marshal.FinalReleaseComObject(ws);

    wb.Close(false, Type.Missing, Type.Missing);
    Marshal.FinalReleaseComObject(wb);

    // Close/Exit File
    excelApp.Quit();
    Marshal.FinalReleaseComObject(excelApp);

}

I hope this bit helps someone.我希望这对某人有所帮助。 :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM