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

相关推荐
啊哈哈哈哈啊有10 小时前
导出树形结构,excel
java·windows·excel
Dxy123931021614 小时前
Elasticsearch 聚合入门:像 Excel 透视表一样分析数据
elasticsearch·excel
一位代码14 小时前
Excel | .xls 与 .xlsx 文件的区别及使用 Excel 打开 CSV 文件出现乱码的解决办法
excel
CodeCraft Studio15 小时前
Excel处理控件Aspose.Cells教程:使用C#在Excel中创建树状图
前端·c#·excel·aspose·c# excel库·excel树状图·excel sdk
悟能不能悟16 小时前
vue导出excel文件
前端·vue.js·excel
CodeCraft Studio17 小时前
国产化Excel开发组件Spire.XLS教程:以Python编程方式在Excel中高亮重复值
开发语言·python·excel·spire.xls·excel自动化·excel高亮重复值·python处理excel
喝汽水的猫^1 天前
Java实现Excel 导出(多 Sheet、复杂格式)
java·excel
小费的部落1 天前
Excel 在Sheet3中 匹配Sheet1的A列和Sheet2的A列并处理空内容
java·前端·excel
缺点内向1 天前
如何在 C# 中重命名 Excel 工作表并设置标签颜色
开发语言·c#·excel
wtsolutions2 天前
Sheet-to-Doc: Automate Document Generation with Excel Data and Word Templates
word·excel·wtsolutions