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
MoveAfterReturnDirectionproperty (which controls up/down/left/right) only matters whenMoveAfterReturnis set totrue—since you want "None", we don’t need to touch that. - Always remember to release COM objects properly like in the
finallyblock—this prevents Excel from lingering in the background after your app closes.
内容的提问来源于stack exchange,提问作者user8417115
相关产品推荐
相关产品推荐

