用Java实现下载功能,并保存到excel表格中
2017-11-29 11:30
337 查看
一,通过POI接口,将从数据库中查出的信息以excel的形式存储
1.首先写一个Utils类public class DownPOIUtils { /** * * @param response:响应对象,类型是HttpServletResponse * @param map:要封装的信息的map容器,其中key为Student,value为String类型的,在这里代表分数 * @throws Exception:代表异常对象 */ public static void downPoi(HttpServletResponse response, Map<Student, String> map) throws Exception { String fname = "detial" + getTimeStamp();// Excel文件名 OutputStream os = response.getOutputStream();// 取得输出流 response.reset();// 清空输出流 response.setHeader("Content-disposition", "attachment; filename=" + fname + ".xls"); // 设定输出文件头,该方法有两个参数,分别表示应答头的名字和值。 response.setContentType("application/msexcel"); try { new DownPOIUtils().new POIS().createFixationSheet(os, map); } catch (Exception e) { e.printStackTrace(); } } /** * 该方法用来产生一个时间字符串(即:时间戳) * @return */ public static String getTimeStamp() { SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd hh:MM:ss"); Date date = new Date(); return dateFormat.format(date); }2.编写POI代码
class POIS { public void createFixationSheet(OutputStream os, Map<Student, String> student) throws Exception { // 创建工作薄 HSSFWorkbook wb = new HSSFWorkbook(); // 在工作薄上建一张工作表 HSSFSheet sheet = wb.createSheet(); HSSFRow row = sheet.createRow((short) 0); sheet.createFreezePane(0, 1); cteateCell(wb, row, (short) 0, "学号"); cteateCell(wb, row, (short) 1, "姓名"); cteateCell(wb, row, (short) 2, "性别"); cteateCell(wb, row, (short) 3, "班级"); cteateCell(wb, row, (short) 4, "分数"); int i = 0; Set<Student> keySet = student.keySet(); Iterator<Student> iterator = keySet.iterator(); while (iterator.hasNext()) { HSSFRow rowi = sheet.createRow((short) (++i)); Student student2 = iterator.next(); for (int j = 0; j < 4; j++) { cteateCell(wb, rowi, (short) 0, student2.getId()); cteateCell(wb, rowi, (short) 1, student2.getName()); cteateCell(wb, rowi, (short) 2, student2.getSex()); cteateCell(wb, rowi, (short) 3, student2.getGrade()); cteateCell(wb, rowi, (short) 4, student.get(student2)); } } wb.write(os); os.flush(); os.close(); System.out.println("文件生成"); } @SuppressWarnings("deprecation") private void cteateCell(HSSFWorkbook wb, HSSFRow row, short col, String val) { HSSFCell cell = row.createCell(col); cell.setCellValue(val); HSSFCellStyle cellstyle = wb.createCellStyle(); cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION); cell.setCellStyle(cellstyle); } }
3.效果如下图:
4.打开可以看到数据已经写入:
5.总结:
在本次代码中,可以看到调用下载的方法的参数中,数据是被放到Map容器中的,其中的key是一个对象,value是一个分数类型是String。当然封装数据的参数也可以List类型的,只不过要看我们实际需求,如果从数据库中查出的信息不是一张表,那么就不能用一个实体对象来封装,所以可以使用map。在本次代码中,student作为map 的key,尽量在Student类中重写一下hashcode方法和equals方法,保证map的键值得唯一性。
相关文章推荐
- java实现文件下载功能,自动弹出保存窗口
- java实现文件下载功能,自动弹出保存窗口
- JAVA 实现下载功能 保存为EXCEL格式
- jsp页面导出用jxl方式导出数据到excel表格并下载保存(第二种直接用java类实现)
- 用java实现上传下载功能
- JAVA应用XFire框架来实现WebServie的大文件传输功能之一(下载)
- C#实现下载功能(好东东就应该保存学习)
- 利用Java实现的下载功能 不支持断点续传
- Java实现FTP的上传下载功能
- Java实现FTP上传下载功能
- java resteasy restful webservice教程(六):实现文件下载功能实例文章来源:爱上123 原文地址:http://www.ishang123.com/jishubowen/
- java 如何实现文件下载时,弹出文件保存窗口
- 把文件以二进制格式或base64字符串形式保存到数据库中,并实现下载功能源码
- Java实现FTP上传下载功能
- C#实现下载功能(好东东就应该保存学习)
- 如何在Java程序中实现FTP的上传下载功能
- Java实现FTP上传下载功能
- JavaWeb中struts2实现文件上传下载功能实例解析
- Java实现文件下载功能
- 用java实现上传下载功能