我能够将GridView导出到文本文档,但是当我打开文本文档时,GridView中的中间列数据丢失了,我相信这是因为它是一个TemplateField,而其他列是普通的BoundFields。我不确定如何在保留TemplateField的同时解决此问题,因为我需要保持这种状态。
这是导出方法背后的我的C#代码
protected void ExportTextFile(object sender, EventArgs e)
{
//the text file
string sFileName = "Credit File-" + System.DateTime.Now.Date + ".txt";
sFileName = sFileName.Replace("/", "");
//send output to the client maching using response object
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=" + sFileName);
Response.ContentType = "application/text";
EnableViewState = false;
StringBuilder objSB = new StringBuilder();
int iCol = 0; //counter for columns
int iRow = 0; //counter for rows
for (iCol = 0; iCol < GridView4.Columns.Count; iCol++)
{
objSB.Append(GridView4.Columns[iCol].HeaderText + ',');
}
objSB.Append(Environment.NewLine);
for (iRow = 0; iRow < GridView4.Rows.Count; iRow++)
{
for (iCol = 0; iCol < GridView4.Columns.Count; iCol++)
{
objSB.Append(GridView4.Rows[iRow].Cells[iCol].Text + ',');
}
objSB.Append(Environment.NewLine);
}
Response.Write(objSB.ToString());
Response.End();
}