0

I have a java code which compares 2 excel sheets and generates results in a new excel sheet. I have to modify the code in a way that where ever the value of the cell is "Cell Data matches" the cell color should be in red color. Please help me out doing this. TIA

        public static void main(String[] args) throws Exception {


        Scanner in = new Scanner(System.in); 
        System.out.print("Enter Sheet1 path: ");
        String name1 = in.nextLine();
        System.out.print("Enter Sheet2 path: ");
        String name2 = in.nextLine();
        FileInputStream file1 = new FileInputStream(new File(name1));
        FileInputStream file2 = new FileInputStream(new File(name2));

        Path p = Paths.get(name1);
        String filenameTOExempt = p.getFileName().toString();
        String fileAbsolutePath = p.toAbsolutePath().toString();
       String filePath = fileAbsolutePath.replace(filenameTOExempt, "");

        // Creating an Empty WorkBook
        XSSFWorkbook resultSheet = new XSSFWorkbook(); 
        // Create a blank sheet 
        XSSFSheet sheet = resultSheet.createSheet("Comparison Results"); 

        // This data needs to be written (Object[]) 
        Map<String, Object[]> data = new TreeMap<String, Object[]>(); 
        int n = 1;

        try (Workbook wb1 = WorkbookFactory.create(file1)) {
            try (Workbook wb2 = WorkbookFactory.create(file2)) {
                for (String d : ExcelComparator.compare(wb1, wb2)) {
                    // Finding the index of : in order to seperate the Result from d
                    String num = Integer.toString(n);
                    int iend = d.indexOf(":");
                    String res = d.substring(0, iend);
                    res = res.trim();

                    String data1 = StringUtils.substringBetween(d, "[", "]");

                    d = d.replace("["+data1+"]", "");

                    String data2 = StringUtils.substringBetween(d, "[", "]");

                    data.put(num, new Object[]{ data1, data2, res }); 
                    n++;
                   in.close(); 
                   file1.close();
                   file2.close();
                }
            }
        }

where ever the cell has the value "Cell Data Matches" those cells should be in red.

VenkataM
  • 29
  • 3
  • That is just the main method i have included. the whole code is long. – VenkataM Jul 31 '19 at 07:34
  • What did you try? What isn't working? It took me about 10sec to find how to add conditional formatting to excel via apache poi. – Amongalen Jul 31 '19 at 07:42
  • 2
    Check this link. https://stackoverflow.com/questions/11529542/changing-cell-color-using-apache-poi/11530150 – Sambit Jul 31 '19 at 08:00
  • Possible duplicate of [Changing cell color using apache poi](https://stackoverflow.com/questions/11529542/changing-cell-color-using-apache-poi) – jpeg Jul 31 '19 at 08:02

0 Answers0