现在有一个gridview,要把里面的数据导出成一个excel文件.网上的例子: HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();导出的内容带了html代码....现在不知道要怎么搞了..
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();导出的内容带了html代码....现在不知道要怎么搞了..
解决方案 »
- 关于WCF netTcp 服务引用的问题,新手请教
- compilation 的 assemblies
- 求教 如何取出存入数据库的word文档的索引,你方便查询?
- 求php的pack("H*","d033e22ae348aeb5660fc2140aec35850c4da997") 转成C#代码?
- datagrid中求值计算
- C#操作XML
- 上了服务器,就报以下错误,\
- 我想使用字符串来索引数组例如:baddd["S"],但我写的代码不成功,请高手们指点
- 如何将16进制数转换成int
- 有个循环附值的问题???答对还给20分!!!
- 新手提问:菜单单击事件中如何获取菜单的位置索引?
- 怎样获得griview的主键?
//导出Excel报表
public void GridViewExporter(DataSet ds)
{
System.Data.DataTable dt = ds.Tables[0];
StringWriter sw = new StringWriter(); string colList = "";
for (int k = 0; k < dt.Columns.Count; k++)
{
if (dt.Columns.Count - 1 != k)
colList += dt.Columns[k] + "\t";
else
colList += dt.Columns[k];
} sw.WriteLine(colList);
foreach (DataRow dr in dt.Rows)
{
string tmplist = "";
for (int i = 1; i < dt.Columns.Count; i++)
{
if (dt.Columns.Count - 1 != i)
{
tmplist += dr[i] + "\t";
}
else
{
tmplist += dr[i];
}
}
sw.WriteLine(tmplist);
} sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=GridView.xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End(); }
{
Export("application/ms-excel", "自命名文件名.xls");
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hell = new HtmlTextWriter(tw);
dg.AllowPaging = false;
dg.RenderControl(hell);
Response.Write(tw.ToString());
Response.End();
dg.AllowPaging = true;
void BindGrid()
{
dt=(System.Data.DataTable)Session[columninf];
DataItem=dt.DefaultView;
DataGrid1.DataSource = DataItem;
DataGrid1.DataBind();
GridControlAddEvent();}
这是我用的导出