Excel ExportAsFixedFormat PDF
本文关键字:PDF ExportAsFixedFormat Excel | 更新日期: 2023-09-27 18:32:05
我可以成功地获取一个 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;
}
}
我需要访问什么参数或对象才能将 excel 文件另存为页面宽度而不是页面高度?
我找到了在具有横向视图的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);
}
-
安装 2007 Microsoft Office 套件服务包 3 (SP3)http://www.microsoft.com/en-in/download/details.aspx?id=27838
-
安装 2007 Microsoft Office 加载项:Microsoft另存为 PDF 或 XPShttp://www.microsoft.com/en-in/download/details.aspx?id=7
必需:Microsoft XPS 文档编写器设置为服务器中的默认打印机。因为导出为固定格式函数Microsoft XPS 文档编写器执行以将 Excel 转换为 PDF。
这对我有用。
请尝试以下操作:
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);
参数paramIgnorePrintAreas=true
调整页面大小。