用JavaScript通过C++插入或删除Excel工作表中的行
Contents
[
Hide
]
在创建新工作表或操作已有工作表时,可能需要添加额外的行或列以容纳数据。也可能需要删除指定位置的行或列。
Aspose.Cells for JavaScript通过C++提供两种插入和删除行的方法:Cells.insertRows(number, number, boolean)和Cells.deleteRows(number, number)。这些方法经过性能优化,执行速度非常快。
建议在插入或删除多行时,始终使用 Cells.insertRows(number, number, boolean) 和 Cells.deleteRows(number, number) 方法,而不是在循环中使用 Cells.insertRow(number) 或 Cells.deleteRow(number) 方法。
Aspose.Cells的工作方式与Microsoft Excel相同。当添加行或列时,工作表内容向下和向右移动。当删除行或列时,工作表内容向上或向左移动。添加或删除行时,其他工作表和单元格中的引用会得到更新。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Insert and Delete Rows</title>
</head>
<body>
<h1>Insert and Delete Rows Example</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, Utils } = 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();
// Instantiate a Workbook object and load the selected file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Get the first worksheet in the book
const sheet = workbook.worksheets.get(0);
// Insert 10 rows at row index 2 (insertion starts at 3rd row)
sheet.cells.insertRows(2, 10);
// Delete 5 rows now. (8th row - 12th row)
sheet.cells.deleteRows(7, 5);
// Save the excel file
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'out_book1.out.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Rows modified successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>