从Excel表逐列读取Flutter数据

quhf5bfb  于 2023-02-05  发布在  Flutter
关注(0)|答案(1)|浏览(296)

我正在使用excel: ^2.0.1从用File Picker选取的文件中读取数据。我可以通过以下方式读取每个单元格

void getExcelFile() async {
    FilePickerResult pickedFile = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: ['xlsx'],
      allowMultiple: false,
    );

    if (pickedFile != null) {
      var file = pickedFile.paths.single;
      var bytes = await File(file).readAsBytes();
      Excel excel = await compute(parseExcelFile, bytes);
      for (var table in excel.tables.keys) {
        print(table);
        print(excel.tables[table].maxCols);
        print(excel.tables[table].maxRows);
        Sheet sheetObject = excel[table];
        for (int row = 0; row < sheetObject.maxRows; row++) {
          sheetObject.row(row).forEach((cell) {
            var val = cell.value; //  Value stored in the particular cell
            print("cell value is: " + val.toString());
          });
        }
      }
    }
  }

但数据是按行的,我想读取按列的数据

在这里,我想添加到我的类每个名称及其相应的电子邮件,用户可以添加自己的excel文件,可以有几个标题在该文件中,但我只需要名称和电子邮件

class ExcelSheetData {
  var name;
  var email;

  ExcelSheetData({this.name, this.email});
}

更新(2023年2月3日):

我能够找到周围的名称和电子邮件的列索引,然后我能够得到名称和电子邮件从每一行从特定的索引,下面是代码,如果它帮助别人:

Future<List<ExcelSheetData>> getExcelFile(BuildContext context, String name, String email) async {
    FilePickerResult pickedFile = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: ['xlsx'],
      allowMultiple: false,
    );
    List<ExcelSheetData> excelList = [];
    int nameIndex;
    int emailIndex;

    if (pickedFile != null) {
      setAddMembersLoadingTrue();
      var file = pickedFile.paths.single;
      var bytes = await File(file).readAsBytes();
      Excel excel = await compute(parseExcelFile, bytes);
      for (var table in excel.tables.keys) {
        for (var row in excel.tables[table].rows) {
          // name variable is for Name of Column Heading for Name
          if (row?.any((element) => element?.value?.toString() == name) ?? false) {
            Data data = row?.firstWhere((element) => element?.value?.toString()?.toLowerCase() == name);
            nameIndex = data.colIndex;
          }
          // email variable is for Name of Column Heading for Email
          if (row?.any((element) => element?.value?.toString() == email) ?? false) {
            Data data = row?.firstWhere((element) => element?.value?.toString()?.toLowerCase() == email);
            emailIndex = data.colIndex;
          }
          if (nameIndex != null && emailIndex != null) {
            if (row[nameIndex]?.value.toString().toLowerCase() != name.toLowerCase() && row[emailIndex]?.value.toString().toLowerCase() != email.toLowerCase())
              excelList.add(
                ExcelSheetData(
                  name: row[nameIndex]?.value.toString(),
                  email: row[emailIndex]?.value.toString(),
                ),
              );
          }
        }
      }
      setAddMembersLoadingFalse();
      return excelList;
    }
    return null;
  }
djp7away

djp7away1#

如果你想按列读取,那么:然后从第二个循环修改它

for (int column = 0; column < sheetObject.maxColumns; column++) {
      sheetObject.column(column).forEach((cell) {
        var val = cell.value; //  Value stored in the particular cell
        print("cell value is: " + val.toString());
      });

相关问题