EPPlus:如何将列和行的背景颜色设置为不同,扩展宽度并将单个单元格设置为电子表格的名称?

EPPlus: How to set background color for Columns and Rows to be different, expand the width and set the single cell as a name of the spreadsheet?

我正在使用 EPPlus 库将 DataTable 映射到 excel。这是我的代码:

using (pck = new ExcelPackage())
{
        ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");
        ws.Cells["A1:I1"].LoadFromDataTable(dt, true);
        using (ExcelRange rng = ws.Cells["A1:I1"])
        {
            rng.Style.Border.Top.Style = ExcelBorderStyle.Thick;
            rng.Style.Border.Bottom.Style = ExcelBorderStyle.Thick;
            rng.Style.Border.Left.Style = ExcelBorderStyle.Thick;
            rng.Style.Border.Right.Style = ExcelBorderStyle.Thick;

            rng.Style.Border.Top.Color.SetColor(System.Drawing.Color.White);
            rng.Style.Border.Bottom.Color.SetColor(System.Drawing.Color.White);
            rng.Style.Border.Left.Color.SetColor(System.Drawing.Color.White);
            rng.Style.Border.Right.Color.SetColor(System.Drawing.Color.White);

            rng.Style.Fill.PatternType = ExcelFillStyle.Solid;
            rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.BurlyWood);

            ws.Cells.Style.Fill.PatternType = ExcelFillStyle.Solid;
            ws.Cells.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Bisque);

            ws.Cells.Style.Border.Top.Style = ExcelBorderStyle.Thick;
            ws.Cells.Style.Border.Bottom.Style = ExcelBorderStyle.Thick;
            ws.Cells.Style.Border.Left.Style = ExcelBorderStyle.Thick;
            ws.Cells.Style.Border.Right.Style = ExcelBorderStyle.Thick;

            ws.Cells.Style.Border.Top.Color.SetColor(System.Drawing.Color.White);
            ws.Cells.Style.Border.Bottom.Color.SetColor(System.Drawing.Color.White);
            ws.Cells.Style.Border.Left.Color.SetColor(System.Drawing.Color.White);
            ws.Cells.Style.Border.Right.Color.SetColor(System.Drawing.Color.White);

        }
        Response.AddHeader("content-disposition", "inline;filename=" + ReportName + ".xls");
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.BinaryWrite(pck.GetAsByteArray());
}

当运行上述代码时,我得到以下结果:

我需要这样的东西:

如您所见,列和行的背景颜色不同,列的宽度更大并且报表有一个名称。

我怎样才能达到那种效果?

调换顺序。首先为整个工作表设置,然后为范围设置。目前您通过设置工作表颜色覆盖范围颜色。

我找到了解决方案:

using (pck = new ExcelPackage())
{
            ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");
            ws.Cells["A5:I5"].LoadFromDataTable(dt, true);
            ws.DefaultColWidth = 25;

            ws.Cells.Style.Border.Top.Style = ExcelBorderStyle.Thick;
            ws.Cells.Style.Border.Bottom.Style = ExcelBorderStyle.Thick;
            ws.Cells.Style.Border.Left.Style = ExcelBorderStyle.Thick;
            ws.Cells.Style.Border.Right.Style = ExcelBorderStyle.Thick;

            ws.Cells.Style.Border.Top.Color.SetColor(System.Drawing.Color.White);
            ws.Cells.Style.Border.Bottom.Color.SetColor(System.Drawing.Color.White);
            ws.Cells.Style.Border.Left.Color.SetColor(System.Drawing.Color.White);
            ws.Cells.Style.Border.Right.Color.SetColor(System.Drawing.Color.White);
            var headerCell = ws.Cells["A5:I5"];
            headerCell.Style.Fill.PatternType = ExcelFillStyle.Solid;
            headerCell.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.BurlyWood);
            var headerFont = headerCell.Style.Font;
            headerFont.Bold = true;
            int totalRow = ws.Dimension.End.Row;
            int totalCol = ws.Dimension.End.Column;
            using (ExcelRange rng = ws.Cells[6,1,totalRow,totalCol])
            {

                rng.Style.Border.Top.Style = ExcelBorderStyle.Thick;
                rng.Style.Border.Bottom.Style = ExcelBorderStyle.Thick;
                rng.Style.Border.Left.Style = ExcelBorderStyle.Thick;
                rng.Style.Border.Right.Style = ExcelBorderStyle.Thick;

                rng.Style.Border.Top.Color.SetColor(System.Drawing.Color.White);
                rng.Style.Border.Bottom.Color.SetColor(System.Drawing.Color.White);
                rng.Style.Border.Left.Color.SetColor(System.Drawing.Color.White);
                rng.Style.Border.Right.Color.SetColor(System.Drawing.Color.White);

                rng.Style.Fill.PatternType = ExcelFillStyle.Solid;
                rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Bisque);

            }
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            ws.Cells["A4"].LoadFromText(name + " Generation Time: " + elapsedMs.ToString());
            Response.AddHeader("content-disposition", "inline;filename=" + name + ".xls");
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.BinaryWrite(pck.GetAsByteArray());

}

我只需要将工作表的设置与范围

交换