excel poi的titleRows 和 headRows含义

  • titleRows
    这个参数的意思是:excel标题占多少行,而不是第几行
  • headRows
    这个参数的意思是:excel表头占几行,而不是第几行(多行的意思是合并的行数)

比如有一个excel如下,1-2行是标题,3-4行是表头,他们都合并了。

那么代码可以这样设置:

java 复制代码
ImportParams params = new ImportParams();
//这个参数的意思是:excel标题占多少行,而不是第几行
params.setTitleRows(2);
//这个参数的意思是:excel表头占几行,而不是第几行(多行的意思是合并的行数)
params.setHeadRows(2);
//表格sheet数量
params.setSheetNum(1);

//从第几个表格开始读 默认0
//params.setStartSheetIndex(1);
//读取的表格sheet数量
//params.setSheetNum(1);

上传示例:

java 复制代码
    @RequestMapping("/importExcel")
    public String importExcel(@RequestParam("file") MultipartFile file, Model model) {
        List<Province> provinces = null;
        InputStream inputStream = null;
        try {
            ImportParams params = new ImportParams();
            //这个参数的意思是:excel标题占多少行,而不是第几行
            params.setTitleRows(2);
            //这个参数的意思是:excel表头占几行,而不是第几行(多行的意思是合并的行数)
            params.setHeadRows(2);
            //表格sheet数量
            params.setSheetNum(1);

            inputStream = file.getInputStream();
            provinces = ExcelImportUtil.importExcel(inputStream, Province.class, params);
            //把导入的信息输出到控制台
            for(Province pro:provinces) {
                System.out.println(pro);
            }
        } catch (Exception e) {
            model.addAttribute("msg","导入失败");
            e.printStackTrace();
        }finally {
            //这里注意释放资源:import org.apache.tomcat.util.http.fileupload.IOUtils;
            //否则会报错
            IOUtils.closeQuietly(inputStream);
        }
        model.addAttribute("msg","导入成功");
        return "importExcel";
    }

最后finally里注意释放资源,否则可能会报类似如下错误:

s.w.m.s.StandardServletMultipartResolver : Failed to perform cleanup of multipart items

java.io.UncheckedIOException: Cannot delete C:\Users\username\AppData\Local\Temp\tomcat.8080.xxxx\work\Tomcat\localhost\ROOT\upload_xxxx_00000000.tmp

相关推荐
Evan芙1 天前
用fping编写脚本扫描10.0.0.0/24网段在线主机
linux·运维·网络·excel
SamDeepThinking1 天前
基于CompletableFuture的主子任务并行处理架构实战:多渠道账单并发导入性能提升5倍的技术方案
java·后端·excel
SamDeepThinking1 天前
88MB Excel文件导致系统崩溃?看我如何将内存占用降低
java·excel
ChrisitineTX1 天前
警惕数据“陷阱”:Python 如何自动发现并清洗 Excel 中的异常值?
开发语言·python·excel
wtsolutions1 天前
Excel to JSON by WTSolutions 4.0.0 版本更新公告
json·excel·wps·插件·转换·加载项·wtsolutions
wtsolutions1 天前
Excel to JSON by WTSolutions 4.0.0 Update Announcement
json·excel·wps·addin·wtsolutions·conversion
癫狂的兔子2 天前
【Office】【Excel】常用函数公式总结
excel
毛飞龙2 天前
Excel迷你图:在单元格中嵌入趋势洞察
excel·迷你图·sparklines
Tatalaluola2 天前
Unity使用EPPlus读取写入表格
unity·c#·游戏引擎·excel
缺点内向2 天前
如何在 C# 中将 Excel 工作表拆分为多个窗格
开发语言·c#·.net·excel