Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am facing a situation here where I need to change the worksheet name of CSV file. I use below code to export my grid to CSV.
C#
StringBuilder sb = new StringBuilder();
string f_name = "GridViewExport.csv";

if (gv.Rows[i].Cells[k].Text != "" && gv.Columns[k].HeaderText == "Question")
{
    string question = gv.Rows[i].Cells[k].Text.Replace(" ", "");

    string newquestion = System.Text.RegularExpressions.Regex.Replace(question, @"\s+", " ");

    sb.Append(newquestion + ',');
}

if (gv.Rows[i].Cells[k].Text != "" && gv.Columns[k].HeaderText == "Answer")
{
    int sequence_number = Convert.ToInt32(gv.Rows[i].Cells[7].Text);
    int item_number = Convert.ToInt32(gv.Rows[i].Cells[k].Text);

    DataSet ds_interview_content = _tpsInterviewdetailManager._GetInterviewDetailBySequenceNumber(sequence_number, item_number);

    if (ds_interview_content.Tables[0].Rows.Count > 0)
    {
        string answ = ds_interview_content.Tables[0].Rows[0]["item_content"].ToString();
        sb.Append(answ + ',');
    }
}

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + f_name+".csv");
Response.Charset = "";
Response.ContentType = "application/text";

//Response.Output.Write(sb.ToString());
Response.Write(sb1.ToString());
Response.Flush();
Response.End();

Actually my worksheet name is different than filename. If I give this code, then filename will be assigned to worksheet, but I don't want that.
Thanks in advance!!!
Posted
v2
Comments
Sinisa Hajnal 11-Mar-15 3:45am    
There is nothing you can do. CSV is not actually excel file and it will be imported and shown as excel wants it. What you need is Microsoft.Interop.Excel and export into the excel file.

1 solution

There is no way to do this with .csv. With comma separated values, you don't have much control with data presentations. You could try to do this in Excel using Interop or OpenXML. Search for this in Google or Bing. You will find it.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900