Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  




           


Change PrintArea in Excel using EPPlus
To change the printarea for an Excel file using EPPlus, use PrinterSettings.PrintArea.

For example, this method will reset the HEIGHT (number of rows) that is in the print area as a single page.

///
/// Set page size to this many rows and move any page break down to end
///

///
///
public void SetPrintablePageHeight(ExcelWorksheet ws, int newRowCount)
{
if (newRowCount <= 0)
throw new NotSupportedException("Row count must be a positive integer");

int currentEndRow = ws.Dimension.End.Row;
string currentEndCol = GetColumnLetterFromAddress(ws.PrinterSettings.PrintArea.End.Address);
string newEndAddress = string.Concat(currentEndCol, newRowCount);
string startAddress = ws.PrinterSettings.PrintArea.Start.Address;
ws.PrinterSettings.PrintArea = ws.Cells[startAddress + ":" + newEndAddress];

// remove any other page breaks
for (int row = 1; row <= currentEndRow; row++)
{
if (ws.Row(row).PageBreak)
{
ws.Row(row).PageBreak = false;
}
}

ws.Row(newRowCount).PageBreak = true;
}

string GetColumnLetterFromAddress(string address)
{
string ret = string.Empty;
foreach (char c in address)
{
if (Char.IsLetter(c))
ret += c;
}
return ret;
}

Created By: amos 3/25/2015 3:16:11 PM
Updated: 8/22/2022 4:30:03 PM