Search code examples
c#excel-2007epplusprint-previewepplus-4

How can I prevent the left "gutter" (row number) column from printing (EPPlus)?


The "Print Preview" of a spreadsheet created with my EPPlus code shows the gutter/row number column (column 0, so to speak):

enter image description here

How can I programmatically prevent the gutter/row number ("crack?") column from printing?

My printing setup code is currently as follows:

private void ConfigureCustomerSheetForPrinting()
{
    string columnName = GetExcelTextColumnName(customerWorksheet.Dimension.End.Column);
    string printArea = string.Format("A1:{0}{1}", columnName, customerWorksheet.Dimension.End.Row);
    customerWorksheet.PrinterSettings.PrintArea = customerWorksheet.Cells[printArea];
    customerWorksheet.PrinterSettings.FitToPage = true;
    customerWorksheet.PrinterSettings.Orientation = eOrientation.Landscape;
    customerWorksheet.View.ZoomScale = 100;
    customerWorksheet.PrinterSettings.FitToPage = true;
    customerWorksheet.PrinterSettings.FitToHeight = 100;
    customerWorksheet.PrinterSettings.Scale = 100;

    customerWorksheet.PrinterSettings.LeftMargin = (decimal).5 / 2.54M; 
    customerWorksheet.PrinterSettings.RightMargin = (decimal).5 / 2.54M;
    customerWorksheet.PrinterSettings.TopMargin = (decimal).5 / 2.54M;
    customerWorksheet.PrinterSettings.BottomMargin = (decimal).5 / 2.54M;
    customerWorksheet.PrinterSettings.HeaderMargin = (decimal).5 / 2.54M;
    customerWorksheet.PrinterSettings.FooterMargin = (decimal).5 / 2.54M;
}

Solution

  • There is a property called ShowHeaders in the PrinterSettings object that should do what you need. You don't seem to have it there so you might be missing that.

    customerWorksheet.PrinterSettings.ShowHeaders = false; customerWorksheet.View.ShowHeaders = false;

    You can see this more explicitly in the source code in the ExcelPrinterSettings class.

    /// <summary>
    /// Print headings (column letter and row numbers)
    /// </summary>
    public bool ShowHeaders
    {
        get
        {
            return GetXmlNodeBool(_headersPath, false);
        }
        set
        {
            SetXmlNodeBool(_headersPath, value, false);
        }
    }
    

    Hope it helps B. ;)