为什么在ExportAsFixedFormat中param paramIgnorePrintAreas不起作用?

时间:2013-01-01 18:39:30

标签: c# excel

我正在尝试将excel导出到pdf 我正在使用C#。

我对ExportAsFixedFormat有疑问。我的文档有打印区域(在Excel应用程序中设置,如果手动发送到打印,它们可以工作)。但它在我的程序中不起作用。我将param paramIgnorePrintAreas设置为false,但没有帮助。

public void createXls(){
    ApplicationClass excelApplication = new ApplicationClass();
    Workbook excelWorkBook = null;
    //Worksheet excelWorksheet = null;
    excelApplication.ScreenUpdating = false;
    excelApplication.Visible = false;

    string paramSourceBookPath = @"C:\Temp\Test.xlsx";
    object paramMissing = Type.Missing;
    string paramExportFilePath = @"C:\Temp\Test.pdf";
    XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF;
    XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard;
    bool paramOpenAfterPublish = false;
    bool paramIncludeDocProps = true;
    bool paramIgnorePrintAreas = false;
    object paramFromPage = Type.Missing;
    object paramToPage = Type.Missing;
    try
    {
        // Open the source workbook.
        excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath,
         false, paramMissing, paramMissing, paramMissing,
         paramMissing, paramMissing, paramMissing, paramMissing,
         paramMissing, paramMissing, paramMissing, paramMissing,
         paramMissing, paramMissing);

    // Save it in the target format.
        if (excelWorkBook != null)
        {
            Worksheet excelWorksheet = (Worksheet) excelWorkBook.ActiveSheet;
            excelWorksheet.ExportAsFixedFormat(paramExportFormat,
             paramExportFilePath, paramExportQuality,
             paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage,
             paramToPage, paramOpenAfterPublish,
             paramMissing);
        }
    } catch (Exception ex)
    {
    //richTextBox1.Text=ex.Message.ToString();
    }
    finally
    {
        // Close the workbook object.
        if (excelWorkBook != null)
        {
            excelWorkBook.Close(false, paramMissing, paramMissing);
            excelWorkBook = null;
        }

        // Quit Excel and release the ApplicationClass object.
        if (excelApplication != null)
        {
            excelApplication.Quit();
            excelApplication = null;
        }

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

1 个答案:

答案 0 :(得分:1)

在导出之前,尝试再次设置打印区域:

excelWorksheet.PageSetup.PrintArea = "Print_Area";

我有一个类似的问题,我认为是因为它是Excel的国际版本,这个命令修复了输出格式。