将查询的结果以excel的格式导出
框架:springboot(没有什么意义,仅记录一下而已)
调用路径:controller→service
一 controller
@ApiOperation(value="导出",notes = "common") @GetMapping("output/exports") @ResponseBody @PreAuthorize("isAuthenticated()") Output elianDataStandardOutputsExport(HttpServletResponse response, Input input) throws IOException { String fileName="excel"; ExcelOutpt excelOutpt = service.data(input); response= OutputExcelUtil.responseHttp2007 (response,fileName); OutputExcelUtil.createWorkBook2007(excelOutpt).write(response.getOutputStream()); return new Output(); }
(注:input为查询条件,比如有个列表,我们根据条件得到查询结果,然后可以选择把只导出被查询出来的数据,没有进行查询时则默认导出所有数据)
二 service
public ExcelOutpt data(Input input) { //在数据库中根据条件把所有数据查询出来 String sql=" select * from table"; //查出来以后放到输出类中,输出类data是一个Data类型的集合 List<Data> data =repository.list(sql,input,Data.class); return dataEncap.transfer(data); }
三 实体转换类encap
public static ExcelOutpt transfer(List<Data> data) { //设置列名 String columnNames[] = {"id","name"}; //每一列对应的字段名。注意。字段名一定要和输入的参数中的data中的字段名一致 String[] keys = {"id","name"}; List<Object[]> list = new ArrayList<Object[]>(); //遍历传过来的集合中的数据 for (Data datas: data) { //new一个对象,把当前被遍历的对象的值一一按顺序赋值给objects Object[] objects = new Object[]{ data.getId(), data.getName() }; list.add(objects); } //创建一个输出文件类 ExcelOutpt excelOutpt = new ExcelOutpt(); //设置列数等参数 excelOutpt.setColumnNames(columnNames); excelOutpt.setKeys(keys); excelOutpt.setList(list); return excelOutpt; }
---------------------------------------------------
public static HttpServletResponse responseHttp2007(HttpServletResponse response,String fileName)throws IOException { response.reset(); response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xlsx").getBytes("gbk"), "iso-8859-1")); return response; }
------------------------------------------------------
/** * 创建excel文档, * [@param](http://my.oschina.net/u/2303379) list 数据 * @param * @param * */ public static Workbook createWorkBook2007(ExcelOutpt excelOutpt) throws IOException{ List<Object[]> Object=excelOutpt.getList (); String[] keys=excelOutpt.getKeys ();//keys list中map的key数组集合 String[] columnNames=excelOutpt.getColumnNames ();//columnNames excel的列名 ExcelOutpt excelOutpt List<Map<String, Object>> list = new ArrayList<Map<String, Object>> (); Map<String, Object> map = new HashMap<String, Object> (); map.put("sheetName", "sheet1"); list.add(map); for (int j = 0; j < Object.size(); j++) { Object[] project= Object.get(j); Map<String, Object> mapValue = new HashMap<String, Object>(); for(int k = 0; k < keys.length; k++){ mapValue.put(keys[k], project[k]); } list.add (mapValue); } // 创建excel工作簿 Workbook wb = new SXSSFWorkbook(100); Sheet sheet = wb.createSheet("sheetName");//建立新的sheet对象 int rowNo = 0; //总行号 // // 手动设置列宽。第一个参数表示要为第几列设;,第二个参数表示列的宽度,n为列高的像素数。 // for(int i=0;i<keys.length;i++){ // sheet.setColumnWidth((short) i, (short) (35.7 * 150)); // } // 创建两种单元格格式 CellStyle cs = wb.createCellStyle(); CellStyle 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<list.size();i++){ //打印300000条后切换到下个工作表,可根据需要自行拓展,2百万,3百万...数据一样操作,只要不超过1048576就可以 if(rowNo%300000==0){ sheet = wb.getSheetAt(rowNo/300000); //动态指定当前的工作表 } rowNo++; if(i==0){ Row row = sheet.createRow(i); for(int j=0;j<columnNames.length;j++){ Cell cell = row.createCell(j); cell.setCellValue(columnNames[j]); cell.setCellStyle(cs); } }else{ Row row1 = sheet.createRow(i); //新建行对象 //设置每行每列的值 for (int j = 0; j < keys.length; j++) { Cell 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; }