2

How to convert data from Firestore Into an Excel sheet on both Android app and flutter web? Any response will be appreciated... thanks in advance!

Jagadish
  • 1,005
  • 11
  • 30
  • This question is a bit unclear; in general, to work with Firestore, you would write code using the Firestore SDK to read and write data. Once you written that code you can then do whatever you want that that data - save it to a file or show an image on the display or create a spreadsheet out of it. Do you have some code you've attempted and are having difficulty with? Please take a moment and review [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Jay May 24 '21 at 17:23
  • Yes, I am able to get the data but I am not getting how to arrange them in an excel sheet and send that to users' email as attachments both on the web and app. – Jagadish May 24 '21 at 18:03
  • So if you can get the data from Firebase, what's stopping you from creating an excel .xlsx file from that data? There are some great Q&A's here on SO about that - see [here](https://stackoverflow.com/questions/11082278/how-to-properly-assemble-a-valid-xlsx-file-from-its-internal-sub-components) and then there's actually an entire tag here on SO for [export to excel](https://stackoverflow.com/questions/tagged/export-to-excel) – Jay May 25 '21 at 15:19
  • I just don't have any idea how to make the excel sheet from the data, arange rows and columns and send that to a user theough email on both web and app...I just can't get an example also or where to start...am actually new to flutter – Jagadish May 25 '21 at 15:43
  • Focus on creating an xlsx file. Once you have that file you can email it, upload it or whatever you want. SO is not a code writing service, nor is it a service to provide suggestions on third party libraries; SO is for assistance with existing code. I just did an internet search on creating xlsx files and there are thousands of helpful links on how to do that. Do some research, write some code and when you get stuck, post a question about it! In fact, check out [this question](https://stackoverflow.com/questions/17830119/create-xls-file-programmatically) on how to do it in Swift. – Jay May 25 '21 at 15:53
  • Also, IMO, if this is just tabular data, consider writing it out as a .csv or .tab file. Then you are not stuck with a single, complex format, and it can be opened by a variety of other apps, uploaded, parsed etc much more easily. – Jay May 25 '21 at 15:57

1 Answers1

10

Excel library for flutter

You can go through the documentation here.

Now as jay asked to explain in detail how you are gonna retrive the data and store it in the excel sheet,

First step,

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

Second step, commands to write in excel sheet, where A is column id and 1 is row.

 var cell = sheetObject.cell(CellIndex.indexByString("A1"));
 cell.value = 8; // Insert value to selected cell;

Third step, getting data from 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.
}  
});   

Now if we combine second and third step

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;
}  
});   

Once you are done with the data part you can save the file,

      // Save the Changes in file

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

Once you are done with the saving you can choose any of the library to share your sheet to others,

Usually this libraries asks you to provide a file or file path which you can easily provide using the last code block explained where I passed file path to join method

David L
  • 1,134
  • 7
  • 35
  • Ok I know about this plugin but how to put the data from Firestore database in excels sheet? And will this plugin work on flutter web? And after creating the sheet can it be sent to user? Like sending through email or downloading it on device on both app and web? – Jagadish May 24 '21 at 16:00
  • Yes what will you need to share this sheet, first of all you will save this file in you system and then you can email it or whatever you want to do with it. Yes this library works on all platforms. – 50_Seconds _Of_Coding May 24 '21 at 16:04
  • And now for the firestore part, you can retreive the data run some *loops* and add the data as you require in the sheet. – 50_Seconds _Of_Coding May 24 '21 at 16:05
  • you can writeAsBytes method to save the file. – 50_Seconds _Of_Coding May 24 '21 at 16:08
  • Ok, but what does saving in system mean? Like do I have to save it on device first and then send on emai? If like that then how will I save the folder on system? Like it will have to be downloaded...and I want the automatically it gets email with the excel file as attachment to the user and not manually is it possible like that? – Jagadish May 24 '21 at 16:20
  • Two things: the OP is asking *I want to get data from Firestore database* and the link provided in this answer is unrelated to that part of the question. Secondly, link only answers are not always helpful as it's not clear how it addresses the question, but more importantly, if the link breaks, we would have no idea what it's in reference to. Please include some context in the answer of how it applies to the question. – Jay May 25 '21 at 15:08
  • @50_Seconds_Of_Coding yes please give some example code how the data can be aranged in rows nd columns and then sent to a user email on bith web and app without saving on device – Jagadish May 25 '21 at 15:43
  • @Jay I updated the answer as you required , but the OP needs an actual solution rather than going through the documentation and as you can see in my answer it is a literally full fledged logic. – 50_Seconds _Of_Coding May 25 '21 at 19:14
  • Well that's a brilliant and super helpful answer! Upvoted for sure. – Jay May 25 '21 at 19:27