简体   繁体   English

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

[英]How to create Excel file with EPPlus for A4 paper

My current project use EPPlus to create Excel files. 我当前的项目使用EPPlus来创建Excel文件。 These files are printed by the user and I'm trying to force the Excel file to print in only one A4 page, regardless the width og the content. 这些文件由用户打印,我试图强制Excel文件只在一个A4页面打印,无论宽度和内容如何。

Actually, when the file is printed, it takes two pages and the second contains just one column. 实际上,当打印文件时,它需要两页,而第二页只包含一列。

My code: 我的代码:

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;

The result: The result provided by my code 结果:我的代码提供的结果

在此输入图像描述

What I need: 我需要的:

在此输入图像描述

I searched things like "autofit to A4 page", eso but no solution yet. 我搜索了“autofit to A4 page”之类的内容,但是还没有解决方案。

Remark: all the columns are needed. 备注:需要所有列。 I can't simply delete one before creating the file. 在创建文件之前,我不能简单地删除一个。

Thanks for helping! 谢谢你的帮助!

I found the solution. 我找到了解决方案。

EPPlus has a printerSettings for this. EPPlus有一个打印机设置。 The line to use is 要使用的线路是

ws.PrinterSettings.FitToPage = true;

By using this, the default files properties force Excel to print the data in only one page. 通过使用此功能,默认文件属性强制Excel仅在一个页面中打印数据。

Hope this help some other devs. 希望这有助于其他一些开发者。

You can also use this line of code for a specific paper size: 您还可以将此行代码用于特定纸张尺寸:

ws.PrinterSettings.PaperSize = ePaperSize.A4;

Hopte this helps some others people. Hopte这可以帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM