EPPlus改变细胞的边界颜色

时间:2016-09-13 13:29:17

标签: c# excel epplus

我正在尝试更改所选范围内的单元格边框颜色。除了边框的权重之外,找不到任何其他单元格边框样式,如下所示:

range.Style.Border.Top.Style = ExcelBorderStyle.Thin;
range.Style.Border.Left.Style = ExcelBorderStyle.Thin;
range.Style.Border.Right.Style = ExcelBorderStyle.Thin;
range.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;

3 个答案:

答案 0 :(得分:9)

如果要在单元格的不同部分发送边框颜色,可以这样做:

range.Style.Border.Top.Color.SetColor(Color.Red);
range.Style.Border.Bottom.Color.SetColor(Color.Green);
range.Style.Border.Left.Color.SetColor(Color.Blue);
range.Style.Border.Right.Color.SetColor(Color.Yellow);

SetColor可以使用任何类型的System.Drawing.Color

答案 1 :(得分:4)

您可以使用此代码更改边框样式和颜色

range.Style.Border.BorderAround(ExcelBorderStyle.Medium, System.Drawing.Color.Blue);

答案 2 :(得分:2)

            using (ExcelRange range = worksheet.Cells["A1:H1"])
            {
                range.Style.Font.Bold = true;
                range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                range.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                range.Style.Fill.PatternType = ExcelFillStyle.Solid;
                range.Style.Fill.BackgroundColor.SetColor(Color.White);

                range.Style.Border.Top.Style = ExcelBorderStyle.Thin;
                range.Style.Border.Top.Color.SetColor(Color.Red);
                range.Style.Border.Left.Style = ExcelBorderStyle.Thin;
                range.Style.Border.Left.Color.SetColor(Color.Green);
                range.Style.Border.Right.Style = ExcelBorderStyle.Thin;
                range.Style.Border.Right.Color.SetColor(Color.Green);
                range.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
                range.Style.Border.Bottom.Color.SetColor(Color.Green);
            }

The final output will come as shown here