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

C# Interop Excel设置:将按Enter后移动选区方向改为「无」

How to Set "Move Selection After Enter" to "None" via C# Excel Interop

No worries, I’ve tackled this exact setting before—here’s the straightforward way to get it done with Interop:

The key is using the MoveAfterReturn property on the Excel Application object. When you set this to false, it disables the selection movement entirely, which is exactly the "None" option you’re looking for.

Here’s a complete code example:

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

class ExcelSettingsManager
{
    static void Main()
    {
        Application excelApp = null;
        Workbook workbook = null;

        try
        {
            // Initialize Excel application
            excelApp = new Application();
            excelApp.Visible = true; // Make Excel visible to verify the change

            // Set "Move selection after Enter" to "None"
            excelApp.MoveAfterReturn = false;

            // Optional: Open an existing workbook if needed
            // workbook = excelApp.Workbooks.Open(@"C:\Your\File\Path.xlsx");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
        finally
        {
            // Clean up COM objects to avoid memory leaks
            if (workbook != null)
            {
                workbook.Close(false);
                Marshal.ReleaseComObject(workbook);
            }
            if (excelApp != null)
            {
                excelApp.Quit();
                Marshal.ReleaseComObject(excelApp);
            }
        }
    }
}

Quick Notes:

  • If you’re working with an already running Excel instance, you can get the Application object via Marshal.GetActiveObject("Excel.Application") instead of creating a new one.
  • The MoveAfterReturnDirection property (which controls up/down/left/right) only matters when MoveAfterReturn is set to true—since you want "None", we don’t need to touch that.
  • Always remember to release COM objects properly like in the finally block—this prevents Excel from lingering in the background after your app closes.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:19:17