views:

1443

answers:

1

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?

+2  A: 

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

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

Hope this helps!

Mike

Mike Rosenblum
thank you so much!!! this was VERY helpful!
Cool, glad I could help. :-)
Mike Rosenblum
you wouldn't happen to know how to show a print dialogue instead of straight away printing the document...would ya?
Yes, but I think you should ask another question for this. The answer needs a little bit of explanation and the command itself will look bad in a comment (there's 30 optional parameters for which you'll have to use 30 Type.Missings). Its an easy answer, but it's a bit much to stuff into a comment I think... So start a new Q, i'll be lookning for it. ;-)
Mike Rosenblum
Namespace: System.Runtime.InteropServices
John M
Hi John, right, the 'System.Runtime.InteropServices' namespace is used for the 'Marshal.FinalReleaseComObject' calls to clean up memory at the end. It isn't needed for any of the 'PrintOut' method, nor any of the other calls to the Excel object model. Good pickup, thanks.
Mike Rosenblum