首页 > 解决方案 > 如何使用epplus隐藏一列的网格线

问题描述

WorkSheet.View.ShowGridLines = false;

这将在完整的 excel 中隐藏网格线。

标签: c#.netepplus

解决方案


AFAIK,网格线仅适用于整个工作表,无论是在视图中,打印中,或两者兼而有之。我认为您在谈论Border设置。你会做这样的事情:

[TestMethod]
public void Column_Border()
{
    var datatable = new DataTable("tblData");
    datatable.Columns.AddRange(new DataColumn[] {
        new DataColumn("Col0", typeof(object)),
        new DataColumn("Col1", typeof(int)),
        new DataColumn("Col2", typeof(int)),
        new DataColumn("Col3", typeof(object))
    });
    for (int i = 0; i < 10; i++)
    {
        var row = datatable.NewRow();
        row[0] = Path.GetRandomFileName();
        row[1] = i;
        row[2] = i * 10;
        row[3] = Path.GetRandomFileName();
        datatable.Rows.Add(row);
    }
    var fi = new FileInfo("c:\\temp\\Column_Border.xlsx");
    if (fi.Exists)
        fi.Delete();

    using (ExcelPackage pck = new ExcelPackage(fi))
    {
        var WorkSheet = pck.Workbook.Worksheets.Add("source");
        WorkSheet.Cells["A1"].LoadFromDataTable(datatable, true);
        WorkSheet.View.ShowGridLines = false;

        var allCells = WorkSheet.Cells;
        allCells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
        allCells.Style.Border.Top.Style = ExcelBorderStyle.Thin;
        allCells.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
        allCells.Style.Border.Left.Style = ExcelBorderStyle.Thin;
        allCells.Style.Border.Right.Style = ExcelBorderStyle.Thin;

        allCells.Style.Border.Top.Color.SetColor(Color.Green);
        allCells.Style.Border.Bottom.Color.SetColor(Color.Green);
        allCells.Style.Border.Left.Color.SetColor(Color.Green);
        allCells.Style.Border.Right.Color.SetColor(Color.Green);

        var colRange2 = WorkSheet.Column(2);
        colRange2.Style.Border.Top.Style = ExcelBorderStyle.None;
        colRange2.Style.Border.Bottom.Style = ExcelBorderStyle.None;
        colRange2.Style.Border.Left.Style = ExcelBorderStyle.None;
        colRange2.Style.Border.Right.Style = ExcelBorderStyle.None;

        pck.Save();
    }
}

给你这个:

在此处输入图像描述


推荐阅读