Free Support Forum - aspose.com

Cannot Read Border Style

Hi Guys,

I am getting very weird problem while trying to read border from the attached file(1.xlsx).

1.xlsx.zip (21.7 KB)

The Code i am using is this:

Worksheet sheet = book.getWorksheets().get("Sheet1");
Cells cells = sheet.getCells();

Cell cell = cells.get("B2");

System.out.println("Top border line style: "+cell.getStyle().getBorders().getByBorderType(BorderType.TOP_BORDER).getLineStyle());
System.out.println("Bottom border line style: "+cell.getStyle().getBorders().getByBorderType(BorderType.BOTTOM_BORDER).getLineStyle());
System.out.println("Right border line style: "+cell.getStyle().getBorders().getByBorderType(BorderType.RIGHT_BORDER).getLineStyle());
System.out.println("Left border line style: "+cell.getStyle().getBorders().getByBorderType(BorderType.LEFT_BORDER).getLineStyle());

The output I am getting is this:

Top border line style: 1
Bottom border line style: 1
Right border line style: 0
Left border line style: 1

Why is the right border not getting read. Can you please help me?

Thanks in advance!

@dvector,

We were able to observe the issue but we need to look into it more. We have logged the issue in our database for investigation and for a fix. Once, we will have some news for you, we will update you in this topic.

This issue has been logged as

CELLSJAVA-42716 - Wrong value retrieved for border style

@dvector,

We evaluated your issue further and found it is not an issue with the API. Since the underlying cell “B2” is merged cell (B2–>B2+C2), so, you got to un-merge the range of cells and then get individual style of each cell (B2 and C2) in the range, it will give complete valid results. See the updated sample code that works fine as I tested:
e.g
Sample code:

Workbook wb = new Workbook("f://files//1.xlsx");

		Worksheet sheet = wb.getWorksheets().get("Sheet1");
		Cells cells = sheet.getCells();

		Cell cell = cells.get("B2");
		
		if(cell.isMerged())
	    {
			
			Range range = cell.getMergedRange();
			Iterator<Cell> rcells = range.iterator();
			while (rcells.hasNext()) {
				Cell rcell = rcells.next();
				System.out.println(rcell.getName() + " " +"Top border line style: "+rcell.getStyle().getBorders().getByBorderType(BorderType.TOP_BORDER).getLineStyle());
				System.out.println(rcell.getName() + " " +"Bottom border line style: "+rcell.getStyle().getBorders().getByBorderType(BorderType.BOTTOM_BORDER).getLineStyle());
				System.out.println(rcell.getName() + " " +"Right border line style: "+rcell.getStyle().getBorders().getByBorderType(BorderType.RIGHT_BORDER).getLineStyle());
				System.out.println(rcell.getName() + " " +"Left border line style: "+rcell.getStyle().getBorders().getByBorderType(BorderType.LEFT_BORDER).getLineStyle());
		}
			
	    }

Output:
B2 Top border line style: 1
B2 Bottom border line style: 1
B2 Right border line style: 0
B2 Left border line style: 1
C2 Top border line style: 1
C2 Bottom border line style: 1
C2 Right border line style: 1
C2 Left border line style: 0

Hope, this helps a bit.

@dvector,

This is to inform you that we have fixed your original issue, so you do not need to use the suggested workaround/approach (as mentioned in our previous reply). We have added a method named GetDisplayStyle(bool includeMergedBorders) of Cell class to return the border from merged cells for your needs.

Once the fix is available, we will share the Download link to the new fix with you.

Keep in touch.

Thanks you so much :slight_smile:

@dvector,

You are welcome.

@dvector,

Please try our latest version/fix (attached): Aspose.Cells for Java v18.8.5

Your issue should be fixed in it.

Let us know your feedback.
Aspose.Cells for Java v18.8.5.zip (6.3 MB)

This works like a charm. Thank you!

@dvector,

Good to know that your issue is resolved by the new fix/version. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

The issues you have found earlier (filed as CELLSJAVA-42716) have been fixed in Aspose.Cells for Java 18.9. You can also get the latest Aspose.Cells for Java version from Maven repos. with simple configurations. Please see the document for your reference: https://docs.aspose.com/display/cellsjava/Installation

This message was posted using BugNotificationTool from Downloads module by Amjad_Sahi

Hi,

Was this fixed?

I am using v18.9 with Gradle:

compile group:'com.aspose', name:'aspose-cells',version:'18.9'

The code I am using and the output is below:

