Kevin.qin
Kevin.qin

Reputation: 1

java poi export Excel use Header, but show garbled characters with Chinese word

export excel with Header(return XSSFOddHeader),transform to PDF,it show me the mess code when i input Chinese.

I tried to transform the stirng encode and set the response header and contenttype,but it not working.

Later I tested, even if utf-8 is not added to the new String, the corresponding output hex is the same, so the default is to output utf-8 format, but I don't know the encoding format of poi output, and how to solve this problem

I use poi-3.17.jar, here is my code and image.

response.setContentTypeIfNotSet("text/html; charset=UTF-8");
response.setHeader("text/html; charset=UTF-8", "text/html; charset=UTF-8");

        SXSSFWorkbook workbook = new SXSSFWorkbook(1000);// 建工作薄
        workbook.createSheet("哈喽");// 创建一个表

        SXSSFSheet toSheet1 = workbook.getSheetAt(0);
   
        // 设置页眉、页脚
        Header header = toSheet1.getHeader();
        header.setLeft(DateUtil.formatDate(mmAnbuymaterialInfo.getCraetetime()));
        String newStr ="你好世界/PO NO: "+ mmAnbuymaterialInfo.getPurchaseno();
        byte[] bs = newStr.getBytes();
        String UTF_8String = new String(bs, "UTF-8");
        header.setCenter(UTF_8String);

// convert to PDF code

String path = Play.applicationPath + getTmpFilePath(sheetName);
        // 根据生成的
        File file = new File(Play.applicationPath + UPLOAD_TMPDIR + File.separator + subdir);
        // 如果文件夹不存在则创建
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
        fileOut = new FileOutputStream(path);
        workbook.write(fileOut);
        // 根据generateFilePath方法生成的结算清单xlsx转换成pdf形式,并将路径返回
        // 输出pdf文件的路径
        String pdfPath = path.substring(0, path.lastIndexOf(".")) + ".pdf";
        File pdfFile = new File(pdfPath);// 输出路径
        Workbook wb = new Workbook(path);// 原始excel路径
        PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
        pdfSaveOptions.setOnePagePerSheet(true);
        fileOS = new FileOutputStream(pdfFile);
        wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);

mess code image not convert to PDF image

Upvotes: 0

Views: 340

Answers (0)

Related Questions