Applying Underline over a range

Hi,


I have to apply Underline (typically Font Style) over a range [startRow, startCol, endRow, endCol]. Is there any way other than the cell by cell operations for which I can apply it over the range?

Attached is the current code that is being used for applying underline…
private void Underline(Aspose.Cells.Workbook workBook, int startRow, int startColumn, int endRow, int endColumn, string sheetName, FontUnderlineType underLine)
{

startRow–;
endRow–;
startColumn–;
endColumn–;

if (endRow < startRow || endColumn < startColumn)
return;

if (string.IsNullOrEmpty(sheetName))
sheetName = workBook.Worksheets[workBook.Worksheets.ActiveSheetIndex].Name;

for (int rowIndex = startRow; rowIndex <= endRow; rowIndex++)
{
for (int colIndex = startColumn; colIndex <= endColumn; colIndex++)
{
Aspose.Cells.Style style = workBook.Worksheets[sheetName].Cells[rowIndex, colIndex].GetStyle();
style.Font.Underline = underLine;
workBook.Worksheets[sheetName].Cells[rowIndex, colIndex].SetStyle(style);
}
}
}

Thanks & Regards,.
Anil

Hi,


Yes, you may create a range based on your desired cells area and specify the style for range. Lastly apply the style to the range, see the sample code below:

Workbook workbook = new Workbook();

//Define a style object adding a new style
//to the collection list.
Style stl5 = workbook.Styles[workbook.Styles.Add()];
//Set underline type.
stl5.Font.Underline = FontUnderlineType.Double;
StyleFlag flag = new StyleFlag();
flag.FontUnderline = true;

//Create a named range of cells (A1:B10)in the first worksheet.
//Range range = workbook.Worksheets[0].Cells.CreateRange(“A1:B10”);
//Or
//Range range = workbook.Worksheets[0].Cells.CreateRange(0,0,10,2);
//Or
Range range = workbook.Worksheets[0].Cells.CreateRange(“A1”, “B10”);
//Apply the style to cells in the named range.
range.ApplyStyle(stl5, flag);

workbook.Save(“e:\test2\rangeunderlined.xlsx”);


Also, check the document for reference:
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/named-ranges.html


Thank you.