whmr_soft
2017-03-13 10:51:10
spring mvc使用apache poi导出excel下载的demo代码
这是一个使用poi导出数据到excel实现下载的demo,所有就不连数据库取数据了。
1.首先创建一个TestData类(测试数据的类,这里可以换成从数据库查询的数据)
public class TestData { public static List<Map> getData() { Map map = Maps.newHashMap(); map.put("id", 1); map.put("name", "aaa"); map.put("addr", "北京"); Map map2 = Maps.newHashMap(); map2.put("id", 2); map2.put("name", "bbb"); map2.put("addr", "武汉"); Map map3 = Maps.newHashMap(); map3.put("id", 3); map3.put("name", "ccc"); map3.put("addr", "上海"); List<Map> list = Lists.newArrayList(); list.add(map); list.add(map2); list.add(map3); return list; } }
2.写一个创建excel工作簿的工具类ExcelUtil(使用的apache的poi)
public class ExcelUtil { /** * @param list 数据集合 * @param keys 数据集合里map对象的key * @param columnNames 导出excel里面的列名 * @return */ public static Workbook createWorkbook(List<Map> list,String [] keys,String [] columnNames) { // 创建excel工作簿 XSSFWorkbook wb = new XSSFWorkbook(); // 创建第一个sheet(页),并命名 XSSFSheet sheet = wb.createSheet(); // 手动设置列宽。第一个参数表示要为第几列设;,第二个参数表示列的宽度,n为列高的像素数。 for(int i=0;i<keys.length;i++){ sheet.setColumnWidth((short) i, (short) (35.7 * 150)); } // 创建第一行 XSSFRow row = sheet.createRow((short) 0); // 创建两种单元格格式 XSSFCellStyle cs = wb.createCellStyle(); XSSFCellStyle cs2 = wb.createCellStyle(); // 创建两种字体 Font f = wb.createFont(); Font f2 = wb.createFont(); // 创建第一种字体样式(用于列名) f.setFontHeightInPoints((short) 10); f.setColor(IndexedColors.BLACK.getIndex()); f.setBoldweight(Font.BOLDWEIGHT_BOLD); // 创建第二种字体样式(用于值) f2.setFontHeightInPoints((short) 10); f2.setColor(IndexedColors.BLACK.getIndex()); // 设置第一种单元格的样式(用于列名) cs.setFont(f); cs.setBorderLeft(CellStyle.BORDER_THIN); cs.setBorderRight(CellStyle.BORDER_THIN); cs.setBorderTop(CellStyle.BORDER_THIN); cs.setBorderBottom(CellStyle.BORDER_THIN); cs.setAlignment(CellStyle.ALIGN_CENTER); // 设置第二种单元格的样式(用于值) cs2.setFont(f2); cs2.setBorderLeft(CellStyle.BORDER_THIN); cs2.setBorderRight(CellStyle.BORDER_THIN); cs2.setBorderTop(CellStyle.BORDER_THIN); cs2.setBorderBottom(CellStyle.BORDER_THIN); cs2.setAlignment(CellStyle.ALIGN_CENTER); //设置列名 for(int i=0;i<columnNames.length;i++){ XSSFCell cell = row.createCell(i); cell.setCellValue(columnNames[i]); cell.setCellStyle(cs); } //设置每行每列的值 for (short i = 0; i < list.size(); i++) { // Row 行,Cell 方格 , Row 和 Cell 都是从0开始计数的 // 创建一行,在页sheet上 XSSFRow row1 = sheet.createRow((short) i+1); // 在row行上创建一个方格 for(short j=0;j<keys.length;j++){ XSSFCell cell = row1.createCell(j); cell.setCellValue(list.get(i).get(keys[j]) == null?" ": list.get(i).get(keys[j]).toString()); cell.setCellStyle(cs2); } } return wb; } }
3.写一个TestController这里面调用上面两步的代码来实现导出数据到excel,并且实现下载
@Controller @RequestMapping("/test") public class TestController { @RequestMapping(value = "download") public String download(HttpServletRequest request, HttpServletResponse response) throws IOException { try { String fileName = "excel文件名"; String columnNames[] = { "ID", "姓名", "地址" };// 列名 String keys[] = { "id", "name", "addr"};// map中的key ByteArrayOutputStream os = new ByteArrayOutputStream(); //调用工具类创建excel工作簿 ExcelUtil.createWorkbook(TestData.getData(), keys, columnNames).write(os); byte[] content = os.toByteArray(); InputStream is = new ByteArrayInputStream(content); // 设置response参数,可以打开下载页面 response.reset(); response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xlsx").getBytes(), "iso-8859-1")); OutputStream out = response.getOutputStream(); byte[] b = new byte[2048]; int length; while ((length = is.read(b)) > 0) { out.write(b, 0, length); } // 关闭。 os.flush(); os.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } return null; } }
4.导出后下载的excel
}
评论

黄贵胜
2020年12月24日
暂无贡献等级
wkc LV21
2020年6月28日
linchonghui LV2
2018年12月15日
香酥鸡腿 LV2
2018年9月25日
wangleiabcd LV2
2018年9月14日
潇洒明天 LV2
2018年8月23日
kaseikun006 LV5
2018年6月30日
zhangyongzai LV1
2018年6月18日
anglesice LV4
2018年3月22日
pengboss LV12
2018年2月2日