You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Excel Interop将工作表视图样式设置为普通视图?

使用Excel Interop将Excel工作表设置为普通视图

别慌!这事儿其实没那么复杂,我给你分语言讲清楚实现步骤,代码示例都给你备好啦~

前提准备

首先得在项目里引用Microsoft.Office.Interop.Excel程序集:可以通过NuGet搜索安装,或者在Visual Studio里右键项目→添加引用→找到对应的Office组件。

C# 代码示例

using Microsoft.Office.Interop.Excel;
using System;

class ExcelViewSetter
{
    static void Main(string[] args)
    {
        Application excelApp = null;
        Workbook targetWorkbook = null;
        Worksheet targetSheet = null;

        try
        {
            // 初始化Excel应用实例
            excelApp = new Application();
            // 打开目标工作簿(替换成你的实际文件路径)
            targetWorkbook = excelApp.Workbooks.Open(@"D:\Test\YourExcelFile.xlsx");
            // 获取指定工作表——这里取第一个,也可以按名称获取:targetWorkbook.Worksheets["Sheet1"]
            targetSheet = targetWorkbook.Worksheets[1] as Worksheet;

            // 切换到普通视图
            targetSheet.Activate();
            excelApp.ActiveWindow.View = XlWindowView.xlNormalView;

            // 保存更改
            targetWorkbook.Save();
            Console.WriteLine("已成功将工作表设置为普通视图!");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"操作出错:{ex.Message}");
        }
        finally
        {
            // 关键步骤:释放COM对象,避免Excel进程残留后台
            if (targetSheet != null)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(targetSheet);
            if (targetWorkbook != null)
            {
                targetWorkbook.Close();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(targetWorkbook);
            }
            if (excelApp != null)
            {
                excelApp.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
            }
        }
    }
}

VB.NET 代码示例

Imports Microsoft.Office.Interop.Excel

Module ExcelViewModule
    Sub Main()
        Dim excelApp As Application = Nothing
        Dim targetWorkbook As Workbook = Nothing
        Dim targetSheet As Worksheet = Nothing

        Try
            ' 初始化Excel应用实例
            excelApp = New Application()
            ' 打开目标工作簿(替换成你的实际文件路径)
            targetWorkbook = excelApp.Workbooks.Open("D:\Test\YourExcelFile.xlsx")
            ' 获取指定工作表——这里取第一个,也可以按名称获取:targetWorkbook.Worksheets("Sheet1")
            targetSheet = CType(targetWorkbook.Worksheets(1), Worksheet)

            ' 切换到普通视图
            targetSheet.Activate()
            excelApp.ActiveWindow.View = XlWindowView.xlNormalView

            ' 保存更改
            targetWorkbook.Save()
            Console.WriteLine("已成功将工作表设置为普通视图!")
        Catch ex As Exception
            Console.WriteLine($"操作出错:{ex.Message}")
        Finally
            ' 释放COM对象,避免Excel进程残留后台
            If targetSheet IsNot Nothing Then
                System.Runtime.InteropServices.Marshal.ReleaseComObject(targetSheet)
            End If
            If targetWorkbook IsNot Nothing Then
                targetWorkbook.Close()
                System.Runtime.InteropServices.Marshal.ReleaseComObject(targetWorkbook)
            End If
            If excelApp IsNot Nothing Then
                excelApp.Quit()
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
            End If
        End Try
    End Sub
End Module

小提醒

  • 记得把代码里的文件路径替换成你实际的Excel文件路径哦。
  • 如果不想引用XlWindowView常量,直接把excelApp.ActiveWindow.View的值设为1也可以——因为xlNormalView对应的常量值就是1。
  • 释放COM对象的步骤千万别省!不然哪怕你关闭了工作簿,Excel进程还是会偷偷留在后台占用内存。

内容的提问来源于stack exchange,提问作者hedbisker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 22:03:16