【自用】EasyExcel 表格模板下载及导入
生活随笔
收集整理的這篇文章主要介紹了
【自用】EasyExcel 表格模板下载及导入
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
工具類
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List;import javax.servlet.http.HttpServletResponse;import com.alibaba.excel.EasyExcel; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener;/*** 高性能處理Excel工具類**/ public class EasyExcelUtil {/*** 使用 模型 來讀取Excel* * @param inputStream Excel的輸入流* @param clazz 模型的類* @return 返回 模型 的列表*/public static <T> List<T> readExcel(InputStream inputStream, Class<T> clazz) {ModelExcelListener<T> listener = new ModelExcelListener<T>();EasyExcel.read(inputStream, clazz, listener).sheet().doRead();return listener.getDatas();}/*** 使用 模型 來導出到WEB* * @param response web的響應* @param data 要寫入的以 模型 為單位的數據* @param fileName 配置Excel的表名* @param sheetName 配置Excel的頁簽名* @param clazz 模型的類* @throws IOException */public static <T> void writeExcel(HttpServletResponse response, List<T> data, Class<T> clazz, String fileName, String sheetName) throws IOException {// 這里注意 有同學反應使用swagger 會導致各種問題,請直接用瀏覽器或者用postmanresponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setCharacterEncoding("utf-8");// 這里URLEncoder.encode可以防止中文亂碼 當然和easyexcel沒有關系fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(data);}/*** 使用 模型 來寫入Excel* <br/>注意,如果是web輸出流,需要設置頭* * @param outputStream Excel的輸出流* @param data 要寫入的以 模型 為單位的數據* @param sheetName 配置Excel的表名字* @param clazz 模型的類*/public static <T> void writeExcel(OutputStream outputStream, List<T> data, Class<T> clazz, String sheetName) {EasyExcel.write(outputStream, clazz).sheet(sheetName).doWrite(data);}/*** 模型 解析監聽器*/private static class ModelExcelListener<T> extends AnalysisEventListener<T> {private List<T> datas = new ArrayList<>();@Overridepublic void invoke(T object, AnalysisContext context) {datas.add(object);}@Overridepublic void doAfterAllAnalysed(AnalysisContext context) {}public List<T> getDatas() {return datas;}}}模板下載、導出實體定義(實體可以公用)
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelProperty; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import lombok.Data;@Data @ExcelIgnoreUnannotated //默認情況下Java類中的所有屬性都添加讀寫,在類上面加入@ExcelIgnoreUnannotated注解,加入這個注解后只有加了@ExcelProperty才會參與讀寫。 public class 實體類名 {@TableId(value = "主鍵字段", type = IdType.INPUT)private String xxx;@ExcelProperty(value = "表格模板名字")@TableField(value = "數據庫屬性對應字段")private String xxxx; }模板下載Controller(接口直接調用工具類即可)
import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.shiro.util.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.ArrayList; import java.util.List;/*** @author */ @RestController @RequestMapping("/") @Api(value = "", tags = "") public class Controller {/*** 下載模板*/@PostMapping(value = "/")@ApiOperation(value = "下載導入模板", notes = "下載導入模板")public ResultData downLoadOwnerVehicleForm(HttpServletResponse response) {List<實體> xxxxx= new ArrayList<>();try {EasyExcelUtil.writeExcel(response, ownerVehicleForms, 實體.class, "模板名字", "sheet1");} catch (IOException e) {e.printStackTrace();}return new ResultData<>(ResultData.SUCCESS);}}表格導入Controller
@PostMapping(value = "/")@ApiOperation(value = "", notes = "")public ResultData xxxxx(@RequestParam MultipartFile file) {byte[] byteArr;try {byteArr = file.getBytes();} catch (IOException e) {throw new xxxException("500", "文件格式轉換異常");}InputStream inputStream = new ByteArrayInputStream(byteArr);List<實體> xxxxxx= null;xxxxxx = EasyExcelUtil.readExcel(inputStream, 實體.class);if (CollectionUtils.isEmpty(xxxxxx)) {throw new xxxException("500", "導入數據不可為空");}//此處拿到導入數據 進入業務層處理return new ResultData<>(ResultData.SUCCESS);}。。。。業務層省略 根據實際需求處理
可以添加事務回滾
@Transactional(rollbackFor = xxxException.class)
不知道有沒有遺漏的 自用就這樣 隨機應變吧
總結
以上是生活随笔為你收集整理的【自用】EasyExcel 表格模板下载及导入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NANK南卡降噪耳机和华为耳机哪个好?5
- 下一篇: c 连接mysql批量存储数据库_C语言