Export Data To Excel File In ASP.NET Without Any Extra Library

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.

  1. <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.
  1. protected void ExportExcel_Click(object sender, EventArgs e)  
  2.         {  
  3.             var employees = new[]{  
  4.                                new{ Id="101", Name="Vivek", Address="Hyderabad" },  
  5.                                new{ Id="102", Name="Ranjeet", Address="Hyderabad" },  
  6.                                new{ Id="103", Name="Sharath", Address="Hyderabad" },  
  7.                                new{ Id="104", Name="Ganesh", Address="Hyderabad" },  
  8.                                new{ Id="105", Name="Gajanan", Address="Hyderabad" },  
  9.                                new{ Id="106", Name="Ashish", Address="Hyderabad" }  
  10.                       };  
  11.   
  12.             string excelName = "employees";  
  13.   
  14.             Response.ClearContent();  
  15.             Response.AddHeader("content-disposition""attachment;filename=" + excelName + ".xls");  
  16.             Response.AddHeader("Content-Type""application/vnd.ms-excel");  
  17.   
  18.             //Header for table records  
  19.             //  
  20.             Response.Write("Id");  
  21.             Response.Write("\t");  
  22.             Response.Write("Name");  
  23.             Response.Write("\t");  
  24.             Response.Write("Address");  
  25.             Response.Write("\t");  
  26.   
  27.             Response.Write("\n");  
  28.   
  29.             //Body for table records  
  30.             //  
  31.             foreach (var employee in employees)  
  32.             {  
  33.                 Response.Write(employee.Name);  
  34.                 Response.Write("\t");  
  35.                 Response.Write(employee.Id);  
  36.                 Response.Write("\t");  
  37.                 Response.Write(employee.Address);  
  38.                 Response.Write("\t");  
  39.                 Response.Write("\n");  
  40.             }  
  41.   
  42.             Response.End();  
  43.         }   

In the code given above, we are using Response.ClearContent() method to clear all the content output from the buffer stream.

Two line of code given below, which is to add HTTP header to the output stream and the first parameter is the name of the HTTP header to add the value and the second parameter is for the string to add to the header.

Here, excelName is the string, which is provided by the developer. 

  1. Response.AddHeader("content-disposition""attachment;filename=" + excelName + ".xls");  
  2.  Response.AddHeader("Content-Type""application/vnd.ms-excel");   

We are using the code given below to show the columns for the Excel sheet. 

  1. Response.Write("Id");  
  2. Response.Write("\t");  
  3. Response.Write("Name");  
  4. Response.Write("\t");  
  5. Response.Write("Address");  
  6. Response.Write("\t");  
  7.   
  8. Response.Write("\n");    

In the code given above, we are passing the parameter \t for the next column and \n for the new line.

Now, we can use foreach loop to repeat the records for each row, as shown below. 

  1. foreach (var employee in employees)  
  2.             {  
  3.                 Response.Write(employee.Name);  
  4.                 Response.Write("\t");  
  5.                 Response.Write(employee.Id);  
  6.                 Response.Write("\t");  
  7.                 Response.Write(employee.Address);  
  8.                 Response.Write("\t");  
  9.                 Response.Write("\n");  
  10.             }   

In last, we have to use Response.End() method to send all currently buffered output to the client, stop the execution of the page and raise the System.Web.HttpApplication.EndRequest event.

The screenshot is given below for the Excel sheet.


In this easy way, we can export an Excel sheet in ASP.NET, using C#.

Download the zip file from the attachment for the source code of the sample Application.

Ebook Download
View all
Learn
View all