Interop Worksheets printoutEx不会打印第一页

时间:2013-01-26 11:53:20

标签: c# printing interop

我有一个生成excel文件的应用程序。当按照以下方法打印时,不打印第一页。

我们将filename的excel文件名和PrintDialog的打印机设置作为printerSettings传递。 然后我们使用Microsoft.Office.Interop.Excel.Application打开给定文件,Microsoft.Office.Interop.Excel.Workbook打开工作簿并通过wb.Worksheets.PrintOutEx()打印整张表。 在我选择XPS打印机作为打印机的情况下,它获取输出文件名两次(打印第一张纸的时间和打印其他纸张的时间)。 当我更改调用wb.Worksheets.PrintOutEx(1,1, ... )的打印方法时,尚未创建输出文件。

public static void PrintWorksheet(string fileName, PrinterSettings printerSettings )
    {
        Application excelApp = null;//excel application
        Microsoft.Office.Interop.Excel.Workbook wb = null;//workbook
        try
        {
            excelApp = new Microsoft.Office.Interop.Excel.Application();//initialize excel app

            wb = excelApp.Workbooks.Open(
            fileName,
                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); //open workbook


                wb.Worksheets.PrintOutEx(1, wb.Worksheets.Count, printerSettings.Copies, false, printerSettings.PrinterName, false, printerSettings.Collate, false, Type.Missing);//calling print method
        }
        catch(Exception e)
        {
            string err = e.Message;
        }
        finally
        {
//closing the excel app and workbook

            GC.Collect();
            GC.WaitForPendingFinalizers();

            if (wb != null)
            {

                Marshal.FinalReleaseComObject(wb.Worksheets);

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

                Marshal.FinalReleaseComObject(wb);
            }

            if (excelApp != null)
            {
                excelApp.Quit();

                Marshal.FinalReleaseComObject(excelApp);
            }
        }


    }

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您是否尝试过以下方法NamedRange.PrintOutEx Method

Microsoft.Office.Tools.Excel.NamedRange myNamedRange = Globals.Sheet1.namedRange1;

myNamedRange.PrintOutEx(
  1, 
  wb.Worksheets.Count,
  printerSettings.Copies,
  false, 
  printerSettings.PrinterName, 
  false, 
  printerSettings.Collate, 
  false
);