POI导出EXCEL

前端点击a标签,a标签href设置为到处请求地址即可使用POI导出EXCEL
try{
HSSFWorkbook wb = new HSSFWorkbook();
//设置表头样式
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中格式
//字体样式
HSSFFont font = wb.createFont();
font.setFontName(“宋体”);
font.setFontHeightInPoints((short)12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font);
String dlData = getUsedExamineStyle();
String[] heads = {“考核人编号”,”考核人”,”被考核人编号”,”被考核人”,”考核类型” + dlData};

    HSSFSheet sheet = wb.createSheet("Sheet1");

    HSSFRow headRow = sheet.createRow(0);
    for(int i=0; i < heads.length; i++){
        HSSFCell cell = headRow.createCell((short)i);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellStyle(style);
        cell.setCellValue(heads[i]);
    }
    sheet.setColumnWidth((short)0, (short)5120);
    sheet.setColumnWidth((short)1, (short)6120);
    sheet.setColumnWidth((short)2, (short)5120);
    sheet.setColumnWidth((short)3, (short)6120);
    sheet.setColumnWidth((short)4, (short)15120);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    wb.write(baos);
    byte[] bytes = baos.toByteArray();
    InputStream is = new ByteArrayInputStream(bytes);

    response.reset();
    response.setHeader("Content-Disposition", "attachment;filename="+new String("考核计划导入模板.xls".getBytes("UTF-8"), "ISO-8859-1"));  
    response.setContentType("application/x-download;charset=UTF-8");  
    response.setCharacterEncoding("UTF-8"); 

    OutputStream sos = response.getOutputStream();
    wb.write(sos);
    sos.close();
}catch(Exception ex){
    ex.printStackTrace();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值