C#如何将datagride中的数据导入倒excel 用C#做网站,想将客户端的datagride中的数据通过单击按钮导入倒excel,该如何实现呢,各位高手救命啊,急 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary>/// 输出到Excel/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Button1_Click(object sender, System.EventArgs e){Response.Clear(); Response.Buffer= true; Response.Charset="GB2312"; Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls"); Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文Response.ContentType = "application/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.DataGrid1.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString());Response.End();} private void HtmlToExcel(string FileType, string FileName) { Response.Clear(); Response.Charset = "utf-8"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(FileName)); Response.ContentType = FileType; this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); divtest.RenderControl(hw); Response.Write(tw.ToString()); Response.End();}//然後在按鈕裡調用此函數:HtmlToExcel("application/ms-excel", "Test.xls"); //其中"Test.xls"是EXCEL的文件名 呵呵,忘了告訴樓主,把我貼的代碼裡的“divtest”改成DataGrid名稱即可。 最简单的方法,从页面的取datagird1.innerHTML(或outerHTML) saveas成xsl的文件即可,格式数据全有,excel可以解析html 麻烦帮忙再看看 【求助】C#怎样实现流氓软件效果 问个思路,关于TreeView的,分少只给最有价值的回贴,谢谢了! 请问:怎样才很好的学会--自定义控件--和--使用好,类-- 如何控制输入的值为2位字母? 关于if(Session["qx"].ToString()=="ALL") c# web 串口通信问题 关于DevExpress的XtraReports的问题 [50分]C#调用dll文件中的函数的问题 我想写个类似花生壳的软件。 一个数组的初级问题! 救我啊,如何重绘TextBox里面的字
/// 输出到Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/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.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
{
Response.Clear();
Response.Charset = "utf-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(FileName));
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
divtest.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}//然後在按鈕裡調用此函數:
HtmlToExcel("application/ms-excel", "Test.xls"); //其中"Test.xls"是EXCEL的文件名