2

使用我的 EPPlus 代码创建的电子表格的“打印预览”显示了装订线/行号列(可以说是第 0 列):

在此处输入图像描述

如何以编程方式防止打印排水沟/行号(“裂缝?”)列?

我的打印设置代码目前如下:

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;
}
4

2 回答 2

1

如果我了解您的要求,您想显示列标题(将“第 0 行”表示为“A”、“B”、“C”……)但不显示行标题(例如“第 0 列”如“1”,“2”3“等)。如果是这样,我从未见过这样做的方法。您可以同时隐藏行/列标题或同时显示,但不能根据DocumentFormat.OpenXml .Spreadsheet.PrintOptions . 基本上是ShowHeaders你和 Richardo 谈到的选项。

我能想到的唯一体面的工作就是用这样的东西来伪造它。这包括将第一行设置为重复。我也将第一行设置为冻结,但这是可选的:

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();
}

这在输出中给出了这个:

在此处输入图像描述

这在打印预览中(我向下滚动了几页):

在此处输入图像描述

于 2016-09-12T22:18:11.473 回答
0

在 PrinterSettings 对象中调用了一个属性ShowHeaders,它应该可以满足您的需要。你似乎没有它,所以你可能会错过它。

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

您可以在类的源代码中更明确地看到这一点ExcelPrinterSettings

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

希望它对B有帮助。;)

于 2016-09-12T15:34:49.413 回答