Browse Source

导出功能修改

wangfeng 6 years ago
parent
commit
4120e4f007

+ 114 - 10
common/src/main/java/com/diagbot/util/ExportBeanExcelUtil.java

@@ -1,18 +1,9 @@
 package com.diagbot.util;
 
-import com.diagbot.dto.RespDTO;
-import com.diagbot.exception.CommonErrorCode;
-import com.diagbot.exception.CommonException;
-import org.apache.poi.hssf.usermodel.HSSFCell;
-import org.apache.poi.hssf.usermodel.HSSFCellStyle;
-import org.apache.poi.hssf.usermodel.HSSFRow;
-import org.apache.poi.hssf.usermodel.HSSFSheet;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.usermodel.HorizontalAlignment;
-
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -22,6 +13,19 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
+
+import com.diagbot.dto.RespDTO;
+import com.diagbot.exception.CommonErrorCode;
+import com.diagbot.exception.CommonException;
+
 @SuppressWarnings({ "deprecation" })
 public class ExportBeanExcelUtil<T> {
     private final String MSG_SUCCESS = "操作成功!";
@@ -147,4 +151,104 @@ public class ExportBeanExcelUtil<T> {
 
         // return resultMode;
     }
+    
+	public  void exportExcelNew(String title, List<String> headersName, List<String> headersId, List<T> dtoList,
+			HttpServletResponse response) {
+		/* (一)表头--标题栏 */
+		Map<Integer, String> headersNameMap = new HashMap<>();
+		int key = 0;
+		for (int i = 0; i < headersName.size(); i++) {
+			if (!headersName.get(i).equals(null)) {
+				headersNameMap.put(key, headersName.get(i));
+				key++;
+			}
+		}
+		/* (二)字段 */
+		Map<Integer, String> titleFieldMap = new HashMap<>();
+		int value = 0;
+		for (int i = 0; i < headersId.size(); i++) {
+			if (!headersId.get(i).equals(null)) {
+				titleFieldMap.put(value, headersId.get(i));
+				value++;
+			}
+		}
+		/* (三)声明一个工作薄:包括构建工作簿、表格、样式 */
+		HSSFWorkbook wb = new HSSFWorkbook();
+		HSSFSheet sheet = wb.createSheet(title);
+		sheet.setDefaultColumnWidth((short) 15);
+		// 生成一个样式
+		HSSFCellStyle style = wb.createCellStyle();
+		HSSFRow row = sheet.createRow(0);
+		style.setAlignment(HorizontalAlignment.CENTER);
+		HSSFCell cell;
+		Collection c = headersNameMap.values();// 拿到表格所有标题的value的集合
+		Iterator<String> it = c.iterator();// 表格标题的迭代器
+		/* (四)导出数据:包括导出标题栏以及内容栏 */
+		// 根据选择的字段生成表头
+		short size = 0;
+		while (it.hasNext()) {
+			cell = row.createCell(size);
+			cell.setCellValue(it.next().toString());
+			cell.setCellStyle(style);
+			size++;
+		}
+		// 表格标题一行的字段的集合
+		Collection zdC = titleFieldMap.values();
+		Iterator<T> labIt = dtoList.iterator();// 总记录的迭代器
+		int zdRow = 0;// 列序号
+		while (labIt.hasNext()) {// 记录的迭代器,遍历总记录
+			int zdCell = 0;
+			zdRow++;
+			row = sheet.createRow(zdRow);
+			T l = (T) labIt.next();
+			// 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
+			Field[] fields = l.getClass().getDeclaredFields();// 获得JavaBean全部属性
+			for (short i = 0; i < fields.length; i++) {// 遍历属性,比对
+				Field field = fields[i];
+				String fieldName = field.getName();// 属性名
+				Iterator<String> zdIt = zdC.iterator();// 一条字段的集合的迭代器
+				while (zdIt.hasNext()) {// 遍历要导出的字段集合
+					if (zdIt.next().equals(fieldName)) {// 比对JavaBean的属性名,一致就写入,不一致就丢弃
+						String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);// 拿到属性的get方法
+						Class tCls = l.getClass();// 拿到JavaBean对象
+						try {
+							Method getMethod = tCls.getMethod(getMethodName, new Class[] {});// 通过JavaBean对象拿到该属性的get方法,从而进行操控
+							Object val = getMethod.invoke(l, new Object[] {});// 操控该对象属性的get方法,从而拿到属性值
+							String textVal = null;
+							if (val != null) {
+								textVal = String.valueOf(val);// 转化成String
+							} else {
+								textVal = null;
+							}
+							row.createCell((short) zdCell).setCellValue(textVal);// 写进excel对象
+							zdCell++;
+						} catch (SecurityException e) {
+							e.printStackTrace();
+						} catch (IllegalArgumentException e) {
+							e.printStackTrace();
+						} catch (NoSuchMethodException e) {
+							e.printStackTrace();
+						} catch (IllegalAccessException e) {
+							e.printStackTrace();
+						} catch (InvocationTargetException e) {
+							e.printStackTrace();
+						}
+					}
+				}
+			}
+		}
+		try {
+             String fileNameNew = String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";
+                    String headStr = "attachment; filename=\"" + fileNameNew + "\"";
+                    response.reset();
+                    response.setContentType("APPLICATION/OCTET-STREAM");
+                    response.setHeader("Content-Disposition", headStr);
+                    OutputStream out = response.getOutputStream();
+                    wb.write(out);
+                } catch (IOException e) {
+                    // TODO Auto-generated catch block
+                 e.printStackTrace();
+               } 
+		 
+	}
 }

