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().
我已经在代码写出来了,再写一遍是为了提醒自己不要再犯这样的错误
阅读全文
0 0
- java poi 导出excel
- Java POI导出excel
- Java POI 导出Excel
- java poi 导出excel
- java POI导出Excel
- JAVA POI 导出excel
- JAVA POI 导出excel
- java poi导出excel
- Java POI 导出EXCEL
- Java POI导出Excel
- java 导出到excel----POI
- java excel poi 导入导出
- java poi 导出成excel
- POI批量导出Excel JAVA
- java操作poi导出Excel
- java的excel导出[poi]
- java excel poi导出问题
- java poi导出excel文件
- UITableViewCell中嵌套UICollectionView
- RAC环境下修改字符集
- c primer plus 练习一
- ffmpeg图片转视频设置-r丢帧问题
- 【Linux内核】timer子系统
- Java POI 导出EXCEL
- spring的事务有几种方式,谈谈spring事务的隔离级别和传播行为
- ③看视频NDK学习之C常用函数
- iOS 打电话弹出太慢
- partition-list Java code
- 二叉树删除节点
- Leetcode477. 计算海明距离
- android 3.0 提示找不到@android:windowEnterAnimation
- C-Free编译器[C/C++集成开发环境]