How do you format code in Visual Studio Code (VSCode)?

Questions : How do you format code in Visual Studio Code (VSCode)?

What is the equivalent of Ctrl + K + F and Ctrl + K + D on Windows in Visual Studio for formatting, or “beautifying” code in the Visual Studio Code editor?

Total Answers: 29 Answers 29


Popular Answers:

  1. The code formatting is available in Visual Studio Code through the following shortcuts:

    • On Windows Shift + Alt + F
    • On Mac Shift + Option + F
    • On Linux Ctrl + Shift + I

    Alternatively, you can find the shortcut, as well as other shortcuts, through the ‘Command Palette’ provided in the editor with Ctrl +Shift+ P (or Command + Shift + P on Mac), and then searching for format document.

    For unsaved snippets

    1. Open command palette (Win: F1 or Ctrl+Shift+P)

    2. Find “Change Language Mode”

    3. Select language e.g. json. By now syntax should be highlighted.

    4. Format document (e.g. Open Command Palette -> “Format Document“)

    Unformat

    1. Select text
    2. Command Palette -> Join Lines

    Show the pics

    enter image description here enter image description here

    1. Right click somewhere in the content area (text) for the file
    2. Select Format Document from the menu:
      • Windows: Alt+Shift+F
      • Linux: Alt+Shift+I
      • macOS: ++F

    Enter image description here

  2. The right key combination is Shift + Alt + F.

  3. Visual Studio Code 1.6.1 supports “Format On Save” which will automatically pick up relevant installed formatter extensions and format the whole document on each save.

    Enable “Format On Save” by setting

    "editor.formatOnSave": true 

    And there are available keyboard shortcuts (Visual Studio Code 1.7 and above):

    Format the whole document: Shift + Alt + F

    Format Selection only: Ctrl + K, Ctrl + F

  4. On Linux it is Ctrl + Shift + I.

    On Windows it is Alt + Shift + F. Tested with HTML/CSS/JavaScript and Visual Studio Code 1.18.0.

    For other languages, you might need to install a specific language package.

  5. For Fedora

    1. Click File -> Preferences -> Keyboard shortcuts.
    2. Under Default Keyboard Shortcuts, search (Ctrl + F) for editor.action.format.

    Mine read "key": "ctrl+shift+i"

    You can change it as well. Refer to this answer on how to… or if you are feeling a little lazy to scroll up:


    You can add a keybinding in “Preferences->Keyboard shortcuts”

    { "key": "cmd+k cmd+d", "command": "editor.action.format" }

    Or Visual Studio like:

    { "key": "ctrl+k ctrl+d", "command": "editor.action.format" }


    Please note: cmd key is only for Macs. For Windows and Fedora (Windows keyboard) use Ctrl


    EDIT:

    As per Visual Code version 1.28.2 this is what I found.

    editor.action.format no longer exists. It has now been replaced by editor.action.formatDocument and editor.action.formatSelection.

    Type editor.action.format in the search box to view existing shortcuts.

    To change the key combinations follow these steps:

    1. Click editor.action.formatDocument or editor.action.formatSelection
    2. A pen like icon appears to the left – click it.
    3. A pop-up appears. Press the desired key combination and press enter.
  6. On Ubuntu it’s Ctrl + Shift + I.

  7. Menu FilePreferencesSettings

    "editor.formatOnType": true 

    When you enter the semicolon, it’s going to be formatted.

    Alternatively, you can also use "editor.formatOnSave": true.

  8. Just right-click on the text and select “Format code”.

    Visual Studio Code uses js-beautify internally, but it lacks the ability to modify the style you wish to use. The extension “beautify” lets you add settings.

  9. Shift + Alt + F does the job just fine in 1.17.2 and above.

  10. For some reason Alt + Shift + F didn’t work for me on Mac Visual Studio Code 1.3.1, and actually the command “Format Document” don’t worked at all. But command Formatter worked very well.

    So you can use Command + Shift + P and type Formatter or make your own shortcut in menu FilePreferencesKeyboard ShortcutsCommand + K Command + S then type Formatter and add your shortcut.

    See an example:

    Enter image description here

  11. Formatting code in Visual Studio.

    I have tried to format in Windows 8.

    Just follow the screenshots below.

    1. Click on View on the top menu bar, and then click on Command Palette.

      Enter image description here

    2. Then a textbox would appear where we need type Format

      Shift + Alt + F

      Enter image description here

  12. In Visual Studio Code, Shift+Alt+F is doing what Ctrl+K+D is doing in Visual Studio.

  13. The simplest way I use in Visual Studio Code (Ubuntu) is:

    Select the text which you want to format with the mouse.

    Right click and choose “Format selection”.

  14. On Mac, Shift + Alt + F works for me.

    You can always check the key bindings in the menu:

    Menu FilePreferencesKeyboard Shortcuts and filter by keyword ‘format’.

  15. While changing the default behavior for Visual Studio Code requires an extension, you may override the default behavior in the workspace or user level. It works for most of the supported languages (I can guarantee HTML, JavaScript, and C#).

    Workspace level

    Benefits

    • Does not require an extension
    • Can be shared among teams

    Outcomes

    • .vscode/settings.json is created in the project root folder

    How To?

    1. Go to: Menu FilePreferencesWorkspace Settings

    2. Add and save "editor.formatOnType": true to settings.json (which overrides default behavior for the project you work on by creating .vscode/settings.json file).

      How it looks

    User environment level

    Benefits

    • Does not requires extension
    • Personal development environment tweeking to rule them all (settings:))

    Outcomes

    • User’s settings.json is modified (see location by operating system below)

    How To?

    1. Go to: menu FilePreferencesUser Settings

    2. Add or change the value of "editor.formatOnType": false to "editor.formatOnType": true in the user settings.json

    Your Visual Studio Code user’s settings.json location is:

    Settings file locations depending on your platform, the user settings file is located here:

    • Windows: %APPDATA%CodeUsersettings.json
    • Mac: $HOME/Library/Application Support/Code/User/settings.json
    • Linux: $HOME/.config/Code/User/settings.json The workspace setting file is located under the .vscode folder in your project.

    More details may be found here.

  16. Select the text, right click on the selection, and select the option “command palette”:
  17. Just install Visual Studio Keymap (Visual Studio Keymap for Visual Studio Code) by Microsoft. Problem Solved. :p

  18. In Mac, use +K and then +F.

  19. You have to install the appropriate plug-in first (i.e., XML, C#, etc.).

    Formatting won’t become available until you’ve installed the relevant plugin, and saved the file with an appropriate extension.

  20. Visual Studio Code on Linux:

    Ctrl + [ to unindent block of code and

    Ctrl + ] to make a mass indentation

  21. For those that want to customize what JavaScript files to format, you can use the any extension on the JSfiles property. The same applies to HTML.

    { "beautify.onSave": true, "beautify.JSfiles": ["js", "json", "jsbeautifyrc", "jshintrc", "ts"], "beautify.HTMLfiles": ["htm", "html"] } 

    This will enable beautify on save for TypeScript, and you can add in XML to the HTML option.

  22. If you want to custom the style of format-document, you should use Beautify extention.

    Refer to this screenshot:

    img

  23. Not this one. Use this:

    Menu FilePreferencesWorkspace Settings, “editor.formatOnType”: true

  24. Use extension…

    Enables auto formatting of the code when you save a file.

    Launch Visual Studio Code and Quick Open (Ctrl + P), paste the following command, and press Enter.

    ext install format-on-save

    https://marketplace.visualstudio.com/items?itemName=gyuha.format-on-save

  25. ‘This sure seems like it has been over-complicated. From my experience, there are just three key things to get Excel to close properly:

    1: make sure there are no remaining references to the excel application you created (you should only have one anyway; set it to null)

    2: call GC.Collect()

    3: Excel has to be closed, either by the user manually closing the program, or by you calling Quit on the Excel object. (Note that Quit will function just as if the user tried to close the program, and will present a confirmation dialog if there are unsaved changes, even if Excel is not visible. The user could press cancel, and then Excel will not have been closed.)

    1 needs to happen before 2, but 3 can happen anytime.

    One way to implement this is to wrap the interop Excel object with your own class, create the interop instance in the constructor, and implement IDisposable with Dispose looking something like

    That will clean up excel from your program’s side of things. Once Excel is closed (manually by the user or by you calling Quit) the process will go away. If the program has already been closed, then the process will disappear on the GC.Collect() call.

    (I’m not sure how important it is, but you may want a GC.WaitForPendingFinalizers() call after the GC.Collect() call but it is not strictly necessary to get rid of the Excel process.)

    This has worked for me without issue for years. Keep in mind though that while this works, you actually have to close gracefully for it to work. You will still get accumulating excel.exe processes if you interrupt your program before Excel is cleaned up (usually by hitting “stop” while your program is being debugged).’

  26. Here is a really easy way to do it:

    [DllImport("User32.dll")] static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); ... int objExcelProcessId = 0; Excel.Application objExcel = new Excel.Application(); GetWindowThreadProcessId(new IntPtr(objExcel.Hwnd), out objExcelProcessId); Process.GetProcessById(objExcelProcessId).Kill(); 
  27. My answer is late and its only purpose is to support the solution proposed by Govert.

    Short version:

    • Write a local function with no global variables and no arguments executing the COM stuff.

    • Call the COM function in a wrapping function that calls the COM function and cleans thereafter.

    Long version:

    You are not using .Net to count references of COM objects and to release them yourself in the correct order. Even C++ programmers don’t do that any longer by using smart pointers. So, forget about Marshal.ReleaseComObject and the funny one dot good two dots bad rule. The GC is happy to do the chore of releasing COM objects if you null out all references to COM objects that are no longer needed. The easiest way is to handle COM objects in a local function, with all variables for COM objects naturally going out of scope at the end. Due to some strange features of the debugger pointed out in the brilliant answers of Hans Passant mentioned in the accepted answers Post Mortem, the cleanup should be delegated to a wrapping function that also calls the executing function. So, COM objects like Excel or Word need two functions, one that does the actual job and a wrapper that calls this function and calls the GC afterwards like Govert did, the only correct answer in this thread. To show the principle I use a wrapper suitable for all functions doing COM stuff. Except for this extension, my code is just the C# version of Govert’s code. In addition, I stopped the process for 6 seconds so that you can check out in the Task Manager that Excel is no longer visible after Quit() but lives on as a zombie until the GC puts an end to it.

    using Excel = Microsoft.Office.Interop.Excel; public delegate void WrapCom(); namespace GCTestOnOffice{ class Program{ static void DoSomethingWithExcel(){ Excel.Application ExcelApp = new(); Excel.Workbook Wb = ExcelApp.Workbooks.Open(@"D:\Sample.xlsx"); Excel.Worksheet NewWs = Wb.Worksheets.Add(); for (int i = 1; i < 10; i++){ NewWs.Cells[i, 1] = i;} Wb.Save(); ExcelApp.Quit(); } static void TheComWrapper(WrapCom wrapCom){ wrapCom(); //All COM objects are out of scope, ready for the GC to gobble //Excel is no longer visible, but the process is still alive, //check out the Task-Manager in the next 6 seconds Thread.Sleep(6000); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); //Check out the Task-Manager, the Excel process is gone } static void Main(string[] args){ TheComWrapper(DoSomethingWithExcel); } } } 
  28. Just to add another solution to the many listed here, using C++/ATL automation (I imagine you could use something similar from VB/C#??)

    Excel::_ApplicationPtr pXL = ... : SendMessage ( ( HWND ) m_pXL->GetHwnd ( ), WM_DESTROY, 0, 0 ) ; 

    This works like a charm for me…

  29. [DllImport("user32.dll")] private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
  30. So far it seems all answers involve some of these:

    1. Kill the process
    2. Use GC.Collect()
    3. Keep track of every COM object and release it properly.

    Which makes me appreciate how difficult this issue is 🙂

    I have been working on a library to simplify access to Excel, and I am trying to make sure that people using it won’t leave a mess (fingers crossed).

    Instead of writing directly on the interfaces Interop provides, I am making extension methods to make live easier. Like ApplicationHelpers.CreateExcel() or workbook.CreateWorksheet(“mySheetNameThatWillBeValidated”). Naturally, anything that is created may lead to an issue later on cleaning up, so I am actually favoring killing the process as last resort. Yet, cleaning up properly (third option), is probably the least destructive and most controlled.

    So, in that context I was wondering whether it wouldn’t be best to make something like this:

    public abstract class ReleaseContainer<T> { private readonly Action<T> actionOnT; protected ReleaseContainer(T releasible, Action<T> actionOnT) { this.actionOnT = actionOnT; this.Releasible = releasible; } ~ReleaseContainer() { Release(); } public T Releasible { get; private set; } private void Release() { actionOnT(Releasible); Releasible = default(T); } } 

    I used ‘Releasible’ to avoid confusion with Disposable. Extending this to IDisposable should be easy though.

    An implementation like this:

    public class ApplicationContainer : ReleaseContainer<Application> { public ApplicationContainer() : base(new Application(), ActionOnExcel) { } private static void ActionOnExcel(Application application) { application.Show(); // extension method. want to make sure the app is visible. application.Quit(); Marshal.FinalReleaseComObject(application); } } 

    And one could do something similar for all sorts of COM objects.

    In the factory method:

     public static Application CreateExcelApplication(bool hidden = false) { var excel = new ApplicationContainer().Releasible; excel.Visible = !hidden; return excel; } 

    I would expect that every container will be destructed properly by the GC, and therefore automatically make the call to Quit and Marshal.FinalReleaseComObject.

    Comments? Or is this an answer to the question of the third kind?

  31. There i have an idea,try to kill the excel process you have opened:

    1. before open an excelapplication,get all the process ids named oldProcessIds.
    2. open the excelapplication.
    3. get now all the excelapplication process ids named nowProcessIds.
    4. when need to quit,kill the except ids between oldProcessIds and nowProcessIds.

      private static Excel.Application GetExcelApp() { if (_excelApp == null) { var processIds = System.Diagnostics.Process.GetProcessesByName("EXCEL").Select(a => a.Id).ToList(); _excelApp = new Excel.Application(); _excelApp.DisplayAlerts = false; _excelApp.Visible = false; _excelApp.ScreenUpdating = false; var newProcessIds = System.Diagnostics.Process.GetProcessesByName("EXCEL").Select(a => a.Id).ToList(); _excelApplicationProcessId = newProcessIds.Except(processIds).FirstOrDefault(); } return _excelApp; } public static void Dispose() { try { _excelApp.Workbooks.Close(); _excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelApp); _excelApp = null; GC.Collect(); GC.WaitForPendingFinalizers(); if (_excelApplicationProcessId != default(int)) { var process = System.Diagnostics.Process.GetProcessById(_excelApplicationProcessId); process?.Kill(); _excelApplicationProcessId = default(int); } } catch (Exception ex) { _excelApp = null; } } 
  32. Tested with Microsoft Excel 2016

    A really tested solution.

    To C# Reference please see: https://stackoverflow.com/a/1307180/10442623

    To VB.net Reference please see: https://stackoverflow.com/a/54044646/10442623

    1 include the class job

    2 implement the class to handle the apropiate dispose of excel proces

  33. I had this same problem getting PowerPoint to close after newing up the Application object in my VSTO AddIn. I tried all the answers here with limited success.

    This is the solution I found for my case – DONT use ‘new Application’, the AddInBase base class of ThisAddIn already has a handle to ‘Application’. If you use that handle where you need it (make it static if you have to) then you don’t need to worry about cleaning it up and PowerPoint won’t hang on close.

  34. Of the three general strategies considered in other answers, killing the excel process is clearly a hack, whereas invoking the garbage collector is a brutal shotgun approach meant to compensate for incorrect deallocation of COM-objects. After lots of experimentation and rewriting the management of COM objects in my version-agnostic and late-bound wrapper, I have come to the conclusion that accurate and timely invocations of Marshal.ReleaseComObject() is the most efficient and elegant strategy. And no, you do not ever need FinalReleaseComObject(), because in a well-writtin program each COM acquired on once and therefore requires a single decrement of the reference counter.

    One shall make sure to release every single COM object, preferably as soon as it is no longer needed. But it is perfectly possible to release everything right after quitting the Excel application, at the only expense of higher memory usage. Excel will close as expected as long as one does not loose or forget to release a COM object.

    The simplest and most obvious aid in the process is wrapping every interop object into a .NET class implementing IDisposable, where the Dispose() method invokes ReleaseComObject() on its interop object. Doing it in the destructor, as proposed in here, makes no sense because destructors are non-deterministic.

    Show below is our wrapper’s method that obtains a cell from WorkSheet bypassing the intermediate Cells member. Notice the way it disposes of the intermediate object after use:

    public ExcelRange XCell( int row, int col) { ExcelRange anchor, res; using( anchor = Range( "A1") ) { res = anchor.Offset( row - 1, col - 1 ); } return res; } 

    The next step may be a simple memory manager that will keep track of every COM object obtained and make sure to release it after Excel quits if the user prefers to trade some RAM usage for simpler code.

    Futher reading

    1. How to properly release Excel COM objects,
    2. Releasing COM objects: Garbage Collector vs. Marshal.RelseaseComObject.
  35. I really like when things clean up after them selves… So I made some wrapper classes that do all the cleanup for me! These are documented further down.

    The end code is quite readable and accessible. I haven’t yet found any phantom instances of Excel running after I Close() the workbooks and Quit() the application (besides where I debug and close the app mid process).

    function void OpenCopyClose() { var excel = new ExcelApplication(); var workbook1 = excel.OpenWorkbook("C:Tempfile1.xslx", readOnly: true); var readOnlysheet = workbook1.Worksheet("sheet1"); var workbook2 = excel.OpenWorkbook("C:Tempfile2.xslx"); var writeSheet = workbook.Worksheet("sheet1"); // do all the excel manipulation // read from the first workbook, write to the second workbook. var a1 = workbook1.Cells[1, 1]; workbook2.Cells[1, 1] = a1 // explicit clean-up workbook1.Close(false); workbook2 .Close(true); excel.Quit(); } 

    Note: You can skip the Close() and Quit() calls but if you are writing to an Excel document you will at least want to Save(). When the objects go out of scope (the method returns) the class finalizers will automatically kick in and do any cleanup. Any references to COM objects from the Worksheet COM object will automatically be managed and cleaned up as long as you are careful with the scope of your variables, eg keep variables local to the current scope only when storing references to COM objects. You can easily copy values you need to POCOs if you need, or create additional wrapper classes as discussed below.

    To manage all this, I have created a class, DisposableComObject, that acts as a wrapper for any COM object. It implements the IDisposable interface and also contains a finalizer for those that don’t like using.

    The Dispose() method calls Marshal.ReleaseComObject(ComObject) and then sets the ComObjectRef property to null.

    The object is in a disposed state when the private ComObjectRef property is null.

    If the ComObject property is accessed after being disposed, a ComObjectAccessedAfterDisposeException exception is thrown.

    The Dispose() method can be called manually. It is also called by the finalizer, at the conclusion of a using block, and for using var at the conclusion of the scope of that variable.

    The top level classes from Microsoft.Office.Interop.Excel, Application, Workbook, and Worksheet, get their own wrapper classes where each are subclasses of DisposableComObject

    Here is the code:

    /// <summary> /// References to COM objects must be explicitly released when done. /// Failure to do so can result in odd behavior and processes remaining running after the application has stopped. /// This class helps to automate the process of disposing the references to COM objects. /// </summary> public abstract class DisposableComObject : IDisposable { public class ComObjectAccessedAfterDisposeException : Exception { public ComObjectAccessedAfterDisposeException() : base("COM object has been accessed after being disposed") { } } /// <summary>The actual COM object</summary> private object ComObjectRef { get; set; } /// <summary>The COM object to be used by subclasses</summary> /// <exception cref="ComObjectAccessedAfterDisposeException">When the COM object has been disposed</exception> protected object ComObject => ComObjectRef ?? throw new ComObjectAccessedAfterDisposeException(); public DisposableComObject(object comObject) => ComObjectRef = comObject; /// <summary> /// True, if the COM object has been disposed. /// </summary> protected bool IsDisposed() => ComObjectRef is null; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); // in case a subclass implements a finalizer } /// <summary> /// This method releases the COM object and removes the reference. /// This allows the garbage collector to clean up any remaining instance. /// </summary> /// <param name="disposing">Set to true</param> protected virtual void Dispose(bool disposing) { if (!disposing || IsDisposed()) return; Marshal.ReleaseComObject(ComObject); ComObjectRef = null; } ~DisposableComObject() { Dispose(true); } } 

    There is also a handy generic subclass which makes usage slightly easier.

    public abstract class DisposableComObject<T> : DisposableComObject { protected new T ComObject => (T)base.ComObject; public DisposableComObject(T comObject) : base(comObject) { } } 

    Finally, we can use DisposableComObject<T> to create our wrapper classes for the Excel interop classes.

    The ExcelApplication subclass has a reference to a new Excel application instance and is used to open workbooks.

    OpenWorkbook() returns an ExcelWorkbook which is also a subclass of DisposableComObject.

    Dispose() has been overridden to quit the Excel application before calling the base Dispose() method. Quit() is an alias of Dispose().

    public class ExcelApplication : DisposableComObject<Application> { public class OpenWorkbookActionCancelledException : Exception { public string Filename { get; } public OpenWorkbookActionCancelledException(string filename, COMException ex) : base($"The workbook open action was cancelled. {ex.Message}", ex) => Filename = filename; } /// <summary>The actual Application from Interop.Excel</summary> Application App => ComObject; public ExcelApplication() : base(new Application()) { } /// <summary>Open a workbook.</summary> public ExcelWorkbook OpenWorkbook(string filename, bool readOnly = false, string password = null, string writeResPassword = null) { try { var workbook = App.Workbooks.Open(Filename: filename, UpdateLinks: (XlUpdateLinks)0, ReadOnly: readOnly, Password: password, WriteResPassword: writeResPassword, ); return new ExcelWorkbook(workbook); } catch (COMException ex) { // If the workbook is already open and the request mode is not read-only, the user will be presented // with a prompt from the Excel application asking if the workbook should be opened in read-only mode. // This exception is raised when when the user clicks the Cancel button in that prompt. throw new OpenWorkbookActionCancelledException(filename, ex); } } /// <summary>Quit the running application.</summary> public void Quit() => Dispose(true); /// <inheritdoc/> protected override void Dispose(bool disposing) { if (!disposing || IsDisposed()) return; App.Quit(); base.Dispose(disposing); } } 

    ExcelWorkbook also subclasses DisposableComObject<Workbook> and is used to open worksheets.

    The Worksheet() methods returns ExcelWorksheet which, you guessed it, is also an subclass of DisposableComObject<Workbook>.

    The Dispose() method is overridden and fist closes the worksheet before calling the base Dispose().

    NOTE: I’ve added some extension methods which is uses to iterate over Workbook.Worksheets. If you get compile errors, this is why. Ill add the extension methods at the end.

    public class ExcelWorkbook : DisposableComObject<Workbook> { public class WorksheetNotFoundException : Exception { public WorksheetNotFoundException(string message) : base(message) { } } /// <summary>The actual Workbook from Interop.Excel</summary> Workbook Workbook => ComObject; /// <summary>The worksheets within the workbook</summary> public IEnumerable<ExcelWorksheet> Worksheets => worksheets ?? (worksheets = Workbook.Worksheets.AsEnumerable<Worksheet>().Select(w => new ExcelWorksheet(w)).ToList()); private IEnumerable<ExcelWorksheet> worksheets; public ExcelWorkbook(Workbook workbook) : base(workbook) { } /// <summary> /// Get the worksheet matching the <paramref name="sheetName"/> /// </summary> /// <param name="sheetName">The name of the Worksheet</param> public ExcelWorksheet Worksheet(string sheetName) => Worksheet(s => s.Name == sheetName, () => $"Worksheet not found: {sheetName}"); /// <summary> /// Get the worksheet matching the <paramref name="predicate"/> /// </summary> /// <param name="predicate">A function to test each Worksheet for a macth</param> public ExcelWorksheet Worksheet(Func<ExcelWorksheet, bool> predicate, Func<string> errorMessageAction) => Worksheets.FirstOrDefault(predicate) ?? throw new WorksheetNotFoundException(errorMessageAction.Invoke()); /// <summary> /// Returns true of the workbook is read-only /// </summary> public bool IsReadOnly() => Workbook.ReadOnly; /// <summary> /// Save changes made to the workbook /// </summary> public void Save() { Workbook.Save(); } /// <summary> /// Close the workbook and optionally save changes /// </summary> /// <param name="saveChanges">True is save before close</param> public void Close(bool saveChanges) { if (saveChanges) Save(); Dispose(true); } /// <inheritdoc/> protected override void Dispose(bool disposing) { if (!disposing || IsDisposed()) return; Workbook.Close(); base.Dispose(disposing); } } 

    Finally, the ExcelWorksheet.

    UsedRows() simply returns an enumerable of unwrapped Microsoft.Office.Interop.Excel.Range objects. I haven’t yet encountered a situation where COM objects accessed from properties of the Microsoft.Office.Interop.Excel.Worksheet object need to manually wrapped like was needed with Application, Workbook, and Worksheet. These all seem to clean them selves up automatically. Mostly, I was just iterating over Ranges and getting or setting values, so my particular use-case isn’t as advanced as the available functionality.

    There is no override of Dispose() in this case as no special action needs to take place for worksheets.

    public class ExcelWorksheet : DisposableComObject<Worksheet> { /// <summary>The actual Worksheet from Interop.Excel</summary> Worksheet Worksheet => ComObject; /// <summary>The worksheet name</summary> public string Name => Worksheet.Name; // <summary>The worksheets cells (Unwrapped COM object)</summary> public Range Cells => Worksheet.Cells; public ExcelWorksheet(Worksheet worksheet) : base(worksheet) { } /// <inheritdoc cref="WorksheetExtensions.UsedRows(Worksheet)"/> public IEnumerable<Range> UsedRows() => Worksheet.UsedRows().ToList(); } 

    It is possible to add even more wrapper classes. Just add additional methods to ExcelWorksheet as needed and return the COM object in a wrapper class. Just copy what we did when wrapping the workbook via ExcelApplication.OpenWorkbook() and ExcelWorkbook.WorkSheets.

    Some useful extension methods:

    public static class EnumeratorExtensions { /// <summary> /// Converts the <paramref name="enumerator"/> to an IEnumerable of type <typeparamref name="T"/> /// </summary> public static IEnumerable<T> AsEnumerable<T>(this IEnumerable enumerator) { return enumerator.GetEnumerator().AsEnumerable<T>(); } /// <summary> /// Converts the <paramref name="enumerator"/> to an IEnumerable of type <typeparamref name="T"/> /// </summary> public static IEnumerable<T> AsEnumerable<T>(this IEnumerator enumerator) { while (enumerator.MoveNext()) yield return (T)enumerator.Current; } /// <summary> /// Converts the <paramref name="enumerator"/> to an IEnumerable of type <typeparamref name="T"/> /// </summary> public static IEnumerable<T> AsEnumerable<T>(this IEnumerator<T> enumerator) { while (enumerator.MoveNext()) yield return enumerator.Current; } } public static class WorksheetExtensions { /// <summary> /// Returns the rows within the used range of this <paramref name="worksheet"/> /// </summary> /// <param name="worksheet">The worksheet</param> public static IEnumerable<Range> UsedRows(this Worksheet worksheet) => worksheet.UsedRange.Rows.AsEnumerable<Range>(); } 
  36. Excel is not designed to be programmed via C++ or C#. The COM API is specifically designed to work with Visual Basic, VB.NET, and VBA.

    Also all the code samples on this page are not optimal for the simple reason that each call must cross a managed/unmanaged boundary and further ignore the fact that the Excel COM API is free to fail any call with a cryptic HRESULT indicating the RPC server is busy.

    The best way to automate Excel in my opinion is to collect your data into as big an array as possible / feasible and send this across to a VBA function or sub (via Application.Run) which then performs any required processing. Furthermore – when calling Application.Run – be sure to watch for exceptions indicating excel is busy and retry calling Application.Run.

  37. This is the only way that really works for me

     foreach (Process proc in System.Diagnostics.Process.GetProcessesByName("EXCEL")) { proc.Kill(); } 

Tasg: code-formatting, visual-studio-code