asp中如何将数据库的内容导出到word?

asp中如何将数据库的内容导出到word?,第1张

写一个js,我回去找一下

function AllAreaWord()

{

var oWD = new ActiveXObject("Word.Application")

var oDC = oWD.Documents.Add("",0,1)

var oRange =oDC.Range(0,1)

var sel = document.body.createTextRange()

sel.moveToElementText(PrintA)

sel.select()

sel.execCommand("Copy")

oRange.Paste()

oWD.Application.Visible = true

//window.close()

}

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", "attachmentfilename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8))

Response.ContentType = "application/octet-stream"

Response.WriteFile(path)

Response.Flush()

Response.Close()

Response.End()

}

标准word文档的格式微软暂未公布,由此我们可将需要导出的内容转为标准HTML文件储存,后缀名为.doc

也可以将要导出内容转为标准XML格式存储,改后缀为.doc

具体格式随意新建个word文档,输入内容,另存为.XML可见

另外一种导出方式为word导出标准格式,服务器需要安装Microsoft Office word,需要预先设置好一个word文档并在要插入内容的地方设置书签做为模版,导出word文档时需要先遍历模版文件中的所有书签,然后给书签赋值就能实现导出数据了

还有不懂的可以直接百度HI我

asp.net

,web页面中的查询数据导出Word,实现思路

一:需要把自己查询后的结构组装成一张数据集,如DataTable或DataSet..

二:定义需要导出的字段与数据库所创建列对应

三:确定导出那张表,传如:表名

希望对你有用!


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6692891.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-26
下一篇2023-03-26

发表评论

登录后才能评论

评论列表(0条)

    保存