java导出excel

亲测可用
@RequestMapping("/createExcel")
public String createExcel(HttpServletResponse response) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet=wb.createSheet("成绩表");
HSSFRow row1=sheet.createRow(0);
HSSFCell cell=row1.createCell(0);
cell.setCellValue("学员考试成绩一览表");
sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
HSSFRow row2=sheet.createRow(1);
row2.createCell(0).setCellValue("姓名");
row2.createCell(1).setCellValue("班级");
row2.createCell(2).setCellValue("笔试成绩");
row2.createCell(3).setCellValue("机试成绩");
HSSFRow row3=sheet.createRow(2);
row3.createCell(0).setCellValue("李明");
row3.createCell(1).setCellValue("As178");
row3.createCell(2).setCellValue(87);
row3.createCell(3).setCellValue(78);


OutputStream output=response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename=details.xls");
response.setContentType("application/msexcel");
wb.write(output);
output.close();
return null;
}







    org.apache.poi
    poi
    3.17



中文名称


    @Override
    public String downloadExcel(HttpServletResponse response) {
        try{
            FundFilterQuery condition=new FundFilterQuery();
            List rows = fundAnalysisDao.queryFundFilter(condition, -1, -1);
            for (Object obj : rows) {
                Map row = (Map) obj;
                System.out.println("fundFullName = " + row.get("fundFullName"));

            }
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet=wb.createSheet("成绩表");
            HSSFRow row1=sheet.createRow(0);
            HSSFCell cell=row1.createCell(0);
            cell.setCellValue("学员考试成绩一览表");
            sheet.addMergedRegion(new CellRangeAddress(0,0,0,4));
            HSSFRow row2=sheet.createRow(1);
            row2.createCell(0).setCellValue("姓名");
            row2.createCell(1).setCellValue("班级");
            row2.createCell(2).setCellValue("笔试成绩");
            row2.createCell(3).setCellValue("机试成绩");


            HSSFRow row3=sheet.createRow(2);
            row3.createCell(0).setCellValue("李明");
            row3.createCell(1).setCellValue("As178");
            row3.createCell(2).setCellValue(87);
            row3.createCell(3).setCellValue(78.52);

            OutputStream output=response.getOutputStream();
            response.reset();
            //response.setHeader("Content-disposition", "attachment; filename=hello1.xls");
            response.setHeader("Content-disposition", "attachment; filename="+toUtf8String("中文.xls"));
            response.setContentType("application/msexcel");
            wb.write(output);
            output.close();


            return UtilJson.listToJson(rows);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static String toUtf8String(String s){
        StringBuffer sb = new StringBuffer();
        for (int i=0;i= 0 && c <= 255){sb.append(c);}
            else{
                byte[] b;
                try { b = Character.toString(c).getBytes("utf-8");}
                catch (Exception ex) {
                    System.out.println(ex);
                    b = new byte[0];
                }
                for (int j = 0; j < b.length; j++) {
                    int k = b[j];
                    if (k < 0) k += 256;
                    sb.append("%" + Integer.toHexString(k).toUpperCase());
                }
            }
        }
        return sb.toString();
    }





你可能感兴趣的:(java导出excel)