Cell Formatting Issues

Apologies up front, it appears that I am unable to search the forums at this time. I just keep getting Error pages:

I am trying to set the style on a single cell without luck, any suggestions? I am using the following code:

var cellText = "{TextSource}";
var trimLength = 60;
...
var style = cell.GetStyle();
if (style.IsLocked) { style.IsLocked = false; }
var offset = (cellText.Length / trimLength);
style.Font.Size -= offset;
cell.SetStyle(style);
...
cell.PutValue(cellText);
...
workbook.Save(newPath);
Hi,

Check some topics for applying styles on cells in the documentation for your complete reference:
http://www.aspose.com/docs/display/cellsnet/Approaches+to+Format+Data+in+Cells
http://www.aspose.com/docs/display/cellsnet/Dealing+with+Font+Settings
http://www.aspose.com/docs/display/cellsnet/Colors++and++Background+Patterns

Check the topics for protecting worksheets:
http://www.aspose.com/docs/display/cellsnet/Protecting+Worksheets
http://www.aspose.com/docs/display/cellsnet/Advanced+Protection+Settings+since+Excel+XP

Hope, this helps you.

thank you.

Thanks for the replies, I actually don’t need to protect the worksheet, so that code may be superfluos. Apologies for not sending a more complete example, I have been using the documentation extensively.
My “cell” variable is defined from:

var workbook = new Workbook(targetFile);
var ranges = workbook.Worksheets.GetNamedRanges();
if ((ranges != null) && (ranges.Length > 0)) {
foreach (var range in ranges) {
...
var cell = range.GetCellOrNull(range.RowCount - 1, range.ColumnCount - 1) ?? range[0, 0];
...
var style = cell.GetStyle();
...
}
}

What I am doing is updating data in a number of named ranges. And I get the cell, then try to modify the style. No matter what I do, this style does not change

Hi,

What I understand is that you want to set the style of a Range object.

You need to use the Range.ApplyStyle() method for this purpose.

Please see the following code. It applies a background color to yellow and set the number style for all the cells inside the range as shown in the screenshot.


C#


string path = @“F:\Shak-Data-RW\Downloads\source.xlsx”;


Workbook workbook = new Workbook(path);

Worksheet worksheet = workbook.Worksheets[0];


string lastCell = worksheet.Cells.LastCell.Name;


Range rng = worksheet.Cells.CreateRange(“A1:” + lastCell);


Style st = workbook.CreateStyle();

//set the number pattern

st.Number = 2;


//set the background color to yellow

st.Pattern = BackgroundType.Solid;

st.ForegroundColor = Color.Yellow;


StyleFlag flag = new StyleFlag();

flag.All = true;


rng.ApplyStyle(st, flag);


workbook.Save(path + “.out.xlsx”);


Screenshot:

Thanks, I think you just helped me see the problem!

Hi,

Thanks for your feedback.

It’s good to know your issue is now resolved with the above code.

If you find any other problem, please feel free to post, we will be glad to help you.