用 JavaScript 和 C++ 显示或隐藏行、列和滚动条
显示和隐藏行和列
使用 C++ 的 Aspose.Cells for JavaScript 提供了一个类 Workbook,代表一个 Microsoft Excel 文件。Workbook 类包含一个 worksheets 集合,允许开发人员访问 Excel 文件中的每个工作表。工作表由 Worksheet 类表示。Worksheet 类提供一个 cells 集合,表示工作表中的所有单元格。cells 集合提供了多种管理行或列的方法,以下几种为例。
显示行和列
开发者可以通过调用cells集合的unhideRow(number, number)和unhideColumn(number, number)方法,显示任何隐藏的行或列。两个方法都接受两个参数:
- 行或列索引 - 用于显示特定行或列的索引。
- 行高或列宽 - 在取消隐藏后分配给行或列的行高或列宽。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Unhide Row and Column 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, Worksheet, Cell, 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();
const workbook = new Workbook(new Uint8Array(arrayBuffer));
const worksheet = workbook.worksheets.get(0);
worksheet.cells.unhideRow(2, 13.5);
worksheet.cells.unhideColumn(1, 8.5);
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
隐藏行和列
开发者可以通过调用cells集合的hideRow(number)和hideColumn(number)方法,隐藏某一行或列。两个方法都接受行或列的索引作为参数以隐藏对应的行或列。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Hide Row/Column 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 from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access the first worksheet
const worksheet = workbook.worksheets.get(0);
// Hide the 3rd row (index 2) and 2nd column (index 1)
worksheet.cells.hideRow(2);
worksheet.cells.hideColumn(1);
// Save the modified workbook and provide a download link
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Row and column hidden successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
隐藏多个行和列
开发者还可以一次隐藏多行或多列,通过调用cells集合的hideRows(number, number)和hideColumns(number, number)方法,两个方法都接受起始行或列索引和隐藏的行或列数目作为参数。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Hide Rows and Columns</title>
</head>
<body>
<h1>Hide Rows and Columns 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();
// Instantiating a Workbook object by opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.worksheets.get(0);
// Hiding 3, 4 and 5 rows in the worksheet (rows are zero-based index)
worksheet.cells.hideRows(2, 3);
// Hiding 2 and 3 columns in the worksheet (columns are zero-based index)
worksheet.cells.hideColumns(1, 2);
// Saving the modified Excel file
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
显示和隐藏滚动条
滚动条用于浏览任何文件的内容。通常有两种类型的滚动条:
- 垂直滚动条
- 水平滚动条
Microsoft Excel还提供水平和垂直滚动条,以便用户可以滚动工作表内容。使用Aspose.Cells,开发人员可以控制Excel文件中两种类型滚动条的可见性。
控制滚动条的可见性
Aspose.Cells提供了一个类Workbook,代表一个Excel文件。Workbook类提供了丰富的属性和方法用以管理Excel文件。若要控制滚动条的显示,请使用WorkbookSettings.isVScrollBarVisible()和WorkbookSettings.isHScrollBarVisible()属性。WorkbookSettings.isVScrollBarVisible()和WorkbookSettings.isHScrollBarVisible()是布尔型属性,只能存储true或false。
显示滚动条
通过将WorkbookSettings.isHScrollBarVisible()类的WorkbookSettings.isVScrollBarVisible()或WorkbookSettings.isHScrollBarVisible()属性设置为true,使滚动条可见。
隐藏滚动条
通过将WorkbookSettings.isHScrollBarVisible()类的WorkbookSettings.isVScrollBarVisible()或WorkbookSettings.isHScrollBarVisible()属性设置为false,隐藏滚动条。
示例代码
下面是一个完整的代码,打开一个Excel文件book1.xls,隐藏两个滚动条,然后将修改后的文件保存为output.xls。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells - Hide Scrollbars 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();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Hiding the vertical scroll bar of the Excel file
workbook.settings.isVScrollBarVisible = false;
// Hiding the horizontal scroll bar of the Excel file
workbook.settings.isHScrollBarVisible = false;
// Saving the modified Excel file
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Scrollbars hidden successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>