我们在开发中经常要将数据导入成Excel表格供展示,也需要解析Excel中的数据,官方提供的api操作太麻烦,这边封装了一个Excel工具类,可以很轻松的实现Excel的操作
首先加入依赖
<dependencies><!-- EasyPoi 基本库依赖 --><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>4.2.0</version></dependency><!-- EasyPoi 注解库依赖 --><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>4.2.0</version></dependency><!-- EasyPoi Web 库依赖 --><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>4.2.0</version></dependency>
</dependencies>
介绍如下
EasyPoi 是一个用于处理 Excel 文件的 Java 库,它提供了各种功能,包括读取和写入 Excel 文件,支持不同的 Excel 格式,以及用于生成
Excel 报表的工具;这些库依赖项用于在 Java 项目中引入 EasyPoi 的相关功能,以便于可以在应用程序中轻松地处理 Excel 文件;通过添加这些
依赖,可以在项目中使用 EasyPoi 提供的功能来操作 Excel 文件
接下来实现工具类
public class ExcelUtils {/*** 根据数据生成excel表格,并将其写入HttpServletResponse。** @param name Excel文件名* @param title 表格标题* @param entityClass 数据对象的类* @param list 数据列表* @param httpServletResponse 用于响应的HttpServletResponse对象*/public static void toExcelList(String name, String title, Class<?> entityClass, List<?> list, HttpServletResponse httpServletResponse) {// 创建导出参数对象ExportParams exportParams = new ExportParams();// 设置导出参数的工作表名称为传入的titleexportParams.setSheetName(title); // 使用EasyPoi库来导出Excel,生成一个Workbook对象Workbook workbook = ExcelExportUtil.exportExcel(exportParams, entityClass, list);// 响应生成的Excel文件给客户端responseExcel(name, httpServletResponse, workbook);}/*** 将生成的Excel表格保存到本地文件。** @param filepath 目标文件路径* @param title Excel标题* @param entityClass 数据对象的类* @param list 数据列表* @throws Exception 如果保存文件时发生异常*/public static void fileExcel(String filepath, String title, Class<?> entityClass, List<?> list) throws Exception {// 创建导出参数对象ExportParams params = new ExportParams();// 设置Excel文件类型为XLSXparams.setType(ExcelType.XSSF); // 设置工作表名称为传入的titleparams.setSheetName(title); try {// 使用EasyPoi库来导出Excel,生成一个Workbook对象Workbook workbook = ExcelExportUtil.exportExcel(params, entityClass, list);// 将生成的Excel写入到文件FileOutputStream fileOutputStream = new FileOutputStream(filepath);// 将Workbook对象写入到文件输出流中workbook.write(fileOutputStream); } catch (Exception e) {// 抛出运行时异常throw new Exception("Excel导出失败: " + e.getMessage(), e); // 抛出异常并包含错误消息}}/*** 解析Excel表格为List对象** @param multipartFile 文件上传对象* @param clazz 返回值类型的Class对象* @return 表格数据的List* @throws Exception 如果解析过程中发生异常*/public static List<?> excelToList(MultipartFile multipartFile, Class<?> clazz) throws Exception {// 创建Excel导入参数对象ImportParams params = new ImportParams();// 设置Excel文件中标题所占的行数,setTitleRows(0)表示Excel文件中没有标题行params.setTitleRows(0);// 设置Excel文件中表头(列名)所占的行数,setHeadRows(1)表示第一行包含了表头信息params.setHeadRows(1);// 使用EasyExcel工具类导入Excel数据并返回List对象return ExcelImportUtil.importExcel(multipartFile.getInputStream(), clazz, params);}/*** 将Excel表格写入响应体输出** @param name Excel文件名* @param httpServletResponse 响应体对象* @param workbook Excel工作簿对象*/public static void responseExcel(String name, HttpServletResponse httpServletResponse, Workbook workbook) {try {// 对文件名进行URL编码,处理可能的特殊字符name = URLEncoder.encode(name, "UTF-8");// 设置响应的字符编码为UTF-8,以确保处理中文字符正确httpServletResponse.setCharacterEncoding("UTF-8");// 设置响应的Content-Type头,指定响应内容的类型为二进制流httpServletResponse.setHeader("Content-Type", "application/octet-stream");// 设置Content-Disposition头,提示浏览器以附件形式下载文件,并指定下载的文件名httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + name);// 将Workbook对象的内容写入HttpServletResponse的输出流,实现文件下载workbook.write(httpServletResponse.getOutputStream());} catch (UnsupportedEncodingException e) {// 抛出运行时异常,处理不支持的字符编码异常throw new RuntimeException(e);} catch (IOException e) {// 抛出运行时异常,处理文件输出异常throw new RuntimeException(e);}}
}
这个工具类可以直接copy下来,这样就能简单的实现Excel的功能