Border style comparator

Hello. I needed to build right style’s enumeration. So i have implemented array’s comparator, that applies different border style for two neighbor cells, then reads a border style for result and decide, that border style had biggest priority. But in some step each cell had different cell’s border. Can you help me with this issue?


private static class StyleComparator implements Comparator {

private Cells cells;

private int column;

private StyleComparator(Cells cells) {
this.cells = cells;
this.column = 3;
}

@Override
public int compare(Integer o1, Integer o2) {
Style cs1 = cells.getCellStyle(3, column);
cs1.setBorder(BorderType.BOTTOM_BORDER, o1, Color.getBlack());
cells.get(3, column).setStyle(cs1);

Style cs2 = cells.getCellStyle(4, column);
cs2.setBorder(BorderType.TOP_BORDER, o2, Color.getBlack());
cells.get(4, column).setStyle(cs2);

cs1 = cells.getCellStyle(3, column);
Integer result1 = cs1.getBorders().getByBorderType(BorderType.BOTTOM_BORDER).getLineStyle();
cs2 = cells.getCellStyle(4, column);
Integer result2 = cs2.getBorders().getByBorderType(BorderType.TOP_BORDER).getLineStyle();

assertEquals(result1, result2);

cells.get(6, column).putValue(result1);

column += 2;

if (result1.equals(o1)) {
return 1;
} else if (result1.equals(o2)) {
return -1;
} else {
return 0;
}
}
}

public void bordersTest() throws Exception {
Workbook wb = new Workbook();

Cells cells = wb.getWorksheets().get(0).getCells();

List styles = Arrays.asList(
CellBorderType.NONE,
CellBorderType.THIN,
CellBorderType.MEDIUM,
CellBorderType.DASHED,
CellBorderType.DOTTED,
CellBorderType.THICK,
CellBorderType.DOUBLE,
CellBorderType.HAIR,
CellBorderType.MEDIUM_DASHED,
CellBorderType.MEDIUM_DASH_DOT,
CellBorderType.DASH_DOT_DOT,
CellBorderType.MEDIUM_DASH_DOT_DOT,
CellBorderType.SLANTED_DASH_DOT,
CellBorderType.DASH_DOT
);

Collections.sort(styles, new StyleComparator(cells));

for (Integer style : styles) {
System.out.println(style);
}

wb.save(“D:\out.xlsx”);
}

Hi Alexey,

Thanks for your posting and using Aspose.Cells.

Please explain your issue more what you are trying to achieve with this code. We will look into your issue and let you know any workaround if possible.

Please note that two adjacent cells share same border for example the right border of cell A1 and left border of cell A2 should be same.

Also let us know what do you mean by
that border style had biggest priority.

Thanks for your cooperation.

Hello Shakeel Faiz.


I have modify test to simple way. Issue cause is that adjanced cells {3,3} (bottom border) and {4, 3} (top border) are different.

public void simpleBorderTest() {
Workbook wb = new Workbook();

Cells cells = wb.getWorksheets().get(0).getCells();

Style cs1 = cells.getCellStyle(3, 3);
cs1.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getBlack());
cells.get(3, 3).setStyle(cs1);

Style cs2 = cells.getCellStyle(4, 3);
cs2.setBorder(BorderType.TOP_BORDER, CellBorderType.THIN, Color.getBlack());
cells.get(4, 3).setStyle(cs2);

cs1 = cells.getCellStyle(3, 3);
Integer result1 = cs1.getBorders().getByBorderType(BorderType.BOTTOM_BORDER).getLineStyle();
cs2 = cells.getCellStyle(4, 3);
Integer result2 = cs2.getBorders().getByBorderType(BorderType.TOP_BORDER).getLineStyle();

assertEquals(result1, result2);
}


About border style priority - u can see it in Excel. “Medium” border always had bigger priority, than “Thin” (for adjanced cells), “Thick” border always had bigger priotity, than “Medium” etc.

Hi Alexey,

Thanks for your sample code and using Aspose.Cells.

We have tested your code with the latest version: Aspose.Cells
for Java v8.1.0.3
and found this issue. Adjacent cells D4 (bottom border) and D5 (top border) are different. They should be same.

We have logged this issue in our database for investigation. We will look into it and fix this issue or if you need any code change we will suggest it to you. Once, there is some fix or other news for you, we will update you asap.

This issue has been logged as CELLSJAVA-40867.

Hi,


We have evaluated your issue further. Well, to make borders synchronized when applying borders on adjacent cells similar to MS Excel’s behavior, please set WorkbookSettings.UpdateAdjacentCellsBorder as true. The default value of this property is false for performance considerations.
e.g
Sample code:

Workbook wb = new Workbook();
wb.getSettings().setUpdateAdjacentCellsBorder(true);

Thank you.

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan