Gridview到Excel导出 [英] Gridview to Excel export
问题描述
HI,
我需要将Gridview内容导出到Excel工作表。我尝试搜索并获得了许多解决方案。其中我使用了最简单的一个。但问题是它是导出整个HTML页面而不是选择GridView。这是我的代码:
I need to export Gridview contents to an excel sheet. I tried searching and got many solutions. Among them i used the most simple one. But the problem is that it is exporting the entire HTML page instead of selected GridView. Here''s my code:
protected void exportToExcel_Click(object sender, EventArgs e)
{
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
try
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=MyFiles.xls");
Response.Charset = "";
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
catch (Exception except1)
{
Response.Write(except1);
}
}
请建议我如何进行出口。我是C#环境的新手。
Please suggest me how to do the exporting stuff. I''m new to C# environment.
推荐答案
您好,
如果您填写网格视图通过从数据库中获取数据然后跟随对您有用...
protected void btnExport_Click(object sender,EventArgs e)
{
试试
{
string strconstring = ConfigurationManager.AppSettings [con]; //来自web.config文件的数据库连接字符串
SqlConnection cn = new SqlConnection(strconstring); //连接到数据库
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
string strSQL =;
cn.Open();
strSQL =select * from table1; //你的选择查询在gridview中显示数据
Hi,
If your filling your grid view by fetching data from database then following will be useful to you...
protected void btnExport_Click(object sender, EventArgs e)
{
try
{
string strconstring = ConfigurationManager.AppSettings["con"]; //database connection string from web.config file
SqlConnection cn = new SqlConnection(strconstring); // connection to DB
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
string strSQL = "";
cn.Open();
strSQL = "select * from table1 "; //your select query which display data in gridview
<pre lang="text">
}
da.SelectCommand = new SqlCommand (strSQL,cn);
da.Fill(dt);
// DataTable dt = new DataTable();
// dt =(DataTable)Session [MonthlyCostingData];
Response.Clear(); < br $>
string attachment =attachment; filename = AfterSalesPerformance_+ DateTime.Now.Date.ToString()+。xls; < br $>
Response.ClearContent();
Response.AddHeader(content-disposition,附件);
Response.ContentType =application / vnd.ms-excel;
string tab = ;
foreach(dt.Columns中的DataColumn dc)
{
响应。写(tab + dc.ColumnName);
tab =\t;
}
Response.Write(\ n);
int i;
foreach(DataRow dr in dt.Rows)
{
tab =;
for(i = 0;我< dt.Columns.Count; i ++)
{
//dr [i] .Style.Add (\"background-color,#FFFFC0);
Response.Write(tab + dr [i] .ToString());
tab =\t;
}
Response.Write(\ n);
}
Response.End();
}
#endregion
catch(System.Threading.ThreadAbortException lException)
{
//什么都不做
}
catch(例外情况)
{
Response.Write(ex.Message);
}
}
}
da.SelectCommand = new SqlCommand(strSQL, cn);
da.Fill(dt);
//DataTable dt = new DataTable();
//dt = (DataTable)Session["MonthlyCostingData"];
Response.Clear();
string attachment = "attachment; filename=AfterSalesPerformance_" + DateTime.Now.Date.ToString() + ".xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
foreach (DataColumn dc in dt.Columns)
{
Response.Write(tab + dc.ColumnName);
tab = "\t";
}
Response.Write("\n");
int i;
foreach (DataRow dr in dt.Rows)
{
tab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
//dr[i].Style.Add("background-color", "#FFFFC0");
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();
}
#endregion
catch (System.Threading.ThreadAbortException lException)
{
// do nothing
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
如果你想从Directly gridview导出数据,那么你可以尝试这段代码。
它将导出Excel中的GridView数据,无论sourcec如何数据库的数据。
string filename =EmployeeRecord.xls;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
//获取控件的HTML。
dgGrid.RenderControl(hw);
//编写HTML回到浏览器。
//Response.ContentType = application / vnd.ms-excel;
Response.ContentType =application / vnd.ms-excel;
Response.AppendHeader(Content-Disposition,附件; filename =+ filename +);
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
希望它会对你有帮助。
If you want to export data from Directly gridview then you can try this code.
It will export the GridView Data in Excel regardless of source of database.
string filename = "EmployeeRecord.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
Hopefully It will help you.
对不起,我对这种可能性没有任何想法..
Sorry I don''t have any idea about any kind of such possibilities..
这篇关于Gridview到Excel导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!