ASP.NET导出word实例
最近遇到一个题目就是如何在asp.net中将数据导出到word中,由于数据是动态的,所以需要在后台拼出想要的的格式,翻遍了网页找出了一个比较满意的代码,感谢那位高手。代码如下:
publicvoidDownload()
{
Randomrd=newRandom();
stringfileName=DateTime.Now.ToString("yyyyMMddhhmm")+rd.Next()+".doc";
//存储路径
stringpath=Server.MapPath(fileName);
//创建字符输出流
StreamWritersw=newStreamWriter(path,true,System.Text.UnicodeEncoding.UTF8);
//需要导出的内容
//stringstr="无标题文档 这里放从数据库导出的word文档内容";
stringstr="";
str+="无标题文档 ";
str+="阅读报表";
str+="";
str+="20000 ";
str+="10000 ";
str+="30000 ";
str+="30000 ";
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();
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短
-
返回顶部
-
3162201930
-
czq8825@qq.com