作者:乔山办公网日期:
返回目录:word文档
public void Download()
{
Random rd = new Random();
string fileName = DateTime.Now.ToString("yyyyMMddhhmm") + rd.Next() + ".doc";
//存储路径
string path = Server.MapPath(fileName);
//创建字符输出流
StreamWriter sw = new StreamWriter(path, true, System.Text.UnicodeEncoding.UTF8);
//需要导出的内容
string str = "<html><head><title>无标题文档</title></head><body>这里放从数据库导出的word文档内容</body></html>";
//写入
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();
}
标准word文档的格式636f7079e799bee5baa6361微软暂未公布,由此我们可将需要导出的内容转为标准HTML文件储存,后缀名为.doc
也可以将要导出内容转为标准XML格式存储,改后缀为.doc
具体格式随意新建个word文档,输入内容,另存为.XML可见
另外一种导出方式为word导出标准格式,服务器需要安装Microsoft Office word,需要预先设置好一个word文档并在要插入内容的地方设置书签做为模版,导出word文档时需要先遍历模版文件中的所有书签,然后给书签赋值就能实现导出数据了
还有不懂的可以直接百度HI我
是否可以解决您的问题?