Free Support Forum - aspose.com

May I do inside borders for an entire named range?

I am familiar with creating an outline border for an entrie range of cells but is it possible to do an inside border (i.e. select a group of cells and with one line of code it draws borders between each and every cell within the group) ?

Hi,

Yes, you may do it. You can get the Cell Array using NamedRange.getCells() method to obtain the array of cells. Now define a style object with your desired border attributes, then apply that style to each cell in the named range in the loop. Please see the following sample code for your reference:

Sample code:

//Instantiating a Workbook object
Workbook workbook = new Workbook();

//Obtaining the reference of the newly added worksheet
Worksheet worksheet = workbook.getWorksheets().addSheet();

//Accessing the “A1” cell from the worksheet
Cell cell = worksheet.getCells().getCell(“A1”);

//Adding some value to the “A1” cell
cell.setValue(“Hello World From Aspose”);

//Creating a range of cells starting from “A1” cell
NamedRange range = worksheet.getCells().createNamedRange(“MyRange”,0,0,9,4);


//Adding a thick outline border with the blue line
//range.setOutlineBorder(BorderLineType.THICK,Color.BLUE);


//Specify a Style object for borders.
Style style = cell.getStyle();
//Setting the line style of the top border
style.setBorderLine(BorderType.TOP,BorderLineType.THICK);
//Setting the color of the top border
style.setBorderColor(BorderType.TOP,Color.BLACK);
//Setting the line style of the bottom border
style.setBorderLine(BorderType.BOTTOM,BorderLineType.THICK);
//Setting the color of the bottom border
style.setBorderColor(BorderType.BOTTOM,Color.BLACK);
//Setting the line style of the left border
style.setBorderLine(BorderType.LEFT,BorderLineType.THICK);
//Setting the color of the left border
style.setBorderColor(BorderType.LEFT,Color.BLACK);
//Setting the line style of the right border
style.setBorderLine(BorderType.RIGHT,BorderLineType.THICK);
//Setting the color of the right border
style.setBorderColor(BorderType.RIGHT,Color.BLACK);




Cell[][] cellArray = range.getCells();
for(int i = 0 ; i < cellArray.length ; i++)
{
for(int j = 0 ; j < cellArray[i].length ; j++)
{
System.out.println(cellArray[i][j].getName());
//Saving the modified style to the cell.
cellArray[i][j].setStyle(style);

}
}



//Saving the Excel file
workbook.save(“d:\files\myoutlineborders.xls”);



Thank you.

Hi,

Also, you may simply use the API:

Cells.setRangeStyle(int startRow, int endRow, int startColumn, int endColumn, Style style)
to set style for all the cells in a given range, it will surely minimize your looping lines of code.

e.g
//Instantiating a Workbook object
Workbook workbook = new Workbook();

//Obtaining the reference of the newly added worksheet
Worksheet worksheet = workbook.getWorksheets().addSheet();

//Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().getCell("A1");

//Adding some value to the "A1" cell
cell.setValue("Hello World From Aspose");

//Specify a Style object for borders.
Style style = cell.getStyle();
//Setting the line style of the top border
style.setBorderLine(BorderType.TOP,BorderLineType.THICK);
//Setting the color of the top border
style.setBorderColor(BorderType.TOP,Color.BLACK);
//Setting the line style of the bottom border
style.setBorderLine(BorderType.BOTTOM,BorderLineType.THICK);
//Setting the color of the bottom border
style.setBorderColor(BorderType.BOTTOM,Color.BLACK);
//Setting the line style of the left border
style.setBorderLine(BorderType.LEFT,BorderLineType.THICK);
//Setting the color of the left border
style.setBorderColor(BorderType.LEFT,Color.BLACK);
//Setting the line style of the right border
style.setBorderLine(BorderType.RIGHT,BorderLineType.THICK);
//Setting the color of the right border
style.setBorderColor(BorderType.RIGHT,Color.BLACK);

worksheet.getCells().setRangeStyle(0, 9, 0, 4, style);

//Saving the Excel file
workbook.save("d:\\files\\myoutlineborders.xls");


Thank you.