当前位置:   article > 正文

hutool ExcelUtil导出excel、读取excel内容_excelutil.getwriter

excelutil.getwriter

ExcelUtil为操作Excel的工具类,包括封装的excel读取的ExcelReader对象,excel生成的ExcelWriter对象。

导出

ExcelUtil将Excel写出封装为ExcelWriter,原理为包装了Workbook对象,每次调用merge(合并单元格)或者write(写出数据)方法后只是将数据写入到Workbook,并不写出文件,只有调用flush或者close方法后才会真正写出文件。
由于机制原因,在写出结束后需要关闭ExcelWriter对象,调用close方法即可关闭,此时才会释放Workbook对象资源,否则带有数据的Workbook一直会常驻内存。

获取导出ExcelWriter对象ExcelUtil.getWriter()

getWriter()有几个重载方法

/**
* destFile 目标文件
* sheetName sheet表名
*/
getWriter(File destFile, String sheetName)  
/**
* destFilePath 目标文件路径
* sheetName sheet表名
*/
getWriter(String destFilePath, String sheetName)
/**
* destFile 目标文件
*/
getWriter(File destFile)
/**
* destFilePath 目标文件路径
*/
getWriter(String destFilePath)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

导出到固定磁盘位置

   @PostMapping("/export")
   public Result<String> exportData(@RequestBody List<Map<String, Object>> dataList) {
       if (CollUtil.isNotEmpty(dataList)) {
           // 通过hutool工具类创建writer,默认创建xls格式
           ExcelWriter writer = ExcelUtil.getWriter("E:\test\test.xls");
           //将list map数据写出文件
           writer.write(dataList, true);
           writer.close();
       } else {
           return ResultUtil.failure("表无数据!");
       }
   }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

以response流的方式输出

	@PostMapping("/exportData")
    public Result<String> exportDataForExcel(@RequestBody List<Map<String, Object>> dataList,HttpServletResponse response) throws IOException {
        if (CollUtil.isNotEmpty(dataList)) {
            // 通过工具类创建writer,默认创建xls格式
            ExcelWriter writer = ExcelUtil.getWriter();
            writer.write(dataList, true);
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
       		response.setHeader("Content-Disposition", "attachment;filename=test.xls");
            ServletOutputStream out = response.getOutputStream();
            writer.flush(out);
            writer.close();
            return null;
        } else {
            return ResultUtil.failure("表无数据!");
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

读取excel内容

读取Excel内容的封装,通过构造ExcelReader对象,指定被读取的Excel文件、流或工作簿,然后调用readXXX方法读取内容为指定格式。

读取excel内容的其他read方法

/**
* startRowIndex 起始行(包含,从0开始计数)
*/
read(int startRowIndex);
/**
* startRowIndex  起始行(包含,从0开始计数)
* endRowIndex    读取结束行(包含,从0开始计数)
*/
read(int startRowIndex, int endRowIndex);
/**
* startRowIndex 起始行(包含,从0开始计数)
* endRowIndex   结束行(包含,从0开始计数)
* aliasFirstLine 是否首行作为标题行转换别名
*/
read(int startRowIndex, int endRowIndex, boolean aliasFirstLine);
/**
* beanType  每行对应Bean的类型
*/
readAll(Class<T> beanType);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

读取为Map列表

	private List<Map<String,Object>> readerExcelInfo(String excelUrl){
        List<Map<String,Object>> result = new ArrayList<>();
        if(excelUrl.isEmpty()){
            return Collections.emptyList();
        }
        try {
            ExcelReader excelReader = ExcelUtil.getReader(new File(excelUrl));
            //读取为Map列表,默认第一行为标题行,Map中的key为标题,value为标题对应的单元格值。
            result = excelReader.readAll();
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/770553
推荐阅读
相关标签
  

闽ICP备14008679号