How to color range cell without coloring their borders?

Hi there,

I want to set style to a range. All works, but the border is override by the color.
How to only color the foreground, but keep the border lines?
Range rng = ws_risk.Cells.CreateRange(“A1:L1”);
rng.SetOutlineBorders(CellBorderType.Medium,Color.Black);
Aspose.Cells.Style st = wb.CreateStyle();
st.HorizontalAlignment = TextAlignmentType.Center;
st.VerticalAlignment = TextAlignmentType.Center;

Color color = Color.FromArgb(240, 236, 236);
st.Pattern = BackgroundType.Solid;
st.ForegroundColor = color;
st.Font.Color = Color.Black;
st.Font.IsBold = true;
st.Font.Name = “Calibri”;
st.Font.Size = 10;

StyleFlag flag = new StyleFlag();
flag.Alignments = true;
flag.FontBold = true;
flag.FontColor = true;
flag.All = true;
rng.ApplyStyle(st, flag);

regards and thanks
borderless.png (9.6 KB)

@ibox
If you set StyleFlag.All to true, all style related settings will be replaced. You just need to set the parts that need to be replaced to true. Please refer to the following sample code.

The sample code as follows:

Workbook wb = new Workbook();
Worksheet ws_risk = wb.Worksheets[0];
Range rng = ws_risk.Cells.CreateRange("A1", "L1");
rng.SetOutlineBorders(CellBorderType.Medium, Color.Black);

Aspose.Cells.Style st = wb.CreateStyle();
st.HorizontalAlignment = TextAlignmentType.Center;
st.VerticalAlignment = TextAlignmentType.Center;

Color color = Color.FromArgb(240, 236, 236);
st.Pattern = BackgroundType.Solid;
st.ForegroundColor = color;
st.Font.Color = Color.Black;
st.Font.IsBold = true;
st.Font.Name = "Calibri";
st.Font.Size = 10;

StyleFlag flag = new StyleFlag();
flag.Alignments = true;
flag.FontBold = true;
flag.FontColor = true;
flag.Font = true;
flag.CellShading = true;
//comment this line
//flag.All = true;
rng.ApplyStyle(st, flag);

wb.Save(filePath + "out_net.xlsx");

About how to format a range, please refer to the following documents.

hi, thank u Sir,

Unfortunately, the cell border is gone. How to keep the lines?
borderless2.png (9.6 KB)

regards

@ibox
If you need to set the internal borders of the range, you can use Range.SetInsideBorders method. Please refer to the following sample code.

Workbook wb = new Workbook();
Worksheet ws_risk = wb.Worksheets[0];
Range rng = ws_risk.Cells.CreateRange("A1", "L5");

//set inside borders
CellsColor insideColor = wb.CreateCellsColor();
insideColor.Color = Color.Black;            
rng.SetInsideBorders(BorderType.Horizontal, CellBorderType.Medium, insideColor);
rng.SetInsideBorders(BorderType.Vertical, CellBorderType.Medium, insideColor);

rng.SetOutlineBorders(CellBorderType.Medium, Color.Black);

Aspose.Cells.Style st = wb.CreateStyle();
st.HorizontalAlignment = TextAlignmentType.Center;
st.VerticalAlignment = TextAlignmentType.Center;

Color color = Color.FromArgb(240, 236, 236);
st.Pattern = BackgroundType.Solid;
st.ForegroundColor = color;
st.Font.Color = Color.Black;
st.Font.IsBold = true;
st.Font.Name = "Calibri";
st.Font.Size = 10;

StyleFlag flag = new StyleFlag();
flag.Alignments = true;
flag.FontBold = true;
flag.FontColor = true;
flag.Font = true;
flag.CellShading = true;
//comment this line
//flag.All = true;
rng.ApplyStyle(st, flag);

wb.Save(filePath + "out_net.xlsx");

Hope helps a bit.

Thank u, Sir
It work like a charm.
regards

@ibox,

You are welcome. If you have any further queries or comments, feel free to write us back. We will be happy to assist you soon.