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

相关推荐
葡萄城技术团队2 天前
从100秒到10秒的性能优化,你真的掌握 Excel 的使用技巧了吗?
excel
QQ3596773453 天前
ArcGIS Pro实现基于 Excel 表格批量创建标准地理数据库(GDB)——高效数据库建库解决方案
数据库·arcgis·excel
星空的资源小屋4 天前
Digital Clock 4,一款免费的个性化桌面数字时钟
stm32·单片机·嵌入式硬件·电脑·excel
揭老师高效办公5 天前
在Excel和WPS表格中批量删除数据区域的批注
excel·wps表格
我是zxb5 天前
EasyExcel:快速读写Excel的工具类
数据库·oracle·excel
辣香牛肉面5 天前
[Windows] 搜索文本2.6.2(从word、wps、excel、pdf和txt文件中查找文本的工具)
word·excel·wps·搜索文本
ljf88385 天前
Java导出复杂excel,自定义excel导出
java·开发语言·excel
tebukaopu1485 天前
json文件转excel
json·excel
shizidushu5 天前
How to work with merged cells in Excel with `openpyxl` in Python?
python·microsoft·excel·openpyxl
Eiceblue6 天前
使用 C# 设置 Excel 单元格格式
开发语言·后端·c#·.net·excel