使用 JavaScript 和 C++ 显示或隐藏网格线以及行列标题

显示和隐藏网格线

所有Excel工作表默认情况下都有网格线。它们有助于划分单元格,便于将数据输入特定的单元格。网格线使我们能够将工作表视为单元格的集合,其中每个单元格都易于识别。

控制网格线的可见性

Aspose.Cells提供了一个类Workbook,代表一个Microsoft Excel文件。Workbook类包含一个Workbook.worksheets集合,允许开发者访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供了丰富的属性和方法管理工作表。若要控制网格线的显示,请使用Worksheet.isGridlinesVisible()属性。Worksheet.isGridlinesVisible()是一个布尔型属性,只能存储true或false值。

使网格线可见

通过将Worksheet.isGridlinesVisible()属性设置为true,使网格线可见。

隐藏网格线

通过将Worksheet.isGridlinesVisible()属性设置为false,隐藏网格线。

下面给出一个完整示例,展示如何通过打开Excel文件(book1.xls)、隐藏第一个工作表上的网格线,并将修改后的文件另存为output.xls,来使用Worksheet.isGridlinesVisible()属性。

<!DOCTYPE html>
<html>
    <head>
        <title>Hide Gridlines Example</title>
    </head>
    <body>
        <h1>Hide Gridlines 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');
            const resultDiv = document.getElementById('result');
            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
            // Opening the Excel file through the file data
            const workbook = new Workbook(new Uint8Array(arrayBuffer));

            // Accessing the first worksheet in the Excel file
            const worksheet = workbook.worksheets.get(0);

            // Hiding the grid lines of the first worksheet of the Excel file
            worksheet.isGridlinesVisible = 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';

            resultDiv.innerHTML = '<p style="color: green;">Gridlines hidden successfully! Click the download link to get the modified file.</p>';
        });
    </script>
</html>

显示和隐藏行列标题

Excel文件中的所有工作表都由排列在行和列中的单元格组成。所有行和列都有用于标识它们和单个单元格的唯一值。例如,行编号为1、2、3、4等,列按字母顺序排列为A、B、C、D等。行和列值显示在标题中。使用Aspose.Cells,开发人员可以控制这些行和列标题的可见性。

控制工作表的可见性

Aspose.Cells提供了一个类Workbook,代表一个Microsoft Excel文件。Workbook类包含一个Workbook.worksheets集合,允许开发者访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供了丰富的属性和方法管理工作表。若要控制行列标题的可见性,请使用Worksheet.isRowColumnHeadersVisible()属性。Worksheet.isRowColumnHeadersVisible()是一个布尔型属性,只能存储true或false值。

使行/列标头可见

通过将Worksheet.isRowColumnHeadersVisible()属性设置为true,使行和列标题可见。

隐藏行/列标头

通过将Worksheet.isRowColumnHeadersVisible()属性设置为false,隐藏行列标题。

下例展示了如何通过打开Excel文件(book1.xls)、隐藏第一个工作表的行列标题,并将修改后的文件另存为output.xls,来使用Worksheet.isRowColumnHeadersVisible()属性。

<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Example - Hide Row/Column Headers</title>
    </head>
    <body>
        <h1>Hide Row and Column Headers 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 } = 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 with the uploaded file
            const workbook = new Workbook(new Uint8Array(arrayBuffer));

            // Accessing the first worksheet in the Excel file
            const worksheet = workbook.worksheets.get(0);

            // Hiding the headers of rows and columns
            worksheet.isRowColumnHeadersVisible = 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;">Headers hidden successfully! Click the download link to get the modified file.</p>';
        });
    </script>
</html>