使用JavaScript通过C++操作表格和区域
Contents
[
Hide
]
介绍
有时您在Microsoft Excel中创建一个表格,但不想继续使用它带来的表格功能。相反,您想要看起来像表格的东西。为了在不丢失格式的情况下保留表格中的数据,可以将表格转换为普通数据范围。
Aspose.Cells确实支持Microsoft Excel的表格和列表对象的此功能。
使用Microsoft Excel
使用转换为范围功能快速将表格转换为常规数据范围,而不丢失格式。在Microsoft Excel 2007/2010中:
- 单击表中的任意位置,确保活动单元格位于表列中。
- 在设计选项卡的工具组中,单击转换为范围。
使用Aspose.Cells
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Convert Table to Range</title>
</head>
<body>
<h1>Convert Table to Range 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 } = 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 workbook from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access the first worksheet and convert the first table/list object to a normal range
const worksheet = workbook.worksheets.get(0);
const listObject = worksheet.listObjects.get(0);
listObject.convertToRange();
// Save the modified workbook and provide a download link
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Table converted to range successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
表格在转换为范围后将不再可用其表格功能。例如,行标头不再包含排序和筛选箭头,以及在公式中使用的结构引用(使用表名称的引用)会变成常规单元格引用。
使用选项将表格转换为范围
Aspose.Cells在将表格转换为范围时提供附加选项,通过TableToRangeOptions类提供TableToRangeOptions类,并提供lastRow属性,允许您设置表格行的最后索引。表格格式将保留到指定的行索引,其余格式将被移除。
以下给出的示例代码演示了TableToRangeOptions类的使用。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Table to Range Example</title>
</head>
<body>
<h1>Convert Table to Range Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx" />
<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, TableToRangeOptions, 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');
const resultDiv = document.getElementById('result');
const downloadLink = document.getElementById('downloadLink');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Create TableToRangeOptions and set lastRow property
const options = new TableToRangeOptions();
options.lastRow = 5;
// Convert the first table/list object (from the first worksheet) to normal range
workbook.worksheets.get(0).listObjects.get(0).convertToRange(options);
// Saving the modified Excel file
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
resultDiv.innerHTML = '<p style="color: green;">Table converted to range successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>