Java POI 导出EXCEL

来源:互联网 发布:editplus php代码提示 编辑:程序博客网 时间:2024/05/12 07:07

1、创建xls格式,首部

public HSSFSheet createSheet_balanceStatistics(HSSFWorkbook busWorkBook, String areaname) {        //创建一个工作表        HSSFSheet sheet = busWorkBook.createSheet(areaname);        //设置每一列的默认宽度        sheet.setColumnWidth(0, 10*256);        sheet.setColumnWidth(1, 25*256);        sheet.setColumnWidth(2, 35*256);        //冻结第一列        sheet.createFreezePane( 0, 1, 0, 1 );        //设置表头的样式        Region region = new Region(1, (short)0, 2, (short)8);//合并单元格,从(1,0)到(2,8)        sheet.addMergedRegion(region);        //创建一个excel表样式(居中)        HSSFCellStyle cellStyle = busWorkBook.createCellStyle();        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);        // 生成一个字体          HSSFFont font = busWorkBook.createFont();          font.setFontHeightInPoints((short) 16);          font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);          // 把字体应用到当前的样式  ,标题格式        cellStyle.setFont(font);                cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);        HSSFRow rowTitle = sheet.createRow(1);        HSSFCell titleCell = rowTitle.createCell(0);        titleCell.setCellValue("短信邮件发送记录");             titleCell.setCellStyle(cellStyle);        HSSFFont font2 = busWorkBook.createFont();  //各项数据小标题格式        font2.setFontHeightInPoints((short) 13);          font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);         HSSFCellStyle cellStyle2 = busWorkBook.createCellStyle();        cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);        cellStyle2.setFont(font2);        //因为没找到一行设置格式,就一个单元格一个的设了,还好不多        HSSFRow row = sheet.createRow(4);        HSSFCell areaCell = row.createCell(0);        areaCell.setCellValue("地区");        areaCell.setCellStyle(cellStyle2);//设置格式        HSSFCell recDateCell = row.createCell(1);        recDateCell.setCellValue("时间");        recDateCell.setCellStyle(cellStyle2);        HSSFCell subjectCell = row.createCell(2);        subjectCell.setCellValue("主题");        subjectCell.setCellStyle(cellStyle2);        return sheet;    }

导出excel配置

private void ExportAlarmNoticeInfo(HttpServletRequest request,            HttpServletResponse response) throws IOException {        response.reset();//避免可能出现未关闭的getWriter()        response.setContentType("text/html;charset=utf-8");        /*开始时间*/        String sTime = request.getParameter("sTime");        /*结束时间*/        String eTime = request.getParameter("eTime");        String area = request.getParameter("area");        if(area != null&&!area.equals("")){            area = new String(area.getBytes("ISO8859-1"),"utf-8");        }else{            area = "";        }        try {            List<NoticeInfo> noticeList = emailAndSMSService.getAlarmNoticeCon(sTime, eTime, area);            ByteArrayOutputStream buffer = new ByteArrayOutputStream();//获取数据            //创建一个excel表            HSSFWorkbook busWorkBook = new HSSFWorkbook();            HSSFSheet sheet = createSheet_balanceStatistics(busWorkBook,"邮件配置");//excel文件名            int size = 5;            for(NoticeInfo ni:noticeList){//循环遍历                HSSFRow row = sheet.createRow(size++);                HSSFCell areaCell = row.createCell(0);//地区                areaCell.setCellValue(ni.getArea());                HSSFCell recDateCell = row.createCell(1);//时间                recDateCell.setCellValue(ni.getRecDate());                HSSFCell subjectCell = row.createCell(2);                subjectCell.setCellValue(ni.getSubject());//主题            }            size+=3;            HSSFRow row = sheet.createRow(size);            HSSFCell othCell = row.createCell(0);//地区            if(area == null||"".equals(area)){                area = "全部地区";            }            othCell.setCellValue("附录:查询的地方是:"+area+",查询时间范围:从"+sTime+"到"+eTime);            // 生成并设置另一个样式  (附录)            HSSFCellStyle style2 = busWorkBook.createCellStyle();              // 生成另一个字体              HSSFFont font2 = busWorkBook.createFont();              font2.setFontHeightInPoints((short) 13);              font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);              // 把字体应用到当前的样式              style2.setFont(font2);              othCell.setCellStyle(style2); //正式开始调用浏览器下载excel            try {                busWorkBook.write(buffer);                String fileName = sTime+"至"+eTime+" "+area+"邮件配置.xls";                response.setContentType("application/msexcel");                byte[] bytes = buffer.toByteArray();                try {                    response.setHeader(                            "Content-Disposition",                            "attachment;filename="                                    + new String(fileName.getBytes("GBK"),"iso8859_1"));                    response.addHeader("Content-Length", "" + bytes.length);                } catch (UnsupportedEncodingException e1) {                    e1.printStackTrace();                }                ServletOutputStream sos = null;                sos = response.getOutputStream();                sos.write(bytes);                sos.flush();            } catch (IOException e) {                logger.info("写入流失败");            }        } catch (Exception e) {            e.printStackTrace();        }    }

over了

注意:

1.如果报这个错误

 getWriter() has already been called for this response(异常解决)

通过response.reset(); 刷新可能存在一些未关闭的getWriter().
我已经在代码写出来了,再写一遍是为了提醒自己不要再犯这样的错误

原创粉丝点击