Java_复杂Excel导入导出和转换

Excel常用的几种方法:POI,jxls,easypoi,easyexcel

史上最全的excel读写技术分享_烟花散尽13141的博客-CSDN博客_aftercelldispose

1:Easypoi实现导入导出excel:

EasyPoi实现excel文件导入导出_crysw的博客-CSDN博客_easypoi excel导入

public class OfficialExcelUtil {
    public static void exportExcel(List list, Class pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) {
        ExportParams exportParams = new ExportParams();
        exportParams.setCreateHeadRows(isCreateHeader);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        if (workbook != null) ;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            try {
                throw new Exception(e.getMessage());
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }

}

Easypoi支持二进制导出图片

//实体字段
private byte[] oilCostRecord;

//下载后下来后转成字节
Result result = systemMinioProvider.downloadBase64(record.getRecord());
String split = result.getData().toString().split("base64,")[1];
byte[] decode = Base64.getDecoder().decode(split);
ExcelVo.setRecord(decode);

//调用导出
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);

2:Easyexcel实现导入导出excel:

EasyExcel官方文档

​​​​​​EasyExcel导入存在合并单元格的Excel_easyexcel导入合并单元格_我可能在扯淡的博客-CSDN博客

EasyExcel模板导出(行和列自动合并)_easyexcel 模板导出_Lzfnemo2009的博客-CSDN博客(牛逼)

EasyExcel导出合并单元格_easyexcel合并单元格_是一个菜鸟程序员啊的博客-CSDN博客

https://www.jb51.net/article/258822.htm EasyExcel工具读取Excel空数据行问题的解决办法

EasyExcel实现Excel文件导入导出_crysw的博客-CSDN博客_easyexcel

3:Java导出复杂数据到Excel(apache poi)(使用反射获取bean的get方法获取值)

https://blog.csdn.net/hjf_1291030386/article/details/75288220

4:HuTool工具包导入导出excel

Hutool参考文档

@Slf4j
@Component
public class ExcelExportHandler {
    /**
     * 下载Excel格式的数据
     *
     * @param response response
     * @param fileName 文件名(支持中文)
     * @param data     待下载的数据
     * @param map    key 为对象中的字段名  value 为表头展示的文字
     * @param       数据泛型
     */
    public static   void export(HttpServletResponse response, String fileName,
                                   List data, LinkedHashMap map) throws IOException {
        ExcelWriter writer = ExcelUtil.getWriter();
        // 数据为空,打印表头
        if (CollectionUtil.isEmpty(data)){
            writer.writeHeadRow(map.values());
        }        
        for(String str : map.keySet()){
            writer.addHeaderAlias(str,map.get(str));
        }
        writer.setColumnWidth(-1, 30);
        writer.setRowHeight(-1, 20);
        writer.write(data,true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");
        fileName= URLEncoder.encode(fileName,"UTF-8");
        response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");
        ServletOutputStream outputStream= response.getOutputStream();
        writer.flush(outputStream,true);
        outputStream.close();
        writer.close();
    }

}

5:若依导出使用Field.get(object)获取bean字段的值(apache poi)(Field.get(obj)取得对象obj的Field属性值)

(8条消息) Springboot导出EXCEl方法(若依实例)_星光榴莲鸡的博客-CSDN博客_springboot导出excel表格

6:Java根据模板导出excel(jxls.poi) easyexcel easypoi jxls都支持模板导出

问题1:

使用fileName = URLEncoder.encode(fileName, "UTF-8");偶尔不生效时,使用下面方法解决中文乱码问题。

JAVA输出EXCEL文件名中文乱码解决
response.setCharacterEncoding(“UTF-8”);
String filename = new String( “明细记录”.getBytes(“utf-8” ), “ISO8859-1” );
response.setHeader(“Content-disposition”, “attachment; filename=”+filename+".xls");// 设定输出文件头
response.setContentType(“application/msexcel”);// 定义输出类型
————————————————
版权声明:本文为CSDN博主「weixin_43972670」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43972670/article/details/120525642

问题2:

说法1:
注意导出的controller中,不能有返回值,改为void,防止IO流冲突。

说法2:
controller添加返回值json后报错。
结论:下载文件时controller方法只能返回null或者void。
原因:因为response已经输出了,无论你返回不返回都没啥用了

解决方法:导出的接口调用返回null
    @GetMapping("/exportIMO/year")
    public ResponseResult exportImo(HttpServletResponse response, @RequestParam Integer year){
        Date firstDate = DateUtil.getYearFirst(year);
        Date lastDate = DateUtil.getAfterDay(DateUtil.getYearLast(year));
        if (!iImoVoyageService.isBeginPeriodOil(firstDate, lastDate)){
            return ResponseResult.fail("查询的开始时间和结束时间的期初存油均不能为空");
        }else {
            try {
                iImoVoyageService.exportIMOVoyageData(response,firstDate,lastDate,"1");
                return null;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

(8条消息) Java实现根据excel模板导出数据(适合导出结构复杂的excel)_名字看着办的博客-CSDN博客_java根据模板导出excel

7:百万数据量时,导出操作

使用EasyExcel实现excel导出,支持百万大数据量导出-----超简单_easyexcel导出大量数据_MCP~的博客-CSDN博客

POI百万级大数据量EXCEL导出_poi大数据量导出excel_请叫我猿叔叔的博客-CSDN博客

项目中Excel导出大数据量记录解决方案及实战(POI,Hutools,EasyExcel)_蜀山雪松的博客-CSDN博客

8:Java实现Excel转PDF的两种方法(spire和jacob):

https://blog.csdn.net/weixin_44985880/article/details/124293130?ops_request_misc=&request_id=&biz_id=102&utm_term=java%20excel%E8%BD%AC%E6%8D%A2%E4%B8%BApdf&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-124293130.142^v11^control,157^v12^control&spm=1018.2226.3001.4187
 

9:Spire.XLS将 Excel 转换为 CSV或将CSV转换为Excel:

https://blog.csdn.net/Augenstern__zyx/article/details/121607454?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165363799616782388025854%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=165363799616782388025854&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-5-121607454-null-null.142^v11^control,157^v12^control&utm_term=java+excel%E8%BD%AC%E6%8D%A2%E4%B8%BAcsv&spm=1018.2226.3001.4187
 

你可能感兴趣的:(Java基础知识,蓝桥杯,职场和发展)