You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现跳过Excel文件首行读取内容?附Java实现代码示例

Java读取Excel跳过表头实现方案

核心实现逻辑

你当前代码中使用的ExcelWriter.read()方法第二个参数为读取的起始行,只需要根据你工具类的行计数规则调整该参数即可实现跳过第一行表头:

  • 若工具类行号从1开始计数(第一行序号为1):将第二个参数改为2,即可从第二行有效数据开始读取
  • 若工具类行号从0开始计数(第一行序号为0):将第二个参数改为1,即可跳过表头行

通用实现思路(适配所有Excel操作工具)

如果后续换用POI、EasyExcel等其他Excel处理工具,也可以通过行索引判断实现跳过表头:

  • 遍历Excel行数据时,判断当前行索引是否为表头行序号,匹配则直接continue跳过不处理即可

修改后参考代码

public void doConvertFileAmazon(String pathFolderRead, String pathFolderWrite, String bpcnum) throws Exception {
        Collection<File> listFiles = FileUtils.listFiles(new File(pathFolderRead), new String[] { "xlsx" }, false);
        for (File file : listFiles) {
            StringBuffer sb = new StringBuffer();
            // 仅修改read方法的第二个参数为对应起始行即可,此处以行号从1开始计数为例,传2跳过第一行表头
            List<String[]> listRow = ExcelWriter.read(file, 2);
            for (String[] cols : listRow) {
                sb.append("BEV01;SRN;;" + bpcnum + ";SL;" + cols[3] + ";" + cols[0] + ";" + cols[1] + ";" + cols[13]
                        + ";" + cols[17] + ";1;;\r\n");
            }
            String filename = FilenameUtils.getBaseName(file.getName());
            FileUtils.writeStringToFile(new File(pathFolderWrite, filename + ".csv"), sb.toString(), "UTF-8");
        }
    }

注意事项

  • 提前确认所用Excel工具类的行计数规则,避免起始行参数传错导致漏读数据
  • 若表头占多行,直接将起始行号设置为「表头行数+1」即可适配

内容的提问来源于stack exchange,提问作者gian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 12:06:04