如何在 excel 单元格 SXSSFSheet java 中添加复选符号

how to add check symbol in excel cell SXSSFSheet java

如果条件满足,我需要在此单元格中打勾。这是我的示例代码:

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);}

我已经用过

UTF-16-feff2713

UTF-16BE - 2713

UTF-16LE - 1327

UTF-8 - e29c93

click here for sample output 样本预期输出

面积 |目标 |区域结果 | MOB > 区域

城市 | 85% | 80% | ✔

我能想到的唯一方法是使用符号字体(如 Windings)格式化您的单元格,并在该字体中使用正确的字符作为复选符号。 所以你的代码应该是这样的:

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);
}

尝试使用

ChrW(&H2713)

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

没有采用 char 的方法 setCellValue。尝试在那里使用 String

以下对我有用:

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();
  }
 }
}

结果: