日期:2014-5-20 浏览次数:20226次

小妹刚学c#做应用程序,现在遇到一个问题,是把datagrid显示的数据导入到excel,该如何做,急
小妹刚学c#做应用程序,现在遇到一个问题,是把datagrid显示的数据导入到excel,该如何做,急

------解决方案--------------------
Response.ContentType = "application/vnd.ms-excel ";
Response.Charset = " ";
this.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
dgShow.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
------解决方案--------------------
提供一个最简单给你
Response.Clear();
Response.Buffer= true;
Response.Charset= "GB2312 ";
Response.AppendHeader( "Content-Disposition ", "attachment;filename= "+文件名+ ".xls ",System.Text.Encoding.UTF8));
//attachment --- 作为附件下载
//inline --- 在线打开
//filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)
//进行进行编码,以解决文件名乱码的问题
Response.ContentEncoding=System.Text.Encoding.GetEncoding( "GB2312 ");//设置输出流为简体中文
Response.ContentType = "application/ms-excel ";//设置输出文件类型为excel文件。
//Response.ContentType是输出流的 HTTP MIME 类型
//Response.ContentType = "Response.ContentType ";
//Response.ContentType --- word文件
//application/vnd.ms-excel --- excel文件
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo( "ZH-CN ",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.Your_DataGrid.RenderControl(oHtmlTextWriter);
//Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
Response.Write(oStringWriter.ToString());
Response.End();
------解决方案--------------------
是web还是winform???
------解决方案--------------------
给你一个.
System.IO.StringWriter SW = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter HTW=new System.Web.UI.HtmlTextWriter(SW);.
DataGrid1.RenderControl(HTW);
//Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
Response.Buffer=true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "vnd.ms-excel ";
//Response.ContentType是输出流的 HTTP MIME 类型
//Response.ContentType --- word文件
//application/vnd.ms-excel --- excel文件
//...
Response.Charset= "utf-8 ";
Response.ContentEncoding=System.Text.Encoding.GetEncoding( "utf-8 ");
Response.AddHeader( "Content-Disposition ", "attachment;filename=XXX.doc ");
//attachment --- 作为附件下载
//inline --- 在线打开
//filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)
//进行进行编码,以解决文件名乱码的问题
Response.Write(SW.ToString());
Response.Flush();
Response.Close();
------解决方案--------------------
Response.Clear();
Response.Buffer= true;
Response.Charset= "gb2312 ";

Response.AppendHeader( "Content-Disposition ", "attachment;filename=Sheet1.xls ");
Response.ContentEncoding=System.Text.Encoding.GetEncoding( "gb2312 ");
Response.ContentType = "application/ms-excel ";
this.EnableViewState = false;

System.IO.StringWriter oStringWriter = new System.IO.StringWriter();