The most common and widely used spreadsheet file formats are XLS, XLSX, and ODS. The famous Microsoft Excel and OpenOffice Calc support these formats and we normally use these formats for maintaining accounts and different spreadsheets. Therefore, as a developer, we widely need to edit Excel files within our applications programmatically. In this article, we will discuss how to edit Excel files in C# using the .NET API.

The following are the topics discussed briefly in this article:

.NET API for Excel Spreadsheets Editing and Automation

GroupDocs showcases the .NET API for spreadsheet editing. I will use it in the C# examples of this article. It is the document editing API and allows developers to load, edit, and save various document formats using WYSIWYG HTML editors. In addition to the XLS, XLSX, and ODS spreadsheet formats, the API supports editing of various other spreadsheets & MS Excel supported formats like CSV, TSV, DSV, XLT, XLTX, XLTM, XLSM, XLSB, XLAM, SXC, SpreadsheetML, FODS, DIF.

Download the DLLs or MSI installer from the downloads section or install the API in your .NET application via NuGet.

PM> Install-Package GroupDocs.Editor

Edit Excel Files in C#

Hope you have successfully referenced the API. Now you can quickly start editing your Excel documents. The following steps will let you edit the spreadsheet documents using C#.

  • Load the Excel file.
  • Edit accordingly with options.
  • Save the edited document.

Load the Excel Spreadsheet

Firstly, load the spreadsheet by providing the document path/stream and the password (if the document is password protected).

Edit the Excel File

After loading, you can edit the loaded spreadsheet as needed. Now we are going to replace all the occurrences of “Old Company Name” with “New Company Name” in the first tab of the spreadsheet. The following steps allow you to edit the excel file accordingly in C#.

  • Load the Excel file using Editor and loading options.
  • Prepare the Spreadsheet Editing Options to extract the exact sheet/tab.
  • Extract the content of the tab.
  • Modify the tab’s content.
  • You can extract the images and all the resources from the selected tab.
  • Create the new EditableDocument using the modified content.
  • Save the editing spreadsheet using the appropriate Save() method.

The following C# source code edits the excel file and changes its content.

Save the Edited Excel File with Options

After editing, while saving the edited spreadsheet content, you can set various options. These options include; setting password, output format, protection, etc. I am setting the above options in the below-mentioned code and saving the edited spreadsheet as a password-protected and write-protected XLSX file.

Get a Free License

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion

To conclude, we discussed how to edit Excel documents in C# using document editing API for .NET applications. You can use the API with the WYSIWYG editors for the visual editing of your documents. After that, you can move ahead to build your own online spreadsheet editor.

For more details, options, and examples, you can visit the documentation and the GitHub repository. For further queries, contact the support on the forum.

See Also