下面是对poi导出excel的封装,稍微改动一下就能用于其它项目
pojo类见http://liudeh-009.iteye.com/blog/1847626的Student类
Excel导出类:
/** * * 类ExportBookOrderTool.java的实现描述:学生数据报表导出工具类 * @author liudeh_009 2012-11-5 下午02:34:03 */ public class ExportStudentDataTool { private HSSFWorkbook workbook; private HSSFSheet sheet; private HSSFCellStyle cellStyle; public ExportStudentDataTool(){ workbook = new HSSFWorkbook(); sheet = workbook.createSheet(); workbook.setSheetName(0, "学生数据报表"); Font font = workbook.createFont(); font.setColor(HSSFColor.RED.index); cellStyle = workbook.createCellStyle(); cellStyle.setFont(font); } public void exportExcel(List<Student> Students,HttpServletResponse response){ createSheetTitle(sheet); int sumCount = createSheetCotent(Students,sheet); totalToExport(sheet,sumCount); exportExcel2Client(workbook,response); } /** * 输出excel的标题行 * @param sheet */ private void createSheetTitle(HSSFSheet sheet){ int cellCount=0; HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(cellCount++,1); cell.setCellValue("ID");//忽视的列 cell = row.createCell(cellCount++,1); cell.setCellValue("姓名"); cell = row.createCell(cellCount++,1); cell.setCellValue("年龄"); } /** * 输出excel的主体内容 * @param Students * @param sheet * @param count * @param userId * @return */ private int createSheetCotent(List<Student> Students,HSSFSheet sheet){ int sumRow = 0; //共导出多少条 HSSFCell cell = null; HSSFRow row = null; for(Student student : Students){ if (student == null) { continue; } int cellCount=0; sumRow++; row = sheet.createRow(sumRow); cell = createCell(sumRow,row,cellCount++, 1); cell.setCellValue(student.getId());//忽视的列 cell = createCell(sumRow,row,cellCount++, 1); cell.setCellValue(student.getName());//预约流水号 cell = createCell(sumRow,row,cellCount++, 1); cell.setCellValue(student.getAge());//订单标号 } return sumRow; } /** * 偶数行字体增加颜色 * @param Student * @param row * @param location * @param size * @return */ @SuppressWarnings("deprecation") private HSSFCell createCell(int sumRow,HSSFRow row,int location, int size){ HSSFCell cell = row.createCell(location,size); if(sumRow%2==0){ cell.setCellStyle(cellStyle); } return cell; } /** * 总共行 * @param sheet * @param totalCount */ private void totalToExport(HSSFSheet sheet,int totalCount){ HSSFRow row = sheet.createRow(totalCount + 2); HSSFCell cell = row.createCell(1,1); cell.setCellValue("总共"); cell = row.createCell(2,1); cell.setCellValue(Integer.toString(totalCount)); } @SuppressWarnings("unused") private void exportExcel2Client(HSSFWorkbook workbook,HttpServletResponse response){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String nowDate = sdf.format(new Date()); String fileName = "attachment; filename=学生数据报表-"+nowDate+".xls"; OutputStream out = null; try { response.setHeader("Content-disposition",new String(fileName.getBytes("gbk"),"ISO-8859-1")); response.setContentType("application/msexcel;charset=GBK"); out = response.getOutputStream(); workbook.write(out); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); }finally{ if(out!=null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
调用方式非常简单.只需传学生的列表对象stList和response对象,如下:
ExportStudentDataTool exportStudentDataTool = new ExportStudentDataTool(); exportStudentDataTool.exportExcel(stList, response);
导出最终excel表格内容如下:
最后要注意,一个sheet的记录行数不能超过65535,超过这个数就要考虑创建多个sheet或者多个excel文件
相关推荐
用于导入导出Excel,方便好用 支持动态用流写至客户端
详解JAVA_POI导出EXCEL报表的操作(包括各种格式及样式的实现),最全面的JAVA_POI导出EXCEL报表操作。
NULL 博文链接:https://keep-going.iteye.com/blog/1907664
使用poi导出excel报表的程序,把项目导入到Eclipse或者myEclipse可以直接运行。
POI报表导出excel文件,POI包为3.6版本。里面导出的方法及设置样、合并单元格,合并多表头均有比较清晰的注释。希望对大家有用。
ExportExcel类为导出Excel报表通用类。 1.将源代码导入eclipse中 2.部署项目,启动tomcat服务器 3.web页面具体路径:http://localhost:80/poi 4.点击Read excel 2003 or 2007,可以读取2003或2007版Excel 5.点击...
包含POI 的Jar包,利用反射机制写的导出报表类,报表实体类,测试类,struts 2配置文件及Action中调用
Java利用poi导出excel表格
poi导出excel统计报表,有合并列的。 根据结构生成层级。poi导出excel统计报表,有合并列的。 根据结构生成层级。
poi导入导出excel生成报表
亲测 poi 导出word 导出excel项目 导入 myeclipse 可运行
poi jxl 生成EXCEL 报表 POI 用的JAR poi-3.6-20091214.jar jxl 用到的jar jxl-2.6.jar
利用poi技术生成excel报表和读取word文档内容
POI百万级大数据量EXCEL导出 - 请叫我猿叔叔的博客 - CSDN博客.htm
使用Apache POI的3.16版实现,Excel模板填充数据导出Excel报表
主要用于POI导出Excel,是用于用户手动导出。先由项目将excel表创建到webapp中然后用户将其下载到对应的网页上
POI导出Word、Excel、PDF,附有源代码
ExportExcel类为导出Excel报表通用类。 1.将源代码导入eclipse中 2.部署项目,启动tomcat服务器 3.web页面具体路径:http://localhost:80/poi 4.点击Read excel 2003 or 2007,可以读取2003或2007版Excel 5....
java 生成Excel,可根据模板格式生成 Excel打印专用 报表 这个是旧版本的 ,需要新版本的 可以联系我