【问题标题】:How to write Japanese characters in excel using poi in java?如何在java中使用poi在excel中写日文字符?
【发布时间】:2014-12-16 06:17:17
【问题描述】:

我正在使用 POI 编写 excel 表,但是当我尝试在 excel 中编写日文字符时遇到了麻烦。将打印一些 ascii 字符而不是日文。我尝试了很多方法来解决,但我不能。谁能帮帮我。

这是我的代码。

public void readExcel() throws IOException {

   workbook = new HSSFWorkbook();
   worksheet = workbook.createSheet("FieldCount");

   HSSFFont font = workbook.createFont();
   font.setColor(HSSFFont.COLOR_RED);
   font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

   // Create the style
   HSSFCellStyle cellStyle = workbook.createCellStyle();
   cellStyle.setFont(font);

   HSSFRow row1 = worksheet.createRow(0);
   HSSFCell cellA1 = row1.createCell((short)0);
   cellA1.setCellStyle(cellStyle);
   cellA1.setCellValue("Object Type");

   HSSFCell cellA2 = row1.createCell((short)1);
   cellA2.setCellStyle(cellStyle);
   cellA2.setCellValue("Object Name");

   HSSFCell cellA3 = row1.createCell((short)2);
   cellA3.setCellStyle(cellStyle);

   cellA3.setCellValue("Script");
   HSSFCell cellA4 = row1.createCell((short)3);
   cellA4.setCellStyle(cellStyle);

   cellA4.setCellValue("Available Script");
   HSSFCell cellA5 = row1.createCell((short)4);
   cellA5.setCellStyle(cellStyle);

   cellA5.setCellValue("Caption");

   i = 1;
}

public void writexsl(Fields fields) {
   System.out.println("\n" + i + "\n");
   try {
      HSSFRow row1 = worksheet.createRow(i);

      HSSFCell cellA1 = row1.createCell((short)0);
      cellA1.setCellValue(fields.ObjectType);
      HSSFCell cellA2 = row1.createCell((short)1);
      cellA2.setCellValue(fields.ObjectName);
      HSSFCell cellA3 = row1.createCell((short)2);
      cellA3.setCellValue(fields.Script);
      HSSFCell cellA4 = row1.createCell((short)3);
      cellA4.setCellValue(fields.ScriptValue);
      HSSFCell cellA5 = row1.createCell((short)4);
      //fields.Cation = URLEncoder.encode(fields.Cation, "UTF-8");
      //fields.Cation = URLDecoder.decode(fields.Cation, "UTF-8"); 

      cellA5.setCellValue(fields.Cation);
      System.out.println("Caption Japanese" + cellA5.getStringCellValue());
      i++;
      System.out.println("write columns");
   }
   catch (Exception e) {
      System.out.println("error" + e);
   }
}

【问题讨论】:

  • 您确定您使用的是最新版本的 Apache POI 吗?您是否确保日文字符正确地进入您的 Java 代码?
  • 是的,我明白了。将 poi 版本升级到 v3.9 后,我在 excel 中获得了日文字符。谢谢你..

标签: java excel character-encoding apache-poi


【解决方案1】:

我用

HSSFRichTextString s = new HSSFRichTextString(str);
cell.setCellValue(s);

使用这种技术创建的 excel 文件确实包含 UTF-16 字符。

【讨论】:

    猜你喜欢
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    相关资源
    最近更新 更多