How to create Excel file with EPPlus for A4 paper

浪子不回头ぞ 提交于 2019-12-22 10:40:07

问题


My current project use EPPlus to create Excel files. 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.

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.

Remark: all the columns are needed. I can't simply delete one before creating the file.

Thanks for helping!


回答1:


I found the solution.

EPPlus has a printerSettings for this. 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.

Hope this help some other devs.




回答2:


You can also use this line of code for a specific paper size:

ws.PrinterSettings.PaperSize = ePaperSize.A4;

Hopte this helps some others people.



来源:https://stackoverflow.com/questions/34068831/how-to-create-excel-file-with-epplus-for-a4-paper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!