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.
You can create Excel file in C# without using Office or Interop by using Epplus in your project, here are the steps
PM > Install-Package EPPlus
// 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 -------------- //
}?
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);
}?
Thanks.
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
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.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly