This repository contains the example code for integrating Firebase Firestore with React (using Vite) to export data as Excel files. The solution leverages Firebase Cloud Functions to automate the conversion of Firestore documents into CSV format, which are then made downloadable as Excel files. This example is particularly useful for generating reports and managing data exports in admin panels or similar applications.
Before you begin, ensure you have the following:
- A Firebase account.
- A Firebase project with Firestore and Cloud Functions enabled.
- Node.js installed on your machine.
- Yarn or npm for managing packages.
Start by cloning this repository to your local machine:
git clone https://github.com/your-username/firestore-data-to-excel.git
cd firestore-data-to-excel
Install the necessary dependencies using npm or Yarn:
npm install
# or
yarn install
# or
pnpm install
Ensure your Firebase project is set up and initialized:
-
Follow the Firebase documentation to create a new Firebase project and enable Firestore and Cloud Functions.
-
Configure your Firebase project settings in your application.
Run the application locally using the following command:
npm run dev
# or
yarn dev
This will start the Vite development server and open the application in your default web browser.
Open up a new terminal window and run the following command to deploy the Cloud Functions:
firebase deploy --only functions
This will deploy the Cloud Functions to your Firebase project.
-
React Frontend: Showcases how to fetch and display Firestore data in a table using React hooks.
-
Excel Export: Includes a button that triggers a Cloud Function to convert Firestore data to CSV and then to Excel format.
-
Real-time Updates: Uses Firestore's onSnapshot() method to watch for updates and retrieve the download link once ready.
The included Cloud Function automates the process of fetching data from Firestore, converting it into a readable date format using date-fns, converting that data to CSV using Papaparse, and then uploading the file to Firebase Storage for public access.
For more detailed understanding and customizations, refer to the blog post that walks through the development of this feature: Medium Blog.
Happy Coding!