+ 0 - 1
user-service/src/main/java/com/diagbot/config/ResourceServerConfigurer.java

@@ -34,7 +34,6 @@ public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
                 .antMatchers("/userver/getSmsWithResetPassword").permitAll()
                 .antMatchers("/userver/verifySmsVerification").permitAll()
                 .antMatchers("/getUserEnumsData").permitAll()
-                .antMatchers("/userInfo/getUserInfoPag").permitAll()
                 .antMatchers("/user/getPermission").permitAll()
                 .antMatchers("/user/index").permitAll()
                 .antMatchers("/user/getUserAndOrg").permitAll()

+ 0 - 1
user-service/src/main/java/com/diagbot/config/security/UrlAccessDecisionManager.java

@@ -78,7 +78,6 @@ public class UrlAccessDecisionManager implements AccessDecisionManager {
                 || matchers("/userver/getSmsWithResetPassword", request)
                 || matchers("/userver/verifySmsVerification", request)
                 || matchers("/getUserEnumsData", request)
-                || matchers("/userInfo/getUserInfoPag", request)
                 || matchers("/user/getPermission", request)
                 || matchers("/user/resetPassword", request)
                 || matchers("/user/index", request)

+ 9 - 7
user-service/src/main/java/com/diagbot/facade/UserFacade.java

@@ -66,6 +66,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.servlet.http.HttpServletResponse;
+
 /**
  * @Description: 用户业务层
  * @author: gaodm
@@ -628,9 +630,10 @@ public class UserFacade extends UserServiceImpl {
 
     /**
      * @param userExportVO
+     * @return 
      * @return 导出Excel文件
      */
-    public RespDTO export(UserExportVO userExportVO) {
+    public  void export(UserExportVO userExportVO,HttpServletResponse response) {
         Map<String, Object> userMap = new HashMap<String, Object>();
         userMap.put("startTime", userExportVO.getStartTime());
         userMap.put("endTime", userExportVO.getEndTime());
@@ -695,14 +698,13 @@ public class UserFacade extends UserServiceImpl {
                     AuthStatusEnum.getName(userInfo.getAutStatus())));
         }
         //response.setContentType("text/html;charset=UTF-8");
-        String filePath = userExportVO.getFilePath();//文件路径
+/*        String filePath = userExportVO.getFilePath();//文件路径
         String fileName = userExportVO.getFileName();//文件名
         ExportBeanExcelUtil ex = new ExportBeanExcelUtil();
-        RespDTO resul = ex.exportExcel("测试POI导出EXCEL文档", listName, listId, list, filePath, fileName);
-
-        return resul;
-
-
+        RespDTO resul = ex.exportExcel("测试POI导出EXCEL文档", listName, listId, list, filePath, fileName);*/
+        response.setContentType("text/html;charset=UTF-8");
+        ExportBeanExcelUtil ex = new ExportBeanExcelUtil();
+        ex.exportExcelNew("用户信息详情", listName, listId, list,response);
     }
 
     /**

+ 0 - 2
user-service/src/main/java/com/diagbot/vo/UserExportVO.java

@@ -14,8 +14,6 @@ public class UserExportVO {
 
     private String startTime;
     private String endTime;
-    private String filePath;//文件路径
-    private String fileName;//文件名
 	private String userName;// 用户名
 	private String orgName;//机构名称
 	private Integer autStatus;//认证状态

+ 5 - 5
user-service/src/main/java/com/diagbot/web/ReportFormController.java

@@ -50,11 +50,11 @@ public class ReportFormController {
 
     @ApiOperation(value = "导出用户信息和机构信息:[by:wangfeng]", notes = "根据时间 导出用户信息和机构信息")
     @PostMapping("/exportUserInfo")
-    @SysLogger("exportUserInfo")
-    @Transactional
-    public RespDTO export(@RequestBody UserExportVO userExportVO)
+    //@SysLogger("exportUserInfo")
+    @ResponseBody
+    public  void export(@RequestBody UserExportVO userExportVO,HttpServletRequest request,HttpServletResponse response)
             throws Exception {
-        RespDTO resul = userFacade.export(userExportVO);
-        return resul;
+         userFacade.export(userExportVO, response);
+        
     }
 }

+ 2 - 2
user-service/src/main/resources/mapper/UserMapper.xml

@@ -274,13 +274,13 @@
         u.is_deleted = 'N'
         AND u.TYPE = "0"
         <if test="userExport.orgName != null">
-            AND org.name LIKE CONCAT('%', #{userInfo.orgName}, '%')
+            AND org.name LIKE CONCAT('%', #{userExport.orgName}, '%')
         </if>
         <if test="userExport.userName != null">
             AND u.username LIKE CONCAT('%', #{userExport.userName}, '%')
         </if>
         <if test="userExport.autStatus != null">
-            AND aut.status = #{userInfo.autStatus}
+            AND aut.status = #{userExport.autStatus}
         </if>
         <if test="userExport.startTime != null and userExport.endTime != null">
             AND u.gmt_create BETWEEN #{userExport.startTime} and #{userExport.endTime}