使用Interop打印Excel

15

有没有人知道如何使用C#和Excel Interop编程地打印Excel文件?如果有,请提供代码。

3个回答

28

为了打印,您可以使用Worksheet.PrintOut()方法。您可以通过传递Type.Missing来省略任何或所有可选参数。如果您省略了所有参数,则默认从您的活动打印机打印一份副本。但是您可以利用这些参数设置要打印的副本数、整理等。有关更多信息,请参见Worksheet.PrintOut()方法的帮助。

帮助文件中显示的示例为:

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

但是,除非你需要更改默认设置,否则你可以为所有参数简单地传入Type.Missing。以下是使用自动化打开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);
}

希望这可以帮到你!

Mike


你是否知道如何显示打印对话框而不是直接打印文档? - yeahumok
是的,但我认为你应该针对此问题提出另一个问题。答案需要一点解释,而且命令本身在注释中看起来很糟糕(有30个可选参数,你必须使用30个Type.Missing)。这是一个简单的答案,但我认为把它塞进注释里有点过分了...所以请开始一个新的问题,我会关注它的。;-) - Mike Rosenblum
命名空间:System.Runtime.InteropServices - John M
嗨John,没错,'System.Runtime.InteropServices' 命名空间用于 'Marshal.FinalReleaseComObject' 调用以在结束时清理内存。它不需要任何 'PrintOut' 方法或任何其他对Excel对象模型的调用。很好的发现,谢谢。 - Mike Rosenblum
这正是我需要做的事情,但我需要将Excel中的单独工作表打印到PDF打印机。然而,每次我这样做时,PDF文件都会以某种方式损坏。我尝试过多个PDF打印机(Adobe、PDF955、PDFCreator、AMYUNI)它们似乎都有同样的问题。我使用了您的示例,但设置了打印机名称、文件名和printofile标志为true,但没有成功。您有什么想法如何让它工作? - Karl
@MikeRosenblum 这段代码非常好用。谢谢你的分享。 - B.K.

2

重要改进是选择打印机的代码,例如:

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

0
所有已经给出的答案都很好,但我认为我可以更简单地提供更多显示对话框和定义页面打印方向的选项。
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);

}

希望这段代码能对某人有所帮助。 :D

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接