@ApiOperation(value="导出简单表头的excel文件(即表头不存在合并行,合并列的情况)") @ApiImplicitParams({@ApiImplicitParam(name = "title",value = "表头的中文名称数组"), @ApiImplicitParam(name = "val",value = "表头对应的数据的key值"), @ApiImplicitParam(name = "data",value = "要导出的数据集(也可以传List,循环的时候改下即可)") @ApiImplicitParam(name = "response",value = "返回流") @ApiImplicitParam(name = "tableName",value = "文件名")}) public static void commconExcel(String[] title,String[] val,JSONArray data,HttpServletResponse response,String tableName) { XSSFWorkbook book = new XSSFWorkbook(); XSSFSheet sheet = book.createSheet("项目信息"); XSSFRow headrow = sheet.createRow(0); OutputStream out = null; try { //填表头 for(int i=0;i<title.length;i++) { headrow.createCell(i).setCellValue(title[i]); } //获取数据信息 //填写数据 for(int i=0;i<data.size();i++) { XSSFRow row = sheet.createRow(i+1); //获取每行数据 Map<String,Object> map = data.getJSONObject(i); //遍历map获取所有value String[] xmxx = new String[title.length]; int x = 0; for(String key: val) { if(map.get(key)!=null && !map.get(key).toString().equals("")) { String value = map.get(key).toString(); xmxx[x]=value; x++; }else { xmxx[x]=""; x++; } } //填数据 for(int j=0;j<title.length;j++) { // Set<String> keySet = map.keySet(); row.createCell(j).setCellValue(xmxx[j]); } } //设置返回头信息 response.addHeader("content-disposition", "attachment;filename=" + java.net.URLEncoder.encode(tableName+".xlsx", "utf-8")); out = response.getOutputStream(); book.write(out); out.flush(); } catch (IOException e) { e.printStackTrace(); }finally { if(out!=null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }