Search code examples
c#asp.netexcelprintingepplus

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

enter image description here

What I need:

enter image description here

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!


Solution

  • 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.