fix excel issue
This commit is contained in:
parent
a100d99f1d
commit
12951b33c3
|
|
@ -439,85 +439,68 @@ export class StockDashboard extends Component {
|
||||||
this.downloadFile(csvContent, fileName, 'text/csv');
|
this.downloadFile(csvContent, fileName, 'text/csv');
|
||||||
}
|
}
|
||||||
|
|
||||||
exportToExcel() {
|
async exportToExcel() {
|
||||||
let tableHeaders, rows;
|
// Load SheetJS library dynamically if not available
|
||||||
|
if (typeof XLSX === 'undefined') {
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
const script = document.createElement('script');
|
||||||
|
script.src = 'https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.full.min.js';
|
||||||
|
script.onload = resolve;
|
||||||
|
script.onerror = reject;
|
||||||
|
document.head.appendChild(script);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let headers, data;
|
||||||
|
|
||||||
if (this.state.activeCategory === 'Finished Products') {
|
if (this.state.activeCategory === 'Finished Products') {
|
||||||
const filteredData = this.getFilteredFinishedProducts();
|
const filteredData = this.getFilteredFinishedProducts();
|
||||||
tableHeaders = `
|
headers = ['Product Code', 'Product Name', 'Opening Stock', 'Production', 'Dispatch', 'Closing Stock', 'UOM', 'Value'];
|
||||||
<tr>
|
data = filteredData.map(row => [
|
||||||
<th>Product Code</th>
|
row.product_code || '',
|
||||||
<th>Product Name</th>
|
row.product_name || '',
|
||||||
<th>Opening Stock</th>
|
row.opening_stock || 0,
|
||||||
<th>Production</th>
|
row.production || 0,
|
||||||
<th>Dispatch</th>
|
row.dispatch || 0,
|
||||||
<th>Closing Stock</th>
|
row.closing_stock || 0,
|
||||||
<th>UOM</th>
|
row.uom || '',
|
||||||
<th>Value</th>
|
row.value || 0
|
||||||
</tr>
|
]);
|
||||||
`;
|
|
||||||
rows = filteredData.map(row => `
|
|
||||||
<tr>
|
|
||||||
<td>${row.product_code || ''}</td>
|
|
||||||
<td>${row.product_name || ''}</td>
|
|
||||||
<td>${row.opening_stock || 0}</td>
|
|
||||||
<td>${row.production || 0}</td>
|
|
||||||
<td>${row.dispatch || 0}</td>
|
|
||||||
<td>${row.closing_stock || 0}</td>
|
|
||||||
<td>${row.uom || ''}</td>
|
|
||||||
<td>${row.value || 0}</td>
|
|
||||||
</tr>
|
|
||||||
`).join('');
|
|
||||||
} else {
|
} else {
|
||||||
const filteredData = this.getFilteredRawMaterials();
|
const filteredData = this.getFilteredRawMaterials();
|
||||||
tableHeaders = `
|
headers = ['Product Code', 'Product Name', 'Opening Stock', 'Receipts', 'Consumption', 'Closing Stock', 'UOM', 'Value'];
|
||||||
<tr>
|
data = filteredData.map(row => [
|
||||||
<th>Product Code</th>
|
row.product_code || '',
|
||||||
<th>Product Name</th>
|
row.product_name || '',
|
||||||
<th>Opening Stock</th>
|
row.opening_stock || 0,
|
||||||
<th>Receipts</th>
|
row.receipts || 0,
|
||||||
<th>Consumption</th>
|
row.consumption || 0,
|
||||||
<th>Closing Stock</th>
|
row.closing_stock || 0,
|
||||||
<th>UOM</th>
|
row.uom || '',
|
||||||
<th>Value</th>
|
row.value || 0
|
||||||
</tr>
|
]);
|
||||||
`;
|
|
||||||
rows = filteredData.map(row => `
|
|
||||||
<tr>
|
|
||||||
<td>${row.product_code || ''}</td>
|
|
||||||
<td>${row.product_name || ''}</td>
|
|
||||||
<td>${row.opening_stock || 0}</td>
|
|
||||||
<td>${row.receipts || 0}</td>
|
|
||||||
<td>${row.consumption || 0}</td>
|
|
||||||
<td>${row.closing_stock || 0}</td>
|
|
||||||
<td>${row.uom || ''}</td>
|
|
||||||
<td>${row.value || 0}</td>
|
|
||||||
</tr>
|
|
||||||
`).join('');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const tableHtml = `
|
// Create workbook and worksheet
|
||||||
<html>
|
const wb = XLSX.utils.book_new();
|
||||||
<head>
|
const ws = XLSX.utils.aoa_to_sheet([headers, ...data]);
|
||||||
<style>
|
|
||||||
table { border-collapse: collapse; width: 100%; }
|
|
||||||
th, td { border: 1px solid black; padding: 8px; text-align: left; }
|
|
||||||
th { background-color: #f2f2f2; }
|
|
||||||
</style>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h2>${this.state.activeCategory} Report - ${this.state.fromDate} to ${this.state.toDate}</h2>
|
|
||||||
<table>
|
|
||||||
${tableHeaders}
|
|
||||||
${rows}
|
|
||||||
</table>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
`;
|
|
||||||
|
|
||||||
const fileName = `${this.state.activeCategory.toLowerCase().replace(/ /g, '-')}_${this.state.fromDate}_to_${this.state.toDate}.xls`;
|
// Set column widths
|
||||||
this.downloadFile(tableHtml, fileName, 'application/vnd.ms-excel');
|
const colWidths = headers.map((header, idx) => {
|
||||||
|
const maxLength = Math.max(
|
||||||
|
header.length,
|
||||||
|
...data.map(row => String(row[idx] || '').length)
|
||||||
|
);
|
||||||
|
return { wch: Math.min(maxLength + 2, 50) };
|
||||||
|
});
|
||||||
|
ws['!cols'] = colWidths;
|
||||||
|
|
||||||
|
// Add worksheet to workbook
|
||||||
|
XLSX.utils.book_append_sheet(wb, ws, this.state.activeCategory);
|
||||||
|
|
||||||
|
// Generate XLSX file
|
||||||
|
const fileName = `${this.state.activeCategory.toLowerCase().replace(/ /g, '-')}_${this.state.fromDate}_to_${this.state.toDate}.xlsx`;
|
||||||
|
XLSX.writeFile(wb, fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
exportToJSON() {
|
exportToJSON() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue