0

I am working to generate excel using java apache poi i just need to beautify it (with border)

below is the excel that i have successfuly create excel

and here is the excel that i wanted (see those border and currency and background color) enter image description here

heres some of my code to generate the excel

Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet1");
Row row = sheet.createRow(rowIndex);
row.createCell(0).setCellValue("Product Name");
row.createCell(1).setCellValue("name");

FileOutputStream fileOut = new FileOutputStream("excel.xlsx");
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
Alexander Chandra
  • 587
  • 2
  • 9
  • 23
  • See if this [post](https://stackoverflow.com/questions/13930668/add-border-to-merged-cells-in-excel-apache-poi-java) can be useful – soufrk Jun 26 '18 at 13:39

1 Answers1

2

I assume you'd need to break down the creation of your cell in this format first before applying any style onto it:

                    Cell cell1 = row.createCell(0);
                    cell1.setCellValue("Product Name");

Later,

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderTop((short) 1); // single line border
        cellStyle.setBorderBottom((short) 1); // single line border
        ...//add many others here
        cell1.setCellStyle(cellStyle); //apply that style to the cell

Simple way is to create a cellStyle at first and then just go ahead with numerous cell creations as per the application requirement! Next, just loop into each cell to apply the cellStyle if it is a common behavior that you need for all. Hope that helps!

robot_alien
  • 955
  • 2
  • 13
  • 30
  • any idea for currency format? and background color? @robot_alien – Alexander Chandra Jun 28 '18 at 05:04
  • 1
    for the color you can use `cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex()); cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);` and for the currency format a hint: you'll need to use decimal formatter to format it. Please upvote if this answer helped you. Thanks! – robot_alien Jun 28 '18 at 10:06
  • 1
    so for currency i must change it from java? instead excel format? i see, thank you – Alexander Chandra Jun 29 '18 at 03:51