C# Excel 导出为固定格式 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10272415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:07:09  来源:igfitidea点击:

Excel ExportAsFixedFormat PDF

c#excel-2007

提问by bl4kh4k

I can successfully take an excel file and export it as a PDF file in c#

我可以成功获取一个excel文件并将其导出为c#中的PDF文件

private static void ExportWorkbookToPDF(string workbook, string output)
{
    if (string.IsNullOrEmpty(workbook) || string.IsNullOrEmpty(output))
    {
        throw new NullReferenceException("Cannot create PDF copy " +
            "from empty workbook.");
    }

    Application excelApplication = new Application();
    excelApplication.ScreenUpdating = false;
    excelApplication.DisplayAlerts = false;
    excelApplication.Visible = false;

    Workbook excelWorkbook = excelApplication.Workbooks.Open(
        Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + 
        "\" + workbook);

    if (excelWorkbook == null)
    {
        excelApplication.Quit();
        excelApplication = null;
        excelWorkbook = null;

        throw new NullReferenceException("Cannot create new excel workbook.");
    }

    try
    {
        excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, 
            Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + 
            "\" + output);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.ReadLine();
    }
    finally
    {
        excelWorkbook.Close();
        excelApplication.Quit();
        excelApplication = null;
        excelWorkbook = null;
    }
}

What parameter or object do I need to access in order to save the excel file as page width instead of page height?

我需要访问什么参数或对象才能将 excel 文件保存为页面宽度而不是页面高度?

采纳答案by Steve

I have found the property required to force the export of your workbook in a PDF with Landscape view.

我找到了强制将您的工作簿导出为具有横向视图的 PDF 所需的属性。

try 
{ 
    ((Microsoft.Office.Interop.Excel._Worksheet)  
    excelWorkbook.ActiveSheet).PageSetup.Orientation =  
    Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;

    excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF,  
    Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + 
        "\" + output); 
} 

回答by user3111213

Please try this:

请试试这个:

object misValue = System.Reflection.Missing.Value;
string paramExportFilePath = @"C:\Test2.pdf";
Excel.XlFixedFormatType paramExportFormat = Excel.XlFixedFormatType.xlTypePDF;
Excel.XlFixedFormatQuality paramExportQuality = Excel.XlFixedFormatQuality.xlQualityStandard;
bool paramOpenAfterPublish = false;
bool paramIncludeDocProps = true;
bool paramIgnorePrintAreas = true;
if (xlWorkBook != null)//save as pdf
    xlWorkBook.ExportAsFixedFormat(paramExportFormat, paramExportFilePath, paramExportQuality,     paramIncludeDocProps, paramIgnorePrintAreas, 1, 1, paramOpenAfterPublish, misValue);

Parameter paramIgnorePrintAreas=trueresizes the page.

参数paramIgnorePrintAreas=true调整页面大小。

回答by Siva.Net

  1. Install The 2007 Microsoft Office Suite Service Pack 3 (SP3) http://www.microsoft.com/en-in/download/details.aspx?id=27838

  2. Install 2007 Microsoft Office Add-in: Microsoft Save as PDF or XPS http://www.microsoft.com/en-in/download/details.aspx?id=7

  1. 安装 2007 Microsoft Office Suite Service Pack 3 (SP3) http://www.microsoft.com/en-in/download/details.aspx?id=27838

  2. 安装 2007 Microsoft Office 插件:Microsoft 另存为 PDF 或 XPS http://www.microsoft.com/en-in/download/details.aspx?id=7

Mandatory :Set Microsoft XPS Document Writeras default printer in server. Because ExportAsFixedFormatfunction executes Microsoft XPS Document Writerto convert Excel to PDF.

强制:Microsoft XPS Document Writer设置为服务器中的默认打印机。因为ExportAsFixedFormat函数执行Microsoft XPS Document Writer将Excel 转换为PDF。

This is working for me.

这对我有用。