We can export Excel files without using any extra library, using C#, we have to use just "\t" for the next column and "\n" for the new row.
If we don't need any control over the styles, formatting etc. then this approach is very good for us.
Here, in this approach, we will get plain data in our spreadsheet.
In my earlier article, I explained about exporting data to Excel file in ASP.NET, using EPPlus Library. We have many features, while using EPPlus DLL.
EPPlus supports many properties like cell ranges, cell styling (border, color, fill, font, number, alignments), charts, pictures, shapes, comments, tables, protection, encryption, pivot tables, data validation, conditional formatting, formula calculation etc.
Please click here for learning about EPPlus DLL for exporting Excel file from my previous article.
We are going to take a sample example for now but we can take the data from a database or anywhere else in real time, as per our requirement.
Below is the code for ASP.NET button to download the exported file.
- <asp:Button ID="ExportExcel" runat="server" Text="Export Excel" OnClick="ExportExcel_Click" />

Now, we are going to write the code behind method to download the Excel sheet.
- protected void ExportExcel_Click(object sender, EventArgs e)
- {
- var employees = new[]{
- new{ Id="101", Name="Vivek", Address="Hyderabad" },
- new{ Id="102", Name="Ranjeet", Address="Hyderabad" },
- new{ Id="103", Name="Sharath", Address="Hyderabad" },
- new{ Id="104", Name="Ganesh", Address="Hyderabad" },
- new{ Id="105", Name="Gajanan", Address="Hyderabad" },
- new{ Id="106", Name="Ashish", Address="Hyderabad" }
- };
- string excelName = "employees";
- Response.ClearContent();
- Response.AddHeader("content-disposition", "attachment;filename=" + excelName + ".xls");
- Response.AddHeader("Content-Type", "application/vnd.ms-excel");
- //Header for table records
- //
- Response.Write("Id");
- Response.Write("\t");
- Response.Write("Name");
- Response.Write("\t");
- Response.Write("Address");
- Response.Write("\t");
- Response.Write("\n");
- //Body for table records
- //
- foreach (var employee in employees)
- {
- Response.Write(employee.Name);
- Response.Write("\t");
- Response.Write(employee.Id);
- Response.Write("\t");
- Response.Write(employee.Address);
- Response.Write("\t");
- Response.Write("\n");
- }
- Response.End();
- }


Nikhil ChopadePosted Jul 21, 2020, 2:36 AM
How to write file with xlsx e.g. Report.xlsx
Frank MillerPosted Nov 22, 2019, 10:59 AM
Without extra library you can do this work if you use ZetExcel tools. It's a verified method. Thanks