最近遇到一个题目就是如何在asp.net中将数据导出到word中,由于数据是动态的,所以需要在后台拼出想要的的格式,翻遍了网页找出了一个比较满意的代码,感谢那位高手。代码如下:
";
代码如下 | 复制代码 | ||||
publicvoidDownload() { Random rd =newRandom(); stringfileName = DateTime.Now.ToString("yyyyMMddhhmm") + rd.Next() +".doc"; //存储路径 stringpath = Server.MapPath(fileName); //创建字符输出流 StreamWriter sw =newStreamWriter(path,true, System.Text.UnicodeEncoding.UTF8); //需要导出的内容 // string str = " stringstr =""; str +=" str +=" 阅读报表 "; str +=" str +=" ";
str +=" ";
str +=" ";
str +=" ";
str +=""; //写入 sw.Write(str); sw.Close(); Response.Clear(); Response.Buffer =true; this.EnableViewState =false; Response.Charset ="utf-8"; Response.AppendHeader("Content-Disposition","attachment;filename="+ HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.ContentType ="application/octet-stream"; Response.WriteFile(path); Response.Flush(); Response.Close(); Response.End(); }
相关文章精彩推荐 |