How to create Excel file using C# without Microsoft office & interop?


Hello, I would like to know how can I generate excel file in my C# project without installing Microsoft Excel/office or interop on server?Can you please provide me step by step guide or easy tutorial links, anything will work, thanks.


Asked by:- Sam
1
: 10622 At:- 5/28/2018 3:40:39 PM
C# Excel without office







3 Answers
profileImage Answered by:- jaya

You can create Excel file in C# without using Office or Interop by using Epplus in your project, here are the steps

  1. Install EPPlus in your project using Nuget Package manager console.
    PM > Install-Package EPPlus
  2. Now, you need to create it's object & empty worksheet
    // Create the package and make sure you wrap it in a using statement
    using (ExcelPackage excel = new ExcelPackage())
    {
         // add a new worksheet to the empty workbook
         ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Test");
    
        // --------- Creating file, Data and styling goes here -------------- //
    
    }?
  3. Add data/header in Worksheet using C#
    using (ExcelPackage excel = new ExcelPackage())
                {
    
                    //Add Worksheets in Excel file
                    excel.Workbook.Worksheets.Add("TestSheet1");
    
                   
                    //Create Excel file in Uploads folder of your project
                    FileInfo excelFile = new FileInfo(Server.MapPath("~/ExcelFile/test.xlsx"));
    
                    //Add header row columns name in string list array
                    var headerRow = new List<string[]>()
                      {
                        new string[] { "Full Name","Email" }
                      };
    
                    // Get the header range
                    string Range = "A1:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + "1";
    
                    // get the workSheet in which you want to create header
                    var worksheet = excel.Workbook.Worksheets["TestSheet1"];
    
                    // Popular header row data
                    worksheet.Cells[Range].LoadFromArrays(headerRow);
    
                    //show header cells with different style
                    worksheet.Cells[Range].Style.Font.Bold = true;
                    worksheet.Cells[Range].Style.Font.Size = 16;
                    worksheet.Cells[Range].Style.Font.Color.SetColor(System.Drawing.Color.DarkBlue);
    
                    //Now add some data in rows for each column
                    var Data = new List<object[]>()
                        {
                          new object[] {"Test","test@gmail.com"},
                          new object[] {"Test2","test2@gmail.com"},
                          new object[] {"Test3","test3@gmail.com"},
    
                        };
    
                    //add the data in worksheet, here .Cells[2,1] 2 is rowNumber while 1 is column number
                    worksheet.Cells[2, 1].LoadFromArrays(Data);
    
                    //Save Excel file
                    excel.SaveAs(excelFile);
                }?
  4. Here is the complete tutorial to generate Excel file using EPPlus in MVC/C# https://qawithexperts.com/article/asp-net/create-excel-file-using-c-without-using-office-or-interop/138
  5. Another article, which generates excel with data from the database using EPPlus https://qawithexperts.com/article/asp.net/export-data-to-excel-xlsx-xls-file-using-aspnet-mvc-c/91

Thanks.

2
At:- 5/31/2018 6:36:49 PM Updated at:- 7/20/2022 3:34:42 PM
Good answer, I have checked the article in step 4/5 which provides EPPlus complete details, thanks. 0
By : Sam - at :- 6/1/2018 2:30:39 PM


profileImage Answered by:- vikas_jk

You can also check this article which decribes about how to write to excel in C# using EPPlus and add images, styling etc.

How to write to excel in C# using Epplus? ( Text, adding images / styling in Excel)

Thanks

2
At:- 7/22/2020 6:55:29 AM
Thanks for adding this, it is much better to use EPPlus to create Excel files without using Interop. 0
By : neena - at :- 11/25/2021 3:30:37 PM


profileImage Answered by:- Steven P

there is an easy solution for this. You can use ZetExcel. I hope you will be benefited from this tool. just try it and get the result.

0
At:- 12/8/2019 12:02:08 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use