Combina più fogli di lavoro in un singolo foglio di lavoro con JavaScript tramite C++
Contents
[
Hide
]
A volte è necessario combinare più fogli di lavoro in un unico foglio di lavoro. Questo può essere facilmente ottenuto utilizzando l’API Aspose.Cells. Questo articolo ti mostrerà un esempio di codice che legge un libro di lavoro di origine e combina i dati di tutti i fogli di lavoro di origine in un unico foglio di lavoro all’interno di un libro di lavoro di destinazione.
Il seguente esempio di codice mostra come combinare più fogli di lavoro in un unico foglio di lavoro.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Merge Worksheets into One Workbook</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Loads the workbook which contains hidden external links
const workbook = new Workbook(new Uint8Array(arrayBuffer));
const destWorkbook = new Workbook();
const destSheet = destWorkbook.worksheets.get(0);
let TotalRowCount = 0;
for (let i = 0; i < workbook.worksheets.count; i++) {
const sourceSheet = workbook.worksheets.get(i);
const sourceRange = sourceSheet.cells.maxDisplayRange;
const destRange = destSheet.cells.createRange(
sourceRange.firstRow + TotalRowCount,
sourceRange.firstColumn,
sourceRange.rowCount,
sourceRange.columnCount
);
destRange.copy(sourceRange);
TotalRowCount += sourceRange.rowCount;
}
const outputData = destWorkbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'Output.out.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Merged Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook merged successfully! Click the download link to get the result.</p>';
});
</script>
</html>