export excel

This commit is contained in:
2024-09-30 16:09:29 +05:45
parent 60f4c75eb0
commit 4683fe67a7
21 changed files with 889 additions and 408 deletions

View File

@ -39,101 +39,76 @@
<link rel="stylesheet" href="<?php echo base_url(); ?>plugins/select2-bootstrap4-theme/select2-bootstrap4.min.css">
<link rel="stylesheet" href="<?php echo base_url(); ?>plugins/bstreeview/css/bstreeview.min.css">
<link rel="stylesheet" href="<?php echo base_url(); ?>dist/css/bbnepalcustom.css">
<!-- CSV File create -->
<script>
// function exportTableToCSVNew(filename) {
// const csvRows = [];
// const rows = document.querySelectorAll("#dataTable tr");
// for (const row of rows) {
// const cols = row.querySelectorAll("td, th");
// const csvRow = [];
// document.getElementById('downloadBtn').addEventListener('click', function() {
// const table = document.getElementById('myTable');
// const csvData = [];
// const rowCount = table.rows.length;
// const output = [];
// for (const col of cols) {
// // csvRow.push(col.innerText.replace(/,/g, '')); // Remove commas for CSV
// csvRow.push(col.innerText); // Include empty cells as ''
// for (let i = 0; i < rowCount; i++) {
// const row = table.rows[i];
// const rowArray = [];
// let skipCell = 0;
// for (let j = 0; j < row.cells.length; j++) {
// const cell = row.cells[j];
// if (skipCell > 0) {
// skipCell--;
// continue; // Skip this cell due to rowspan
// }
// const cellContent = cell.textContent.trim();
// const colspan = cell.colSpan || 1;
// const rowspan = cell.rowSpan || 1;
// // Fill the rowArray for the colspan
// for (let k = 0; k < colspan; k++) {
// if (k === colspan - 1) {
// rowArray.push(cellContent); // Last cell of colspan gets content
// } else {
// rowArray.push(''); // Fill empty for additional colspan cells
// }
// }
// // Set skipCell to account for rowspan
// skipCell = rowspan - 1;
// // Ensure that the row length matches the maximum number of columns
// while (rowArray.length < output.length) {
// rowArray.push(''); // Fill empty cells
// }
// // Fill the output structure
// output[i] = rowArray;
// }
// }
// csvRows.push(csvRow.join(",")); // Join cells with commas
// }
// // Create a CSV string
// const csvString = csvRows.join("\n");
// // Join rows for CSV format
// output.forEach(row => {
// const filledRow = row.map(cell => cell === undefined ? '' : cell);
// csvData.push(filledRow.join(','));
// });
// // Create a Blob for the CSV string
// const blob = new Blob([csvString], {
// type: "text/csv"
// });
// const url = URL.createObjectURL(blob);
// const a = document.createElement("a");
// a.href = url;
// a.download = filename;
// document.body.appendChild(a);
// a.click();
// document.body.removeChild(a);
// }
// const csvContent = csvData.join('\n');
// const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
// const url = URL.createObjectURL(blob);
function exportTableToCSV(filename) {
const csvRows = [];
const rows = document.querySelectorAll("#dataTable tr");
let rowSpanTracker = {};
for (const row of rows) {
const cols = row.querySelectorAll("td, th");
const csvRow = [];
let currentRow = [];
for (const col of cols) {
const colSpan = parseInt(col.getAttribute('colspan') || 1);
const rowSpan = parseInt(col.getAttribute('rowspan') || 1);
const cellValue = col.innerText.replace(/,/g, '').trim() || '';
// Handle row span by marking the cells
if (rowSpan > 1) {
for (let r = 0; r < rowSpan; r++) {
if (!rowSpanTracker[r]) rowSpanTracker[r] = {};
rowSpanTracker[r][currentRow.length] = true; // Mark this position for rowspan
}
}
// Fill CSV row with values
for (let i = 0; i < colSpan; i++) {
if (i === 0 && !rowSpanTracker[currentRow.length]?.[currentRow.length]) {
csvRow.push(cellValue); // First instance of the cell value
} else {
csvRow.push(''); // Other instances as empty
}
}
}
// Fill the empty spaces due to rowspan
if (rowSpanTracker[currentRow.length]) {
currentRow.forEach((_, index) => {
if (!rowSpanTracker[currentRow.length]?.[index]) {
csvRow[index] = ''; // Fill in empty spaces
}
});
}
csvRows.push(csvRow.join(",")); // Join cells with commas
}
// Create a CSV string
const csvString = csvRows.join("\n");
// Create a Blob for the CSV string
const blob = new Blob([csvString], {
type: "text/csv"
});
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
// const link = document.createElement('a');
// link.setAttribute('href', url);
// link.setAttribute('download', 'table_with_colspan_rowspan.csv');
// document.body.appendChild(link);
// link.click();
// document.body.removeChild(link);
// });
</script>
<!-- csv printing ends -->