Worksheet sheet = book.getWorksheets().get("Sheet1");
    Cells cells = sheet.getCells();

    Cell cell = cells.get("H2");


    System.out.println("Top border line style: "+cell.getStyle().getBorders().getByBorderType(BorderType.TOP_BORDER).getLineStyle());
    System.out.println("Bottom border line style: "+cell.getStyle().getBorders().getByBorderType(BorderType.BOTTOM_BORDER).getLineStyle());
    System.out.println("Right border line style: "+cell.getStyle().getBorders().getByBorderType(BorderType.RIGHT_BORDER).getLineStyle());
    System.out.println("Left border line style: "+cell.getStyle().getBorders().getByBorderType(BorderType.LEFT_BORDER).getLineStyle());


    cell = cells.get("B2");


    System.out.println("Top border line style: "+cell.getStyle().getBorders().getByBorderType(BorderType.TOP_BORDER).getLineStyle());
    System.out.println("Bottom border line style: "+cell.getStyle().getBorders().getByBorderType(BorderType.BOTTOM_BORDER).getLineStyle());
    System.out.println("Right border line style: "+cell.getStyle().getBorders().getByBorderType(BorderType.RIGHT_BORDER).getLineStyle());
    System.out.println("Left border line style: "+cell.getStyle().getBorders().getByBorderType(BorderType.LEFT_BORDER).getLineStyle());

Output:

> Top border line style: 1
> Bottom border line style: 1
> Right border line style: 0
> Left border line style: 1
> Top border line style: 1
> Bottom border line style: 1
> Right border line style: 0
> Left border line style: 1

I am using the same file as before

@dvector,

Thank you for writing to us again. As suggested in a previous post you may please try GetDisplayStyle(bool includeMergedBorders) function. It returns proper value according to the flag set in the argument. Please give a try to the following sample code and provide your feedback.

Workbook book = new Workbook(path + "1.xlsx");
Worksheet sheet = book.getWorksheets().get("Sheet1");
Cells cells = sheet.getCells();
Cell cell = cells.get("B2");

System.out.println("With IncludeMergedBorders = true");
Style stWithIncludeMergedBorders = cell.getDisplayStyle(true);
System.out.println("Top border line style: "+stWithIncludeMergedBorders.getBorders().getByBorderType(BorderType.TOP_BORDER).getLineStyle());
System.out.println("Bottom border line style: "+stWithIncludeMergedBorders.getBorders().getByBorderType(BorderType.BOTTOM_BORDER).getLineStyle());
System.out.println("Right border line style: "+stWithIncludeMergedBorders.getBorders().getByBorderType(BorderType.RIGHT_BORDER).getLineStyle());
System.out.println("Left border line style: "+stWithIncludeMergedBorders.getBorders().getByBorderType(BorderType.LEFT_BORDER).getLineStyle());

System.out.println("With IncludeMergedBorders = false");
Style stWithoutIncludeMergedBorders = cell.getDisplayStyle(false);
System.out.println("Top border line style: "+stWithoutIncludeMergedBorders.getBorders().getByBorderType(BorderType.TOP_BORDER).getLineStyle());
System.out.println("Bottom border line style: "+stWithoutIncludeMergedBorders.getBorders().getByBorderType(BorderType.BOTTOM_BORDER).getLineStyle());
System.out.println("Right border line style: "+stWithoutIncludeMergedBorders.getBorders().getByBorderType(BorderType.RIGHT_BORDER).getLineStyle());
System.out.println("Left border line style: "+stWithoutIncludeMergedBorders.getBorders().getByBorderType(BorderType.LEFT_BORDER).getLineStyle());

OUTPUT
With IncludeMergedBorders = true
Top border line style: 1
Bottom border line style: 1
Right border line style: 1
Left border line style: 1
With IncludeMergedBorders = false
Top border line style: 1
Bottom border line style: 1
Right border line style: 0
Left border line style: 1

Ah sorry about that. completely missed it.

Thank you for your help

@dvector,

We are glad to know that your issue is resolved. Please feel free to contact us if you have any other query.

Hi @ahsaniqbalsidiqui or anyone else that can help me,

I have an issue. Though i applied the style on cell B2, Cell A2 also gives a right border. Similarly D2 gives a left border, B1 and C1 gives a bottom border and B3 and C3 gives a top border.

Is there any way I can avoid this problem of double borders?

@dvector,

I have tested the same behavior using Excel and observed that when I set style to B2, the A2 also shows right border in the format cell dialog. Please test the same behavior using Excel and provide your feedback. If any difference is observed, let us know so that we may log it for further analysis and testing.

Hi @ahsaniqbalsidiqui,

That is the issue. I do not want double borders. Is there anyway to avoid it?

@dvector

Cells are attached to each other and can not be separated. You can refer to help topics for more information on cells in a spreadsheet. In case you want separate cells you can avoid picking contiguous cells. For example, instead of using cells A1,B1,C1 you can use A1,C1,E1. This way border line will not be shared between cells. If you are able to achieve this using MS Excel then share your template file for our analysis.

Also, you can try to minimize the width of cells or hide some cells. See the document with example code for your reference:

https://docs.aspose.com/display/cellsjava/Show+and+Hide+Elements#ShowandHideElements-ShowandHideRowsandColumns