Using C# to Create Excel Files in .Net - KTEN.com - Texoma news, weather and sports

Using C# to Create Excel Files in .Net

Posted:

Originally posted on https://ironsoftware.com/csharp/excel/tutorials/create-excel-file-net/

 

Creating Excel files in C# can be simple even without dependency on the legacy Microsoft.Office.Interop.Excel library.

In this tutorial we will use IronXL to create an Excel WorkBook file on any platform that supports .Net Framework 4.5 or .Net Core.

Installation

Before we start we need to install the IronXL Excel NuGet Package.

PM > Install-Package IronXL.Excel

https://www.nuget.org/packages/IronXL.Excel/

As an alternative, the IronXL.Dll can be downloaded and added to your project.

Create and save an Excel File

  1. using IronXL;
  2. //default file format is XLSX, we can override it using CreatingOptions
  3. WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
  4. var sheet = workbook.CreateWorkSheet("example_sheet");
  5. sheet["A1"].Value = "Example";
  6. //set value to multiple cells
  7. sheet["A2:A4"].Value = 5;
  8. sheet["A5"].Style.SetBackgroundColor("#f0f0f0");
  9. //set style to multiple cells
  10. sheet["A5:A6"].Style.Font.Bold = true;
  11. //set formula
  12. sheet["A6"].Value = "=SUM(A2:A4)";
  13. if (sheet["A6"].IntValue == sheet["A2:A4"].IntValue)
  14. {
  15. Console.WriteLine("Basic test passed");
  16. }
  17. workbook.SaveAs("example_workbook.xlsx");

Moving Forward

To learn more about creating Excel files in C# .Net, we encourage you to work with this code sample and explore the object reference to move forwards to meet you own project’s specific need.

Information contained on this page is provided by an independent third-party content provider. Frankly and this Site make no warranties or representations in connection therewith. If you are affiliated with this page and would like it removed please contact pressreleases@franklymedia.com