Home » Java » how to add check symbol in excel cell SXSSFSheet java-Exceptionshub

how to add check symbol in excel cell SXSSFSheet java-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I need to put a check symbol in this cell if the condition met. Here’s my sample code:

private SXSSFWorkbook RepWkBook = null;
private SXSSFSheet RepSheet = null;
private int RepRowNum = 0;
private ResultSet RepResult = null;
private Row RepRow = null;

    RepSheet = RepWkBook.createSheet(reportType);
    RepRowNum = 0;
    Row row = RepSheet.createRow(RepRowNum++);
    CellStyle cellStyle = RepWkBook.createCellStyle();
    Font font = RepWkBook.createFont();
    font.setBold(true);
    cellStyle.setFont(font);cell = RepRow.createCell(col++);

        boolean isMOBhigherThanArea = RepResult.getString("IS_MOB_HIGHER_THAN_AREA").equalsIgnoreCase("1");

        char st = '\u2713';

        if(isMOBhigherThanArea && (!areaStr.equalsIgnoreCase("No Data") || !mobStr.equalsIgnoreCase("No Data"))) {
            cell.setCellValue(st);}

I already used

UTF-16 – feff2713

UTF-16BE – 2713

UTF-16LE – 1327

UTF-8 – e29c93

click here for sample output
SAMPLE EXPECTED OUTPUT

Area | MOB Target | Area Result | MOB > Area

City | 85% | 80% | ✔

How to&Answers:

Try using

ChrW(&H2713)

(source: https://www.mrexcel.com/board/threads/how-to-insert-a-checkmark-with-vba.607363/)

Answer:

There is no method setCellValue which takes a char. Try using a String there.

The following works for me:

import java.io.FileOutputStream;
import org.apache.poi.xssf.streaming.*;

class CreateSXSSFUnicode {

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

  char st = '\u2713';
  String[] headers = new String[] {"Area", "MOB Target", "Area Result", "MOB > Area"};

  try (SXSSFWorkbook workbook = new SXSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {

   SXSSFSheet sheet = workbook.createSheet(); 
   SXSSFRow row;

   int rowNum = 0;
   row = sheet.createRow(rowNum++);
   for (int c = 0; c < headers.length; c++) {
    row.createCell(c).setCellValue(headers[c]);
   }
   row = sheet.createRow(rowNum++);
   int c = 0;
   row.createCell(c++).setCellValue("City");
   row.createCell(c++).setCellValue("85%");
   row.createCell(c++).setCellValue("80%");
   //row.createCell(c++).setCellValue(st); // does not work as st is a char
   row.createCell(c++).setCellValue(String.valueOf(st)); // this works

   workbook.write(fileout);
   workbook.dispose();
  }
 }
}

Result:

enter image description here

Answer:

The only way that I can think of doing that is formatting your cell with a symbolic font, as Windings, and use the proper character for the check symbol in this font.
So your code would be something like:

Font font = RepWkBook.createFont();
font.setBold(true);

Font wingDingFont = RepWkBook.createFont();
wingDingFont.setBold(true);
wingDingFont.setFontName("Wingdings);

cell = RepRow.createCell(col++);

boolean isMOBhigherThanArea = RepResult.getString("IS_MOB_HIGHER_THAN_AREA").equalsIgnoreCase("1");

if(isMOBhigherThanArea && (!areaStr.equalsIgnoreCase("No Data") || !mobStr.equalsIgnoreCase("No Data"))) {
    String st = "ü";
    cellStyle.setFont(wingDingFont);
    cell.setCellValue(st);
} else {
    cellStyle.setFont(font);
}