Skip to content

Commit 20bf105

Browse files
added excel export
1 parent f3ba034 commit 20bf105

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

src/pages/seller/SellerDashboard.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { useNavigate } from "react-router-dom";
2020
import productSlice from "../../store/features/product/productSlice";
2121
import { FaFileExcel } from "react-icons/fa";
2222
import exportToCSV from "../../utils/excel/exportToCSV";
23+
import exportToExcel from "../../utils/excel/exportToExcel";
2324

2425
const SellerDashboard = () => {
2526
const { OrderHistory, message, data, isError } = useAppSelector(
@@ -164,7 +165,7 @@ const SellerDashboard = () => {
164165
);
165166

166167
const exportStats = () => {
167-
exportToCSV(OrderHistory)
168+
exportToExcel(OrderHistory)
168169
}
169170

170171
return (

src/utils/excel/exportToExcel.ts

+32-27
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,46 @@
22
import * as XLSX from 'xlsx';
33

44
const exportToExcel = (orderHistory) => {
5-
const orderData = orderHistory.order.map(order => {
6-
// Parse the products string to JSON
7-
const products = JSON.parse(order.products);
5+
const orders = orderHistory.order;
6+
const rows = [
7+
[
8+
'Order ID',
9+
'Order Date',
10+
'Expected Delivery Date',
11+
'Order Status',
12+
'Payment Method',
13+
'Products',
14+
'Shipping Status'
15+
]
16+
];
17+
18+
orders.forEach(order => {
19+
let productsInfo = '';
820

9-
return products.map(product => ({
10-
orderId: order.id,
11-
cartId: order.cartId,
12-
shopId: order.shopId,
13-
orderDate: order.orderDate,
14-
paymentMethodId: order.paymentMethodId,
15-
status: order.status,
16-
shippingProcess: order.shippingProcess,
17-
expectedDeliveryDate: order.expectedDeliveryDate,
18-
createdAt: order.createdAt,
19-
updatedAt: order.updatedAt,
20-
productId: product.id,
21-
productName: product.name,
22-
productPrice: product.price,
23-
productDiscount: product.discount,
24-
productQuantity: product.quantity,
25-
productTotalPrice: product.totalPrice,
26-
productDescription: product.description,
27-
productImage: product.image,
28-
}));
29-
}).flat(); // Flatten the array of arrays
21+
if (Array.isArray(order.products)) {
22+
productsInfo = order.products.map(p => `${p.productId}(${p.status})`).join(', ');
23+
}
24+
25+
rows.push([
26+
order.id,
27+
new Date(order.orderDate).toLocaleString(),
28+
new Date(order.expectedDeliveryDate).toLocaleString(),
29+
order.status,
30+
order.paymentMethodId,
31+
productsInfo,
32+
order.shippingProcess
33+
]);
34+
});
3035

31-
// Convert the JSON data to a worksheet
32-
const worksheet = XLSX.utils.json_to_sheet(orderData);
36+
// Create a worksheet from the rows
37+
const worksheet = XLSX.utils.aoa_to_sheet(rows);
3338

3439
// Create a new workbook and append the worksheet
3540
const workbook = XLSX.utils.book_new();
3641
XLSX.utils.book_append_sheet(workbook, worksheet, 'OrderHistory');
3742

3843
// Export the workbook to an Excel file
39-
XLSX.writeFile(workbook, 'OrderHistory.xlsx');
44+
XLSX.writeFile(workbook, 'seller_orders.xlsx');
4045
};
4146

4247
export default exportToExcel;

0 commit comments

Comments
 (0)