POI万能导出模板

匿名 (未验证) 提交于 2019-12-03 00:26:01
 /**      * POI导出公共方法      * @param obj      * @param className      * @param sheetName      * @param options      * @param methods      * @param response      * @throws Exception      */     public static void publicExport(Object obj, Class className, String sheetName, String[] options,String[] methods, HttpServletResponse response) throws Exception {         //导出数据为空,直接返回         List query = (List)obj;         if (query.size() == 0) {             return;         }         //获取导出数据的总条数         int countColumnNum = query.size();         //创建XSSFWorkbook文件对象         XSSFWorkbook book = null;         //管理员导出数据         book = new XSSFWorkbook();         //创建一个Name的新表         XSSFSheet sheet = book.createSheet(sheetName);         // 获取表的第一行         XSSFRow firstRow = sheet.createRow(0);         //创建表的第一行的每列的说明         //管理员导出权限         XSSFCell[] firstCells = new XSSFCell[options.length];         //给表的第一行的每一列赋值         for (int j = 0; j < options.length; j++) {             firstCells[j] = firstRow.createCell(j);             firstCells[j].setCellValue(new XSSFRichTextString(options[j]));         }         //拿出方法         List<Method> methodList = new ArrayList<>();         for(String method : methods){             methodList.add(className.getMethod("get"+firstUpper(method)));         }         //把表的第一列写好后,接下来从表的第二列开始把对应的值写入到文件里         for (int i = 0; i < countColumnNum; i++) {             //给execl创建一行             XSSFRow row = sheet.createRow(i + 1);             //取出数据             Object object = query.get(i);             //循环给列赋值             for (int column = 0; column < options.length; column++) {                 //确认每一列对应的表的列                 for(int method = 0;method<methodList.size(); method++){                     XSSFCell cell = row.createCell(method);                     cell.setCellValue(methodList.get(method).invoke(object)==null?"":methodList.get(method).invoke(object).toString());                 }             }         }         //写一个try catch捕捉异常(response获取输出流)         OutputStream os = null;         try {             String filename= new String("全流程".getBytes("utf-8"), "ISO_8859_1");             response.setHeader("Content-Disposition", "attachment;filename="+filename+".xlsx");             response.setContentType("application");             response.setContentType("application/vnd.ms-excel;charset=UTF-8");             os = response.getOutputStream();             book.write(os);         } catch (IOException e) {             System.out.println("IO流异常");         } finally {             try {                 os.close();             } catch (IOException e) {                 System.out.println("关闭IO流异常");             }         }     }       public static String firstUpper(String string) {         char[] charArray = string.toCharArray();         charArray[0] -= 32;         return String.valueOf(charArray);     }

转载请标明出处:POI万能导出模板
文章来源: POI万能导出模板
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!