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();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章