0

この方法を使用して、vb.netを使用してcsvファイルを作成しています。

 protected void btnExportCSV_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
    Response.Charset = "";
    Response.ContentType = "application/text";

    GridView1.AllowPaging = false;
    GridView1.DataBind();

    StringBuilder sb = new StringBuilder();
    for (int k = 0; k < GridView1.Columns.Count; k++)
    {
        //add separator
        sb.Append(GridView1.Columns[k].HeaderText + ',');
    }
    //append new line
    sb.Append("\r\n");
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        for (int k = 0; k < GridView1.Columns.Count; k++)
        {
            //add separator
            sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
        }
        //append new line
        sb.Append("\r\n");
    }
    Response.Output.Write(sb.ToString());
    Response.Flush();
    Response.End();
}

このコードはcsvファイルを作成しています。しかし、csvに余分な列は必要ありません。出来ますか?

4

1 に答える 1

0

はい、もちろん!

  ...
  for (int k = 0; k < GridView1.Columns.Count - 1; k++)
  {
    //add separator
     sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
  }
  //add final element
  sb.Append(GridView1.Rows[i].Cells[GridView1.Columns.Count - 1].Text );
  //append new line
  sb.Append("\r\n");
  ...
于 2012-05-17T05:56:58.070 に答える