Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8781141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:13:29+00:00 2026-06-13T20:13:29+00:00

I am using the code below on an aspx page on button click event

  • 0

I am using the code below on an aspx page on button click event to generate csv file. This works when I do not name my file but when I try to use:
Response.AddHeader(“Content-Disposition”, “attachment;filename=myfilename.csv”);

to name the file as myfilename.csv, the excel sheet generated is the screen shot of the web page instead of having text in it. Can someone help me with this problem.
Thanks!

DataGrid dg = new DataGrid();
dg.DataSource = GetData();
htmlTextWriter.WriteLine("<b>Details</b>");

//Get the html for the control
dg.HeaderStyle.Font.Bold = true;
dg.HeaderStyle.BackColor = System.Drawing.Color.Gray;
dg.DataBind();
dg.RenderControl(htmlTextWriter);

//Write the HTML back to the browser.
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
//Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv");
this.EnableViewState = false;
Response.Write(textWriter.ToString());
Response.End();

private System.Data.DataTable GetData()
{
    System.Data.DataTable dt = new System.Data.DataTable("TestTable");
    dt.Columns.Add("SSN");
    dt.Columns.Add("Employee ID");
    dt.Columns.Add("Member Last Name");
    dt.Columns.Add("Member First Name");
    dt.Columns.Add("Patient Last Name");
    dt.Columns.Add("Patient First Name");
    dt.Columns.Add("Claim No.");
    dt.Columns.Add("Service Line No.");
    dt.Columns.Add("Error Code");
    dt.Columns.Add("Error Message");                
    dt.Rows.Add(123456789,4455,"asdf","asdf","sdfg","xzcv","dsfgdfg123",1234,135004,"some error");            
    dt.Rows.Add(123456788,3344,"rth","ojoij","poip","wer","aadf124",1233,135005,"Some Error");
    dt.Rows.Add(123456787,2233,"dfg","sdfg","vcxb","cxvb","UHCAL125",1223,135006,"another error");
    return dt;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:13:30+00:00Added an answer on June 13, 2026 at 8:13 pm

    I am not exactly sure what you are aiming for here, so I have assumed that you are wanting to create a CSV file in a button click event and send that back to the user. What you currently have appears to write the HTML of the control into an XLS file.

    Try this:

    protected void Button1_Click(object sender, EventArgs e)
    {
        var dataTable = GetData();
        StringBuilder builder = new StringBuilder();
        List<string> columnNames = new List<string>();
        List<string> rows = new List<string>();
    
        foreach (DataColumn column in dataTable.Columns)
        {
            columnNames.Add(column.ColumnName); 
        }
    
        builder.Append(string.Join(",", columnNames.ToArray())).Append("\n");
    
        foreach (DataRow row in dataTable.Rows)
        {
            List<string> currentRow = new List<string>();
    
            foreach (DataColumn column in dataTable.Columns)
            {
                object item = row[column];
    
                currentRow.Add(item.ToString());
            }
    
            rows.Add(string.Join(",", currentRow.ToArray()));
        }
    
        builder.Append(string.Join("\n", rows.ToArray()));
    
        Response.Clear();
        Response.ContentType = "text/csv";
        Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv");
        Response.Write(builder.ToString());
        Response.End();
    }
    

    When I run this I am prompted by the browser to save the CSV file.

    Edit:

    If you would like to maintain your current approach (which is producing HTML, not CSV) then try this:

    Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.xls");
    

    Note that I have simply changed the file extension from CSV to XLS. When using the CSV extension, the text appeared in Excel as HTML. Using XLS, it appears just as it does when the above line is commented out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi Below is the code i m using for login page.but it does not
I have a JavaScript function for a HTML button click event in aspx page
I'm using the code below for dragging an image ( #StndSoln1 ). It works
I'm trying to create a mobile friendly web page using the code below. When
I am using below code to call webservice using javascript: <head> <title>Untitled Page</title> <script
I am using AJAX.NET's update panels in the below manner. My code works perfect
I have some code like below in an aspx page: function CheckEditing() { var
I can insert a row by using code below. USE pmdb; INSERT INTO md5_tbl
Using the code below I can insert a new row to my table (
Using the code below, I am attempting to fill a Canvas with UIElements and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.