используя apache-poi создать Excel вернуть искаженный китайский - PullRequest
0 голосов
/ 17 января 2019

Я использую poi для создания файла excel, содержимое excel - это китайские слова, но я всегда искажаю китайский, кто-нибудь может помочь? Пожалуйста, проверьте мой код ниже.

public class DownPOIUtils { 
@Permission(auth = false)
@Sign(auth = false)
@RequestMapping(value = "/test")
@ResponseBody
public static void downPoi(HttpServletResponse response) throws Exception {
    Map<Student,String> map = new HashMap<Student, String>();
    Student s = new Student("100","张三","男","89");
    Student s1 = new Student("101","李四","男","55");
    Student s2 = new Student("102","王五","男","66");
    map.put(s, "张三");
    map.put(s1, "李四");
    map.put(s2, "王五");
    String fname = "detial" + getTimeStamp();
    OutputStream os = response.getOutputStream();
    response.reset();

    response.setCharacterEncoding("gbk");
    response.setContentType("application/x-download");
    response.setHeader("Content-Disposition", "attachment;filename="+new String("121".getBytes("gbk"), "iso8859-1")+".xls");
    try {
        new DownPOIUtils().new POIS().createFixationSheet(os, map);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static String getTimeStamp() {
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd hh:MM:ss");
    Date date = new Date();
    return dateFormat.format(date);
}

public class POIS {

    public void createFixationSheet(OutputStream os,
            Map<Student, String> student) throws Exception {

        HSSFWorkbook wb = new HSSFWorkbook();


        HSSFSheet sheet = wb.createSheet();
        HSSFRow row = sheet.createRow((short) 0);
        sheet.createFreezePane(0, 1);
        cteateCell(wb, row, (short) 0, new String("学号".getBytes(),"utf-8"));
        cteateCell(wb, row, (short) 1, new String("姓名".getBytes(),"utf-8"));
        cteateCell(wb, row, (short) 2, new String("性别".getBytes(),"utf-8"));
        cteateCell(wb, row, (short) 3, new String("班级".getBytes(),"utf-8"));
        cteateCell(wb, row, (short) 4, new String("分数".getBytes(),"utf-8"));
        int i = 0;
        Set<Student> keySet = student.keySet();
        Iterator<Student> iterator = keySet.iterator();
        while (iterator.hasNext()) {
            HSSFRow rowi = sheet.createRow((short) (++i));
            Student student2 = iterator.next();
            for (int j = 0; j < 3; j++) {
                cteateCell(wb, rowi, (short) 0, student2.getId());
                cteateCell(wb, rowi, (short) 1, student2.getName());
                cteateCell(wb, rowi, (short) 2, student2.getSex());
                cteateCell(wb, rowi, (short) 3, student2.getGrade());
                cteateCell(wb, rowi, (short) 4, student.get(student2));
            }
        }
        wb.write(os);
        os.flush();
        os.close();
        System.out.println("file generated");

    }

    @SuppressWarnings("deprecation")
    private void cteateCell(HSSFWorkbook wb, HSSFRow row, short col,
            String val) {
        HSSFCell cell = row.createCell(col);
        cell.setCellValue(val);
        HSSFCellStyle cellstyle = wb.createCellStyle();
        cell.setCellStyle(cellstyle);
    }
}       

enter image description here

...