1

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

我们通过as传递 excel 文件名 asfilename和打印机设置。然后我们使用打开然后给定的文件并打开工作簿并通过. 在我选择 XPS 打印机作为打印机的情况下,它会两次获取输出文件名(打印第一张纸的时间和打印其他纸的时间)。当我更改调用输出文件的打印方法时,尚未创建。PrintDialogprinterSettingsMicrosoft.Office.Interop.Excel.ApplicationMicrosoft.Office.Interop.Excel.Workbookwb.Worksheets.PrintOutEx()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);
            }
        }


    }

有谁能够帮我?

4

1 回答 1

1

您是否尝试过使用以下方法而不是NamedRange.PrintOutEx 方法

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

myNamedRange.PrintOutEx(
  1, 
  wb.Worksheets.Count,
  printerSettings.Copies,
  false, 
  printerSettings.PrinterName, 
  false, 
  printerSettings.Collate, 
  false
);
于 2013-01-26T13:06:19.930 回答