Hello,
I'm trying to export Gridview to csv. the output is fine except in case if any coloumn value in a big string, it breaks into 2lines as below.
This is my Export to csv code.
protected void ExportToCSV(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=CashOutReport.csv");
Response.Charset = "";
Response.ContentType = "application/text";
grvCashOutReport.AllowPaging = false;
grvCashOutReport.Columns[0].HeaderText = "S.No";
grvCashOutReport.Columns[1].HeaderText = "";
grvCashOutReport.Columns[1].Visible = false;
grvCashOutReport.Columns[9].HeaderText = "";
grvCashOutReport.Columns[9].Visible = false;
grvCashOutReport.Columns[10].HeaderText = "";
grvCashOutReport.Columns[10].Visible = false;
StringBuilder Sb = new StringBuilder();
for (int k = 0; k < grvCashOutReport.Columns.Count; k++)
{
if (!string.IsNullOrEmpty(grvCashOutReport.Columns[k].HeaderText))
Sb.Append(grvCashOutReport.Columns[k].HeaderText + ',');
}
Sb.Append("\r\n");
for (int i = 0; i < grvCashOutReport.Rows.Count; i++)
{
if ((grvCashOutReport.Rows[i].FindControl("lblRowNumber") as Label) != null)
{
Sb.Append((grvCashOutReport.Rows[i].FindControl("lblRowNumber") as Label).Text + ',');
}
for (int k = 0; k < grvCashOutReport.Columns.Count; k++)
{
if (!string.IsNullOrEmpty(grvCashOutReport.Rows[i].Cells[k].Text))
{
Sb.Append(grvCashOutReport.Rows[i].Cells[k].Text + ',');
}
}
Sb.Append("\r\n");
}
Response.Output.Write(Sb.ToString());
Response.Flush();
Response.End();
}