asp.net中怎么把gridview中的数据写入一个文件,再保存到桌面 还有文件中的数据怎么上传到gridview中
3条回答 默认 最新
- devmiao 2016-05-16 15:29关注
using System; using System.Web; using System.Web.UI; using System.IO; using System.Web.UI.WebControls; namespace DotNet.Utilities { public class ExportExcel { protected void ExportData(string strContent, string FileName) { FileName = FileName + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString(); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Charset = "gb2312"; HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; //this.Page.EnableViewState = false; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls"); // 把文件流发送到客户端 HttpContext.Current.Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">"); HttpContext.Current.Response.Write(strContent); HttpContext.Current.Response.Write("</body></html>"); // 停止页面的执行 //Response.End(); } /// <summary> /// 导出Excel /// </summary> /// <param name="obj"></param> public void ExportData(GridView obj) { try { string style = ""; if (obj.Rows.Count > 0) { style = @"<style> .text { mso-number-format:\@; } </script> "; } else { style = "no data."; } HttpContext.Current.Response.ClearContent(); DateTime dt = DateTime.Now; string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString(); HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=ExportData" + filename + ".xls"); HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); obj.RenderControl(htw); HttpContext.Current.Response.Write(style); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); } catch { } } } }
解决 无用评论 打赏 举报
悬赏问题
- ¥35 平滑拟合曲线该如何生成
- ¥100 c语言,请帮蒟蒻写一个题的范例作参考
- ¥15 名为“Product”的列已属于此 DataTable
- ¥15 安卓adb backup备份应用数据失败
- ¥15 eclipse运行项目时遇到的问题
- ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
- ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
- ¥15 自己瞎改改,结果现在又运行不了了
- ¥15 链式存储应该如何解决
- ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站