标签:null 注意 地方 生成 用户名 throw exception pom.xml result
开发工具:eclipse
jar包管理:maven
框架使用:springboot+poi+thymeleaf
1.首先导入pom.xml
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.16</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.16</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.16</version> </dependency>
2.前端查询数据库
展示要导出的列表
3.点击导出通过js提交页面对应的标签值
例如:function export(){
window.location.href = resqURL;
}
注意!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
页面不能通过ajax访问url。
必须通过页面window.location.href = resqURL;属性来访问
我在这个地方吃了好大的亏,,,唉
4.后台到controller层接受参数定制表头初始化数据
@RequestMapping("export") public ResponseEntity<byte[]> userExcel() throws Exception { /*所有表头*/ Map<String,Map<String,String>> titleMaps=new LinkedHashMap<String,Map<String,String>>(); /*所有数据*/ Map<String,List<Map>> results=new HashMap<String,List<Map>>(); /*每个表格的列标题*/ Map<String,String> title=new LinkedHashMap<String, String>(); title.put("uid", "Id"); title.put("username", "用户名"); title.put("password", "密码"); //行初始化为空值 List<Map> nullRows=new ArrayList<Map>(); Map nullMap=new HashMap(); nullMap.put("uid", ""); nullMap.put("username", ""); nullMap.put("password", ""); nullRows.add(nullMap); //文件名 String filename="用户信息"; titleMaps.put(filename, title); //从数据库查询数据 List<Map> collentList=userService.userExcel(); results.put(filename, collentList); ByteArrayOutputStream out = new ByteArrayOutputStream(); ExcelUtilNew.exportExcelDocument(titleMaps, results,out); System.out.println("我来了"); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition", "attchement;filename=" + URLEncoder.encode(filename, "utf-8")+".xlsx"); return new ResponseEntity<byte[]>(out.toByteArray(), headers, HttpStatus.CREATED); }
3.调用poi工具类导出excel
/** * 导出Excel文档 * * @throws FileNotFoundException */ public static void exportExcelDocument(Map<String, String> titleMap, List<Map> result, OutputStream out) throws Exception { Map<String, Map<String, String>> titleMaps = new HashMap<String, Map<String, String>>(); titleMaps.put("Sheet1", titleMap); Map<String, List<Map>> results = new HashMap<String, List<Map>>(); results.put("Sheet1", result); exportExcelDocument(titleMaps, results, out); } public static void exportExcelDocument(Map<String, Map<String, String>> titleMaps, Map<String, List<Map>> results, OutputStream out) throws Exception { // 声明一个工作薄 Workbook workbook = new HSSFWorkbook(); for (String s : titleMaps.keySet()) { Map<String, String> titleMap = titleMaps.get(s); List<Map> result = results.get(s); // 生成一个表格 Sheet sheet = workbook.createSheet((s == null || "".equals(s)) ? "未命名" : s); // 网格线 sheet.setDisplayGridlines(true); // 设置默认表宽 sheet.setDefaultColumnWidth(20); // 产生表格标题行 Row row = sheet.createRow(0); // 生成一个样式 CellStyle titleStyle = workbook.createCellStyle(); // 设置这些样式 titleStyle.setAlignment(CellStyle.ALIGN_CENTER); titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 生成一个字体 Font titleFont = workbook.createFont(); titleFont.setFontName("Arial"); titleFont.setFontHeightInPoints((short) 12); titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD); // 把字体应用到当前的样式 titleStyle.setFont(titleFont); int count = 0; for (String key : titleMap.keySet()) { Cell cell = row.createCell(count); cell.setCellStyle(titleStyle); cell.setCellValue(titleMap.get(key)); count++; } if (result != null) { for (int i = 0, iSize = result.size(); i < iSize; i++) { Map<String, Object> resultMap = result.get(i); // 生成一个字体 Font contentFont = workbook.createFont(); contentFont.setFontName("宋体"); contentFont.setFontHeightInPoints((short) 10); contentFont.setBoldweight(Font.BOLDWEIGHT_NORMAL); // 生成一个样式 CellStyle contentStyle = workbook.createCellStyle(); contentStyle.setAlignment(CellStyle.ALIGN_LEFT); contentStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); row = sheet.createRow(i + 1); // 把字体添加到样式中去 contentStyle.setFont(contentFont); count = 0; for (String key : titleMap.keySet()) { Cell cell = row.createCell(count); cell.setCellStyle(contentStyle); cell.setCellValue(String.valueOf(resultMap.get(key))); count++; } } } } workbook.write(out); out.flush(); out.close(); }
4.结束总结:
通过springmvc封装的下载实现ResponseEntity将字符串输出成文件下载。
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
标签:null 注意 地方 生成 用户名 throw exception pom.xml result
原文地址:https://www.cnblogs.com/DIVEY/p/10213312.html