fix excel issue

This commit is contained in:
Raman Marikanti 2026-02-20 11:52:01 +05:30
parent a100d99f1d
commit 12951b33c3
1 changed files with 54 additions and 71 deletions

View File

@ -439,85 +439,68 @@ export class StockDashboard extends Component {
this.downloadFile(csvContent, fileName, 'text/csv');
}
exportToExcel() {
let tableHeaders, rows;
async exportToExcel() {
// 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') {
const filteredData = this.getFilteredFinishedProducts();
tableHeaders = `
<tr>
<th>Product Code</th>
<th>Product Name</th>
<th>Opening Stock</th>
<th>Production</th>
<th>Dispatch</th>
<th>Closing Stock</th>
<th>UOM</th>
<th>Value</th>
</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('');
headers = ['Product Code', 'Product Name', 'Opening Stock', 'Production', 'Dispatch', 'Closing Stock', 'UOM', 'Value'];
data = filteredData.map(row => [
row.product_code || '',
row.product_name || '',
row.opening_stock || 0,
row.production || 0,
row.dispatch || 0,
row.closing_stock || 0,
row.uom || '',
row.value || 0
]);
} else {
const filteredData = this.getFilteredRawMaterials();
tableHeaders = `
<tr>
<th>Product Code</th>
<th>Product Name</th>
<th>Opening Stock</th>
<th>Receipts</th>
<th>Consumption</th>
<th>Closing Stock</th>
<th>UOM</th>
<th>Value</th>
</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('');
headers = ['Product Code', 'Product Name', 'Opening Stock', 'Receipts', 'Consumption', 'Closing Stock', 'UOM', 'Value'];
data = filteredData.map(row => [
row.product_code || '',
row.product_name || '',
row.opening_stock || 0,
row.receipts || 0,
row.consumption || 0,
row.closing_stock || 0,
row.uom || '',
row.value || 0
]);
}
const tableHtml = `
<html>
<head>
<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>
`;
// Create workbook and worksheet
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet([headers, ...data]);
const fileName = `${this.state.activeCategory.toLowerCase().replace(/ /g, '-')}_${this.state.fromDate}_to_${this.state.toDate}.xls`;
this.downloadFile(tableHtml, fileName, 'application/vnd.ms-excel');
// Set column widths
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() {