reactjs - Excel report is not showing correctly with sales data - Stack Overflow

admin2025-04-17  4

I am trying to download a sales report from the admin dashboard that I have created, when is download it in Excel format sales table data is not printing, but when I console the sales data before and after adding it is correctly printing but not showing in report that is downloaded. I am using XLSX library with React.

My code:

const handleExcelDownload = () => {
    console.log ("Starting Excel export with sales data:", salesData);

    const wb = XLSX.utils.book_new();

    // Summary Sheet
    const summaryData = [
      ['Sales Report', '', '', '', ''],
      ['Generated on:', new Date().toLocaleDateString(), '', '', ''],
      ['', '', '', '', ''],
      ['Summary Statistics', '', '', '', ''],
      ['', '', '', '', ''],
      ['Total Orders', totalSales, '', '', ''],
      ['Total Revenue', formatIndianRupee(totalAmount), '', '', ''],
      ['Total Discounts', formatIndianRupee(totalDiscount), '', '', ''],
      ['', '', '', '', '']
    ];

    const summaryWS = XLSX.utils.aoa_to_sheet(summaryData);
    XLSX.utils.book_append_sheet(wb, summaryWS, 'Summary');

    // Sales data sheet
    const salesHeaders = [
      ['Date', 'Order ID', 'Products', 'User', 'Amount', 'Discount', 'Status']
    ];

    // Convert sales data to rows
    const salesRows = salesData.map(sale => [
      new Date(sale.date).toLocaleDateString(),
      sale.orderId,
      sale.products.map(p => `${p.name} (${p.quantity})`).join(', '),
      sale.userName,
      formatIndianRupee(sale.amount),
      formatIndianRupee(sale.discount),
      sale.status
    ]);

    // Combine headers and rows
    const salesTableData = [...salesHeaders, ...salesRows];
    console.log("Sales Table Data:", salesTableData); // Debugging line

    // Create sales worksheet
    const salesWS = XLSX.utils.aoa_to_sheet(salesTableData);
    console.log("Sales Worksheet:", salesWS); // Debugging line

    // Set column widths
    const salesColWidth = [
      { wch: 15 },  // Date
      { wch: 25 },  // Order ID
      { wch: 40 },  // Products
      { wch: 25 },  // User
      { wch: 15 },  // Amount
      { wch: 15 },  // Discount
      { wch: 15 }   // Status
    ];
    salesWS['!cols'] = salesColWidth;

    // Append sales worksheet to workbook
    XLSX.utils.book_append_sheet(wb, salesWS, 'Sales Details');
    console.log("Workbook Sheets:", wb.SheetNames); // Debugging line

    // Save the file
    try {
      const date = new Date().toISOString().split('T')[0];
      XLSX.writeFile(wb, `sales_report_${date}.xlsx`);
      console.log("Excel file generated successfully");
    } catch (error) {
      console.error("Error generating Excel file:", error);
    }
};

I am trying to download a sales report from the admin dashboard that I have created, when is download it in Excel format sales table data is not printing, but when I console the sales data before and after adding it is correctly printing but not showing in report that is downloaded.

转载请注明原文地址:http://anycun.com/QandA/1744899441a89199.html