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

Flutter中如何从Excel读取的列表中提取指定索引的目标数据?

实现方案

你可以直接对已经拿到的row对象做二次遍历处理,提取对应下标的元素即可,注意先做长度判断避免索引越界报错。

完整修改后的代码示例

Future readExcel() async {
        var excelfileDetails = new Word();
    
        var file = excelPath;
        var bytes = File(file).readAsBytesSync();
        var excel = Excel.decodeBytes(bytes);
        print('${excel}');
    
        print(excel.tables.keys); //sheet Name
        print(excel.tables[excel.tables.keys.first]!.maxCols);
        print(excel.tables[excel.tables.keys.first]!.maxRows);
        final row = excel.tables[excel.tables.keys.first]!.rows
            .map((e) => e.map((e) => e!.value).toList());
        
        // 新增的处理逻辑:提取每行索引为1和3的元素
        final processedRows = row.map((singleRow) {
          // 行长度不足4的话会抛出索引越界错误,提前判断更稳妥
          if (singleRow.length >= 4) {
            return [singleRow[1], singleRow[3]];
          }
          // 长度不符合要求的行返回空列表,你也可以按需修改为默认值
          return <dynamic>[];
        }).toList();

        // 输出处理后的结果
        print(processedRows);
}

可选优化

如果你示例中的第一行[Dil1, Kelime1, Dil2, Kelme2]是表头不需要提取,只要在遍历前调用skip(1)跳过第一个元素即可:

final processedRows = row.skip(1).map((singleRow) {
  // 同上处理逻辑
}).toList();

处理后的数据将完全符合你的预期,你给出的示例输入处理后输出为:[[Kelime1, Kelme2], [ad, name], [öğrenci, student], [teach, öğren]],如果跳过表头则从第二个元素开始提取。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 02:09:01