最近发现包下有一个excel导入导出工具类,非常好用,对于普通的场景及业务逻辑用起来非常方便 。
记录一下;
位置:
该工具类在 com.包下 。
特性:
该工具类对外提供了:读取和下载excel方法 。
package com.gu.alibabaexcel.utils;import com.alibaba.excel.EasyExcel;import com.alibaba.excel.EasyExcelFactory;import com.alibaba.excel.support.ExcelTypeEnum;import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;import com.alibaba.excel.write.handler.WriteHandler;import com.alibaba.excel.write.metadata.style.WriteCellStyle;import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;import org.apache.poi.ss.usermodel.HorizontalAlignment;import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.net.URLEncoder;import java.util.List;import java.util.Objects;/*** @author Mr.Gu* @date 2020/10/21 9:16* @function : excel工具类**/public class ExcelUtil {/*** 读取excel数据* @param excel excel文件* @param cls 映射的实体类*/public static List readExcel(MultipartFile excel, Class cls) throws IOException {return readExcel(excel,cls,1);}/*** 读取excel数据* @param excel excel文件* @param cls 映射的实体类* @param headRowNumber 从第几行开始读0代表从第一行开始 1为第二行*/public static List readExcel(MultipartFile excel,Class cls,int headRowNumber) throws IOException {return EasyExcelFactory.read(excel.getInputStream()).sheet().headRowNumber(headRowNumber).head(cls).doReadSync();}/*** 下载excel*/public static void download(HttpServletResponse response, Class cls, List data, String fileName, String sheetName) throws Exception {download(response, cls, data, fileName, sheetName,null);}public static void download(HttpServletResponse response, Class cls, List data, String fileName, String sheetName, WriteHandler handler) throws Exception {response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("utf-8");response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");//表头样式WriteCellStyle headWriteCellStyle = new WriteCellStyle();//设置表头居中对齐headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//内容样式WriteCellStyle contentWriteCellStyle = new WriteCellStyle();//设置内容靠左对齐contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);ExcelWriterSheetBuilder builder = EasyExcel.write(response.getOutputStream(), cls).excelType(ExcelTypeEnum.XLSX).sheet(sheetName).registerWriteHandler(horizontalCellStyleStrategy);if(Objects.nonNull(handler)){builder.registerWriteHandler(handler);}builder.doWrite(data);}}
使用方式:
/*** 导出excel* @param response*/@GetMapping("/exportExcel")public void exportExcel(HttpServletResponse response) {List
/*** 导入excel* @param file* @return*/@PostMapping("/import")public String importExcel(@RequestBody MultipartFile file) {if (file != null) {try {List
- 能看的下去的长篇韩娱小说,韩娱小说 长篇
- 原来清朝皇后住的坤宁宫并不舒服:每天都杀猪
- 运动型蓝牙耳机推荐:骨传导运动耳机
- 李白斗酒诗百篇,他的酒量到底有多大?
- 推荐几个实用的Excel小技巧给你
- 幼教说课|高频篇目《我们的祖国真大》教案设计+说课稿 中国之最说课反思
- 不说不知道,原来家乡这么牛!这30个“安徽之最”,你老家占了几个? 六安是不是历史名城之最
- 胜利双打球拍推荐,推荐胜利羽毛球拍
- 背包品牌十大排名,户外背包品牌排名推荐
- 乳晕原来是粉色,乳晕怎么变粉色