0
votes

I want to compare two excel file cell style. By using apache poi how can i compare styles cell by cell? And also i need which style is not matched, like align or bold or color..etc. That means unmatched styles should be a string(style name - align, color....)

2
Got one solution. Please check!LittlePanda

2 Answers

1
votes

With Cell.getCellStyle() you can find out the style per cell. With that you can check the fine granular CellStyle settings.

1
votes

You might do it this way:

sheet.getRow(0).getCell(0).getCellStyle().getCoreXf();

The getCoreXf() method returns:

<xml-fragment numFmtId="14" fontId="0" fillId="2" borderId="0" xfId="0" applyNumberFormat="1" applyFill="1" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"/>

Sample code (to compare styles of two cells):

FileInputStream file = new FileInputStream(new File("res/Book1.xlsx"));

//Get the workbook instance for XLS file 
XSSFWorkbook workbook = new XSSFWorkbook (file);

//Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);

CTXf style1 = sheet.getRow(0).getCell(0).getCellStyle().getCoreXf();
CTXf style2 = sheet.getRow(0).getCell(1).getCellStyle().getCoreXf();
System.out.println(style1);
System.out.println(style1.equals(style2)); //true if both are same and false if they are different

This seems to be working fine. Please try!