Flutter - 将数据从 firestore 转换为 Excel 工作表

Jag*_*ish 2 excel dart flutter

如何在 Android 应用程序和 flutter web 上将 Firestore 中的数据转换为 Excel 工作表?任何回复将不胜感激...提前致谢!

50_*_*ing 10

Flutter 的 Excel 库

您可以在此处查看文档。

现在,当 jay 要求详细解释如何检索数据并将其存储在 Excel 工作表中时,

第一步,

var excel = Excel.createExcel();           //create an excel sheet
Sheet sheetObject = excel['SheetName'];    //create an sheet object  
Run Code Online (Sandbox Code Playgroud)

第二步,在Excel工作表中写入命令,其中A是列id,1是行。

 var cell = sheetObject.cell(CellIndex.indexByString("A1"));
 cell.value = 8; // Insert value to selected cell;
Run Code Online (Sandbox Code Playgroud)

第三步,从firebase获取数据

QuerySnapshot _qs =
        await _notificationRef.where('language', isEqualTo: selectedLang).get(); // Lets say I have some collection where I need to get some documents with specific language

//This loop will iterate in all of the documents in the collection

for (int i = 0; i < _qs.docs.length; i++) {
 string data = _qs.docs[i].data()['names'];  //Where name is the field value in the document and i is the index of the document.
}  
});   
Run Code Online (Sandbox Code Playgroud)

现在如果我们结合第二步和第三步

QuerySnapshot _qs =
        await _notificationRef.where('language', isEqualTo: selectedLang).get(); 

for (int i = 0; i < _qs.docs.length; i++) {
 var cell = sheetObject.cell(CellIndex.indexByString('A${i+1}'));   //i+1 means when the loop iterates every time it will write values in new row, e.g A1, A2, ...
 cell.value =  _qs.docs[i].data()['names']; // Insert value to selected cell;
}  
});   
Run Code Online (Sandbox Code Playgroud)

完成数据部分后,您可以保存文件,

      // Save the Changes in file

      excel.encode().then((onValue) {
        File(join("Path_to_destination/excel.xlsx"))
        ..createSync(recursive: true)
        ..writeAsBytesSync(onValue);
    });
Run Code Online (Sandbox Code Playgroud)

完成保存后,您可以选择任何库来与其他人共享您的工作表,

通常,此库会要求您提供一个文件或文件路径,您可以使用最后一个代码块轻松提供该文件或文件路径,该代码块解释了我将文件路径传递给 join 方法的位置