A class to process Firebase Firestore database queries and create an excel file that could be shared
-
Add above class to your project
-
Import necessary libraries
-
To use the class in another class or activity create an instance of it
-fieldNames String array contains the name of the fields you want to retrieve from the query. Order of names in the array is important.
String[] fieldNames = {"name", "email", "id", "more"};
FirebaseDBCollectionToExcel createExcel = new FirebaseDBCollectionToExcel( fieldNames);
- Within the part you call the query include the buildFileFromQuery(QueryDocumentSnapshot document ) method like so:
db.collection("users").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for ( QueryDocumentSnapshot document : task.getResult()) { //Include here
createExcel.buildFileFromQuery(document);
}
}
}
}
- After creating the file, you can save it to the local storage using saveFileToStorage(String fileName, String dirName) method:
String fileName = "File Name";
String dirName = "Directory Name";
createExcel.saveFileToStorage(fileName, dirName);
-
Lastly given the file is in storage, you can share it through mailing applications using the method shareFileToEmail( String subject)
This method will open an intent
String subject = "Email Subject";
createExcel.shareToEmail( subject);
- Create a ready to use importable library