POI导出数据库数据到excel
2016-04-26 11:31
357 查看
废话就不说了,直接上代码。
public class TestPoi{ /** * * */ private InputStream istream; private String fileName; public String exportSatistics() { try { list = orderService.getExportCount(map); String userAgent = this.getCurrentOperator().getUserAgent();//自己封装的获取使用的浏览器 // 乱码 if (fileName == null) fileName = ""; String name = "统计";//导出文件名称 //处理在不同浏览器导出文件乱码 if (null != userAgent) { if (-1 != userAgent.indexOf("firefox")) { fileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(name.getBytes("UTF-8"))))+ "?="; } else if (-1 != userAgent.indexOf("chrome")) { fileName = new String(name.getBytes(), "ISO8859-1"); } else { fileName = URLEncoder.encode(name, "UTF-8"); fileName = fileName.replace("+", "%20"); } } fileName = fileName + new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss").format(new Date()) + ".xls"; this.createCountExcel(list, fileName); } catch (Exception e) { addActionError("导出文件异常!"); flag = false; e.printStackTrace(); return ERROR; } return "filedownload"; } /** * * */ private InputStream createCountExcel(List<OrderStatisticVO> list, String fileName) { // 第①步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第②步,在webbook中添加一个sheet,对应Excel文件中的sheet String name = "商户订单统计报表"; HSSFSheet sheet = wb.createSheet(name); // 指定合并区域 CellRangeAddress cellRangeAddress = new CellRangeAddress(0, (short) 0,0, (short) 25); sheet.addMergedRegion(cellRangeAddress); HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 指定单元格垂直居中对齐 // 设置单元格字体 HSSFFont font = wb.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setFontName("宋体"); font.setFontHeight((short) 300); cellStyle.setFont(font); // 第③步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); cell.setCellValue("商户订单统计报表"); cell.setCellStyle(cellStyle); row = sheet.createRow(1); cell = row.createCell(0); cell.setCellValue("日期"); cell = row.createCell(1); cell.setCellValue("订单号"); cell = row.createCell(2); cell.setCellValue("商品名称"); cell = row.createCell(3); cell.setCellValue("商品价格"); cell = row.createCell(4); cell.setCellValue("商品数量"); cell = row.createCell(5); cell.setCellValue("商品总价"); cell = row.createCell(6); cell.setCellValue("收货人"); cell = row.createCell(7); cell.setCellValue("收货地址"); cell = row.createCell(8); cell.setCellValue("订单状态"); // 第⑤步,写入实体数据 实际应用中这些数据从数据库得到 if (list != null && !list.isEmpty()) { Iterator it=list.iterator(); for (int i = 0; i < list.size(); i++) { row = sheet.createRow(i + 2); OrderStatisticVO vo = (OrderStatisticVO) it.next(); // 得到所有区域 sheet.getNumMergedRegions(); // 第④步,创建单元格,并设置值 if (vo != null) { cell = row.createCell(0); cell.setCellValue(vo.getOrderDate()); cell = row.createCell(1); cell.setCellValue(vo.getOrderId()); cell = row.createCell(2); cell.setCellValue(vo.getProductName()); cell= row.createCell(3); cell.setCellValue(vo.getProductPrice().doubleValue()); cell = row.createCell(4); cell.setCellValue(vo.getProductCount()); cell = row.createCell(5); cell.setCellValue(vo.getTotalPrice().doubleValue()); cell = row.createCell(6); cell.setCellValue(vo.getUserName()); cell = row.createCell(7); cell.setCellValue(vo.getShipAddress()); cell = row.createCell(8); cell.setCellValue(vo.getOrderState()); } } } // 第六步,将文件存到指定位置 try { fileName = new String(fileName.getBytes("utf-8"), "utf-8"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); wb.write(bos); istream = new ByteArrayInputStream(bos.toByteArray()); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return istream; } }
相关文章推荐
- mysql写不了中文数据
- mysql触发器笔记
- 关于查看MYSQL一张表索引的语句SHOW INDEX FROM tleName的分析
- 使用Elasticsearch-jdbc为MySQL数据库建立索引
- redis与spring结合使用
- MySQL数据文件介绍及win下存放位置
- Oracle default 函数记录(sys_guid......)
- MySQL启动参数(一) —— init_file
- Redis实战(五)
- MySQL解决SQL注入的另类方法详解
- mysql中索引的使用
- Redis实战(四)
- 在Java中使用Memcached(转)
- Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer'
- Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer'
- Redis是一种面向“key-value”类型数据的分布式NoSQL数据库系统,具有高性能、持久存储、适应高并发应用场景等优势。它虽然起步较晚,但发展却十分迅速。
- 数据库调优
- 数据库调优
- mysql5.6.x GTID主从复制配置
- 进阶级-多MYSQL数据库 同步数据