Browse Source

导出excel

xujunwei 1 năm trước cách đây
mục cha
commit
5f5dde1304

+ 6 - 0
framework-boot/pom.xml

@@ -133,6 +133,12 @@
             <artifactId>java-jwt</artifactId>
         </dependency>
 
+        <!--easyexcel导入导出-->
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>easyexcel</artifactId>
+        </dependency>
+
 
         <dependency>
             <groupId>org.codehaus.janino</groupId>

+ 8 - 0
framework-boot/src/main/java/com/mrxu/framework/boot/bean/BaseEntity.java

@@ -1,5 +1,6 @@
 package com.mrxu.framework.boot.bean;
 
+import com.alibaba.excel.annotation.ExcelIgnore;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
@@ -18,33 +19,40 @@ public class BaseEntity implements Serializable {
 
     @TableId(value = "id", type = IdType.AUTO)
     @ApiModelProperty(value = "id")
+    @ExcelIgnore
     private Integer id;
 
     @ApiModelProperty(value = "租户id")
     @TableField(exist=false) //需要的话,子类覆盖
+    @ExcelIgnore
     private String tenantId;
 
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
     @ApiModelProperty(value = "创建时间")
     @TableField("create_time")
+    @ExcelIgnore
     private Date createTime;
 
     @ApiModelProperty(value = "创建人")
     @TableField("create_person")
+    @ExcelIgnore
     private String createPerson;
 
     @ApiModelProperty(value = "状态")
     @TableField(exist=false) //需要的话,子类覆盖
+    @ExcelIgnore
     private Integer status;
 
     @ApiModelProperty(value = "更新时间")
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
     @TableField(exist=false) //需要的话,子类覆盖
+    @ExcelIgnore
     private Date updateTime;
 
     @ApiModelProperty(value = "更新人")
     @Size(max=64)
     @TableField(exist=false) //需要的话,子类覆盖
+    @ExcelIgnore
     private String updatePerson;
 
 }

+ 24 - 2
framework-boot/src/main/java/com/mrxu/framework/boot/web/BaseController.java

@@ -1,18 +1,20 @@
 package com.mrxu.framework.boot.web;
 
+import com.alibaba.excel.EasyExcel;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.mrxu.framework.boot.bean.LayuiPage;
 import com.mrxu.framework.boot.bean.PageResult;
 import com.mrxu.framework.boot.bean.ResponseObj;
 import com.mrxu.framework.common.util.BaseCode;
+import com.mrxu.framework.common.util.BusinessException;
 import com.mrxu.framework.common.util.StrFunc;
 import lombok.extern.slf4j.Slf4j;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
+import java.net.URLEncoder;
+import java.util.Collection;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
@@ -235,4 +237,24 @@ public class BaseController {
         }
     }
 
+    /**
+     * 导出excel
+     * @param response
+     * @param fileName
+     * @param clazz
+     * @param data
+     */
+    public void export(HttpServletResponse response,String fileName,Class<?> clazz,Collection<?> data) {
+        try {
+            response.setContentType("application/vnd.ms-excel");
+            response.setCharacterEncoding("utf-8");
+            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
+            EasyExcel.write(response.getOutputStream()).head(clazz).sheet("sheet1").doWrite(data);
+        }
+        catch (Exception e) {
+            log.error("导出Excel异常",e);
+            throw new BusinessException("导出Excel异常");
+        }
+    }
+
 }