如何将gridview导出到csv [英] How to export gridview into csv
本文介绍了如何将gridview导出到csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hai,
我想使用c#.net web application将gridview值导出到csv。如何导出这个。
Hai,
I want to export gridview values into csv by using c# .net web application.How can I export this.
推荐答案
试试这个。它应该工作
Try this. It should work
protected void Button1_Click(object sender, EventArgs e)
{
string strValue = string.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int j = 0; j < GridView1.Rows[i].Cells.Count; j++)
{
if (!string.IsNullOrEmpty(GridView1.Rows[i].Cells[j].Text.ToString()))
{
if (j > 0)
strValue = strValue + "," + GridView1.Rows[i].Cells[j].Text.ToString();
else
{
if (string.IsNullOrEmpty(strValue))
strValue = GridView1.Rows[i].Cells[j].Text.ToString();
else
strValue = strValue + Environment.NewLine + GridView1.Rows[i].Cells[j].Text.ToString();
}
}
}
// strValue = strValue + Environment.NewLine;
}
string strFile = @"C:\YourCSVFile.csv";
if (File.Exists(strFile) && !string.IsNullOrEmpty(strValue))
{
File.WriteAllText(strFile, strValue);
}
}
以这个 [ ^ ]。
大家好,可以做到用另一种方式。
Hi All , It can be done in the another way.
protected void ibtnExporttoCSV_Click(object sender, ImageClickEventArgs e)
{
Response.ContentType = "application/csv";
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.csv");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
string strValue = string.Empty;
grdDisplay.AllowPaging = false;
grdDisplay.DataBind();
for (int i = 0; i < grdDisplay.Rows.Count; i++)
{
for (int j = 0; j < grdDisplay.Rows[i].Cells.Count; j++)
{
if (!string.IsNullOrEmpty(grdDisplay.Rows[i].Cells[j].Text.ToString()))
{
if (j > 0)
strValue = strValue + "," + grdDisplay.Rows[i].Cells[j].Text.ToString();
else
{
if (string.IsNullOrEmpty(strValue))
strValue = grdDisplay.Rows[i].Cells[j].Text.ToString();
else
strValue = strValue + "\n" + grdDisplay.Rows[i].Cells[j].Text.ToString();
}
}
}
}
Response.Write(strValue.ToString());
Response.End();
}
这篇关于如何将gridview导出到csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文