React Native – ExcelJS library export getting stuck

I’m building a data recollection application using React Native, in which said data is exported to an Excel file.

I’m running into an issue which is that whenever I attempt to export the data, the export process never starts processing the data and it just gets stuck.

Where the export is being called:

const sampleData = {
    Datos: [
        { 'Nombre': 'Alice', 'Apellido': 'Smith', 'Correo Electrónico': '[email protected]' },
        { 'Nombre': 'Bob', 'Apellido': 'Johnson', 'Correo Electrónico': '[email protected]' },
        { 'Nombre': 'Charlie', 'Apellido': 'Brown', 'Correo Electrónico': '[email protected]' },
    ],
    Superficies: [
        ['Room', 'Size (m2)'],
        ['Living Room', 20],
        ['Bedroom', 15],
        ['Kitchen', 10],
    ],
    Huecos: [
        { 'Vivienda': 'Apt 101', 'Planta': '1', 'Iluminación': 'SI', 'Ventilación': 'NO' },
        { 'Vivienda': 'Apt 102', 'Planta': '1', 'Iluminación': 'NO', 'Ventilación': 'SI' },
        { 'Vivienda': 'Apt 103', 'Planta': '2', 'Iluminación': 'SI', 'Ventilación': 'SI' },
    ]
};


const handleExport = () => {
    setLoading(true); // Start loading
    console.log('Export started');

    setTimeout(async () => {
        try {
            console.log('Processing data...');

            // Use sampleData directly for export
            const allData = sampleData;

            console.log('Data processed. Starting export...');
            const filePath = await exportToExcel(allData);
            console.log('Export completed');

            if (filePath) {
                console.log('File exported successfully:', filePath);
                Alert.alert('Success', 'File exported successfully', [
                    { text: 'Open', onPress: () => openFile(filePath) },
                    { text: 'OK', style: 'cancel' },
                ]);
            } else {
                console.log('Export failed: File path is null');
                Alert.alert('Error', 'Failed to export file');
            }
        } catch (error) {
            console.log('Export failed with error:', error);
            Alert.alert('Error', `An error occurred: ${error.message}`);
        } finally {
            setLoading(false); // Stop loading
            console.log('Loading stopped');
        }
    }, 0); // Delay to allow UI update
};

How the export is being handled:

import ExcelJS from 'exceljs';
import * as RNFS from 'react-native-fs';
import { Alert } from 'react-native';
import { openFile } from './openFile'; // Assuming openFile is implemented to open the file

export const exportToExcel = async (data) => {
    // Create a new workbook
    const workbook = new ExcelJS.Workbook();

    // Add Datos sheet
    const wsDatos = workbook.addWorksheet('Datos');
    wsDatos.columns = [
        { header: 'Nombre', key: 'Nombre', width: 20 },
        { header: 'Apellido', key: 'Apellido', width: 20 },
        { header: 'Correo Electrónico', key: 'Correo Electrónico', width: 30 },
    ];

    data.Datos.forEach(item => {
        wsDatos.addRow(item);
    });

    // Add Superficies sheet
    const wsSuperficies = workbook.addWorksheet('Superficies');
    wsSuperficies.addRows(data.Superficies);

    // Add Huecos sheet
    const wsHuecos = workbook.addWorksheet('Huecos');
    wsHuecos.columns = [
        { header: 'Vivienda', key: 'Vivienda', width: 20 },
        { header: 'Planta', key: 'Planta', width: 10 },
        { header: 'Iluminación', key: 'Iluminación', width: 15 },
        { header: 'Ventilación', key: 'Ventilación', width: 15 },
    ];

    data.Huecos.forEach(item => {
        wsHuecos.addRow(item);
    });

    // Save workbook to file
    const filePath = `${RNFS.DocumentDirectoryPath}/TestData.xlsx`;
    try {
        const buffer = await workbook.xlsx.writeBuffer();
        await RNFS.writeFile(filePath, buffer.toString('base64'), 'base64');
        console.log('File saved successfully at:', filePath);
        return filePath;
    } catch (error) {
        console.error('Error saving file', error);
        return null;
    }
};

I’ve tried a plethora of things, but none have worked so far. I don’t know if it’s a problem with my project or with how I’m handling the data, since it doesn’t even work with the sample data. The console never logs that it is processing the data, only that the export has started.

New contributor

Alex Tiribeja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật