2

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;
}
Ernie S
  • 13,902
  • 4
  • 52
  • 79
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

2 Answers2

1

If I understand what you are asking for you want to show the Column headers ( say "Row 0" as "A", "B", "C", ...) but NOT show the Row headers (say "Column 0" as "1", "2" 3", etc.). If so, I have never seen a way to do it. You can hide both row/col header or show both but not either-or according to the DocumentFormat.OpenXml.Spreadsheet.PrintOptions. Basically the ShowHeaders option you and Richardo talked about.

The only decent work around I can think of is to fake it with something like this. This includes setting the first row as a repeat. I set the first row as frozen as well but this is optional:

using (var pck = new ExcelPackage(fi))
{
    var wb = pck.Workbook;
    var ws = wb.Worksheets.Add("Sheet1");

    //Make sure headers are not show
    ws.PrinterSettings.ShowHeaders = false;

    //Header
    ws.Cells[1, 1].Value = "A";
    ws.Cells[1, 2].Value = "B";
    ws.Cells[1, 3].Value = "C";
    ws.Cells[1, 4].Value = "D";

    var headerrange = ws.Cells[1, 1, 1, 4];
    headerrange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
    headerrange.Style.Border.Top.Style = ExcelBorderStyle.Thin;
    headerrange.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
    headerrange.Style.Border.Left.Style = ExcelBorderStyle.Thin;
    headerrange.Style.Border.Right.Style = ExcelBorderStyle.Thin;

    ws.View.FreezePanes(1,4);
    ws.PrinterSettings.RepeatRows = new ExcelAddress("$1:$1");

    //Some data > 1 page
    for (var i = 0; i < 1000; i++)
    {
        ws.Cells[2 + i, 1].Value = DateTime.Now.AddDays(i);
        ws.Cells[2 + i, 2].Value = i;
        ws.Cells[2 + i, 3].Value = i*100;
        ws.Cells[2 + i, 4].Value = Path.GetRandomFileName();
    }

    //Save it
    pck.Save();
}

Which gives this in the output:

enter image description here

And this in print preview (I scrolled down a few pages):

enter image description here

Ernie S
  • 13,902
  • 4
  • 52
  • 79
  • Which part of your code gets rid of the row number column (what you call the "row headers")? – B. Clay Shannon-B. Crow Raven Sep 12 '16 at 22:23
  • 1
    @B.ClayShannon I added the line `ws.PrinterSettings.ShowHeaders = false;` to my answer above. It should not be needed if it is a newly generated sheet by EPPlus. But if you are editing an existing sheet with it turned on then you will need that line. – Ernie S Sep 12 '16 at 22:26
  • Thanks, Ernie; your answer is more extensive, but I felt like I had to give the bounty to the other cat, as it came first, and I have no reason why it didn't work the first time, but it did after that. I still think calling the "crack" column with the row numbers a "header" is confusing if not out-and-out wrong. That's what caused my conceptual blindness. – B. Clay Shannon-B. Crow Raven Sep 13 '16 at 14:43
0

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. ;)

Ricardo Velhote
  • 4,630
  • 1
  • 24
  • 30
  • The numbered column is considered a header? – B. Clay Shannon-B. Crow Raven Sep 12 '16 at 15:38
  • In the real Excel application there is an option to `Show rows and column headers` so most likely EPPlus is turning off that option which also includes the column letter. To answer your question comment: Yes. – Ricardo Velhote Sep 12 '16 at 15:45
  • No, I just added that line and regenerated the report, and it still has that numbered column at the far left, so it's not "ShowHeaders" that controls whether that is printed or not. – B. Clay Shannon-B. Crow Raven Sep 12 '16 at 15:46
  • 1
    Can you also try `customerWorksheet.View.ShowHeaders = false;`? – Ricardo Velhote Sep 12 '16 at 15:52
  • No difference with that code; I really don't think that pseudo-column would be referred to as a header. It's a "sider" not a "header" – B. Clay Shannon-B. Crow Raven Sep 12 '16 at 15:56
  • I'm a bit stumped. I know this can be done in Excel (the actual program) via an option and it's also available in Excel via VBA through `Worksheets("Sheet1").PageSetup.PrintHeadings = False` because I've done it before and I'm very sure it hid rows and columns. I don't have actual experience with EPPlus but I remembered that I have done this with VBA so I tried to help :) – Ricardo Velhote Sep 12 '16 at 17:16
  • Thanks, Ricardo; if that is the route, it seems very odd to me that that is called a "Header" because it's not by any stretch of the imagination a header. – B. Clay Shannon-B. Crow Raven Sep 12 '16 at 17:18
  • And now it works; my problem was with the terminology; I couldn't imagine that col (numbered rows) as being a header. To me, a header on my Excel reports is the header section I add (report title, etc.). Why it didn't work before and does now, I have no idea, though. – B. Clay Shannon-B. Crow Raven Sep 12 '16 at 22:53