Using range object to apply formats to cells

I am unsing 2 different calls to the format a group of cells with a border around them. But at the same time I want to be able to bold the contents of the cells within the box. The problem I am having is that whichever operation I perform last, that is the one that gets applied. The first operation does not stay.

Here's some code I was using...


StyleFlag myFlag = new StyleFlag();
myFlag.FontBold = true;

Style myStyle = workbook.Styles[workbook.Styles.Add()];
myStyle.Font.IsBold = true;

Range rng = wks.Cells.CreateRange(5, 0, 6, 14);
rng.Style = myStyle;
rng.SetOutlineBorders(CellBorderType.Thin, System.Drawing.Color.Black);

The last two statements above are the 2 I am questioning. Whichever one I put last, that is the only one that holds.

Can you suggest an alternative?

Hi,

Kindly try the attached version and change your code a bit (it will work fine). Always use Range.ApplyStyle() method instead of Range.Style attribute.

Sample code:

Workbook workbook = new Workbook();
Worksheet ws = workbook.Worksheets[0];

StyleFlag myFlag = new StyleFlag();
myFlag.FontBold = true;

Style myStyle = workbook.Styles[workbook.Styles.Add()];
myStyle.Font.IsBold = true;
Range rng = ws.Cells.CreateRange(5, 0, 6, 14);
rng.ApplyStyle(myStyle,myFlag);
rng.SetOutlineBorders(CellBorderType.Thin, System.Drawing.Color.Black);

workbook.Save("f:\\test\\outBook.xls");

Thank you.

Yes, that works. Thanks.

Now I am trying to bold some of the characters in a single cell. While the bold works, it changes the bolded characters to a different font. Here's my code for this..

Suggestions?

wks.Cells[2.0].Characters(0, 19).Font.IsBold = true;

I have tried following this with this...

wks.Cells[r.Row, r.Col].Characters(r.Start, r.Length).Font.Name = "Courier New";

but that didn't work.

See attachment...

Hi,

Thanks for the template file.

We will figure out your issue soon.

Thank you.

Hi,

Pleaes try this fix in <A href="https://forum.aspose.com/t/86456</A></P>