Skip to main content

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.

<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();  
        }

 

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.

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

 

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

Response.Write("Id");  
Response.Write("\t");  
Response.Write("Name");  
Response.Write("\t");  
Response.Write("Address");  
Response.Write("\t");  
  
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.

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");  
}

 

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.

Leave a Reply