导出为CSV时更改工作表名称 [英] Changing sheet name while exporting to CSV

查看:785
本文介绍了导出为CSV时更改工作表名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正面临着一个需要更改CSV文件工作表名称的情况。我使用下面的代码将我的网格导出为CSV。

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.

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!!!

推荐答案

使用 .csv 无法执行此操作。使用逗号分隔值,您无法控制数据显示。您可以尝试使用 Interop OpenXML Excel 中执行此操作>。在 Google Bing 中搜索此内容。你会发现它。
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.


这篇关于导出为CSV时更改工作表名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