【问题标题】:Saving Excel as PDF fitting everything into one page not working将 Excel 保存为 PDF 将所有内容都放在一页中不起作用
【发布时间】:2021-04-02 22:48:26
【问题描述】:

作为项目的一部分,我需要将某些范围的 Excel 单元格保存为 PDF 文件,但仍要使所有内容都适合一页。如果数据多于一页,则需要很好地修剪并转到第二页。

我的代码将 Excel 文件保存为 PDF,但现在有 8 页,并且数据显示得很大,并且页面的每个边缘周围都有很多空格。 此外,并非所有列都适合一页。就像第一页有 A 列到 G 列,然后在其他页面中,我看到 H 列到 M 列,但这不是我想要的。

var excelPath = "My Excel file path.xlsx";    //Excel file path
var pdfPath = "My PDf file path.pdf";  //Pdf file path
var sheetName = "25. Data";  //Excel sheet name to get data from 
var printRange = "A1:M70";  //Area that needs to be included in PDF

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
var wb = app.Workbooks.Open(excelPath);

Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[sheetName];
ws.PageSetup.Orientation = xlPageOrientation.xlLandscape;
ws.PageSetup.PrintArea = printRange;
ws.PageSetup.FitToPagesWide = 1;
ws.PageSetup.FitToPagesTall = false;
wb.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, pdfPath);

谁能告诉我这段代码有什么问题??

【问题讨论】:

    标签: c# excel pdf


    【解决方案1】:

    想通了。我不知道为什么,但 PageSetup.Zoom 默认设置为 100。添加

    ws.PageSetup.Zoom = false;
    

    解决了这个问题。微软页面说如果这个值设置为trueFitToPagesTallFitToPagesWide将被忽略

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多