简体   繁体   中英

How to create Excel file with EPPlus for A4 paper

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!

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.

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

ws.PrinterSettings.PaperSize = ePaperSize.A4;

Hopte this helps some others people.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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