【问题标题】:exportasfixedformat no paper sizeexportasfixedformat 无纸张大小
【发布时间】:2014-03-04 06:35:28
【问题描述】:

我正在尝试使用 C# excel 文件中的 exportasfixedformat 导出到 pdf 文件。但是excel文件转换为pdf文件(列和行)纸张大小时太大了。将excel文件导出为pdf文件时有没有办法消除纸张大小?

ws.UsedRange.Font.Size = 5;
ws.UsedRange.Font.Name = "Arial Narrow";

ws.UsedRange.Columns.ColumnWidth = 20;

Range RS;
RS = ws.get_Range("A:A", Type.Missing);
RS.EntireColumn.ColumnWidth = 30;

ws.PageSetup.Zoom =false;
ws.PageSetup.FitToPagesWide = 1;
ws.PageSetup.FitToPagesTall = false;

ws.PageSetup.Orientation = 
    Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
ws.PageSetup.PaperSize = 
    Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperLegal;
ws.PageSetup.LeftMargin = 0.5;
ws.PageSetup.RightMargin = 0.5;
ws.PageSetup.TopMargin = 0.5;
ws.PageSetup.BottomMargin = 0.5;

ws.ExportAsFixedFormat(
    Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, Filename,
       Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityMinimum, 
    true, false, 1, ws.UsedRange.Count, false, System.Reflection.Missing.Value);

但是这段代码的输出会产生相互重叠的字母,使它们无法阅读。

【问题讨论】:

    标签: c# excel pdf


    【解决方案1】:

    您应该在调用导出函数之前设置 FitToPagesTall 或 FitToPagesWide。

    ws.PageSetup.Orientation = 
          Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
    
    // Zoom property must be false, otherwise the "FitToPages" properties 
    // are ignored.
    ws.PageSetup.Zoom = false;
    
    // these set the number of pages tall or wide the worksheet will be 
    // scaled to when printed.
    ws.PageSetup.FitToPagesTall = 1;
    ws.PageSetup.FitToPagesWide = 1;
    

    【讨论】:

      【解决方案2】:

      为了将 Excel 保存为 A4 大小的 PDF,可能需要根据 excel 版本更改 excel 设置。

      Microsoft.Win32.RegistryKey Keyxls = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Office\\16.0\\Excel\\Options", true);
      if (Keyxls != null)
      {
          Keyxls.SetValue("A4Letter", "0", Microsoft.Win32.RegistryValueKind.DWord);
          Keyxls.Close();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-15
        • 2017-02-21
        • 2011-01-30
        • 2015-09-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多