如何使用EPPlus创建A4纸张的Excel文件

4

我的当前项目使用EPPlus创建Excel文件。这些文件由用户打印,我试图强制Excel文件在仅一页A4纸上打印,无论内容的宽度如何。

实际上,当文件被打印时,它需要两页,第二页只包含一列。

我的代码:

ws.PrinterSettings.Orientation = eOrientation.Landscape;
ws.PrinterSettings.PrintArea = ws.Cells[ws_dimension_adress];
ws.PrinterSettings.TopMargin= 0;
ws.PrinterSettings.RightMargin = 0;
ws.PrinterSettings.BottomMargin = 0;
ws.PrinterSettings.LeftMargin = 0;
ws.Cells[ws_dimension_adress].AutoFitColumns();
ws.Cells[ws_dimension_adress].Style.Font.Size = 9;

结果: 我的代码提供的结果

enter image description here

我所需要的:

enter image description here

我搜索了类似于“自适应A4页面”的内容,但是还没有找到解决方案。
备注:所有列都是必需的。在创建文件之前,我不能简单地删除其中一个。
感谢您的帮助!
2个回答

6
我找到了解决方案。
EPPlus有一个针对此问题的printerSettings。 要使用的行是:
ws.PrinterSettings.FitToPage = true;

通过使用此功能,Excel 的默认文件属性会强制将数据打印在一页中。希望这可以帮助其他开发人员。

4

你也可以使用以下代码来指定纸张大小:

ws.PrinterSettings.PaperSize = ePaperSize.A4;

希望这能帮助其他人。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接