(jsp)怎样实现把数据库的内容写入word文档
在web-oa系统中,公文管理好象不可或缺,有时需要从数据库中查询一些数据以某种格式输出来,并以word文档的形式展现,有时许多word文档保存到数据库中的某个表的Blob字段里,服务器再把保存在Blob字段中的图片文件展现给用户。
通过网上查找发现很少有关于此类的文章,现在整理起来供大家参考。 1在client端直接生成word文档 在jsp页面上生成word文档非常简单,只需把contentType=”text/html”改为contentType="application/msword; charset=utf-8"即可,代码如下: 通过设置可以使原来页面的内容在word中表现出来。
如果需要把word文档下载下来,只需在jsp页面上面加上如下代码: 其中filename.doc中filename是要下载的word文档的文件名,可以通过来自行定制,如下 .doc"); %> 这样提供一个提示信息供用户选择如下图所示 小技巧:如果程序员需要在生成word文档的时候按照自己预先在word上设计好的格式,可以复制word格式然后粘贴到frontpage中,取html代码贴到jsp页面即可。 2 在客户端输出存在数据库中的word实体 这里只讨论在client输出oracle中BLOB字段中的word文档实体。
其中调用了类getBlobBean,该类提供了从oracle中取出blob功能,代码如下: package yourpackage; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import oracle.sql.*; import beans.yourbeanpackage. getBlobBean; /** * Title: * Description: * Copyright: Copyright (c) 2004 * Company: * @author not attributable * @version 1.0 */ public class GetBlobServlet1 extends HttpServlet { //设置输出内容类型,这个设置很重要,否则客户端浏览器不能识别输出内容,导致弹出下载的对话框。 private static final String CONTENT_TYPE = "application/msword;charset=utf-8"; //Initialize global variables public void init() throws ServletException { } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); perform(request,response); } public void perform(HttpServletRequest request, HttpServletResponse response){ try{ //该类功能是从oracle哭中取出blob实体 getBlobBean getBlob=new getBlobBean(); OutputStream sos = response.getOutputStream(); getBlob.connFunction(); oracle.sql.BLOB blob=getBlob.getBlob("cehui"); //输出word文档 if(blob!=null){ InputStream pi = blob.getBinaryStream(); int blobsize =(int)blob.length(); byte[] blobbytes = new byte[blobsize]; int bytesRead = 0; while ((bytesRead = pi.read(blobbytes)) != -1) { sos.write(blobbytes, 0, bytesRead); } pi.close(); sos.flush(); sos.close(); } getBlob.dropConnFunction(); }catch(Exception e){ System.out.println(e.toString()); } } //Clean up resources public void destroy() { } } 3在client端直接生成EXCEL文档 生成 生成例子excel 。
用WORD排版面,倒出HTML 把代码拷贝到网页里,然后这个JSP页面打印你就随心所欲的控制了。 4在client端直接生成PDF文档 需要下载JAR包:以下代码在JDK1.4 RESIN2.16 下测试通过 ITEXT包 http://mesh.dl.sourceforge.net/sourceforge/itext/itext-1.3.5.jar 字体包 http://itext.sourceforge.net/downloads/iTextAsian.jar JSP生成到客户IE端直接打开 ie_PDF.jsp ------------------------------- 在服务器端生成不下载。
server_PDF.jsp 使用iText可以设置文字的字体,对于我们中国的程序员来说如何显示中文是最紧要的问题。幸好iText中有一个专门的包用来设置亚洲国家的字体你可以从 http://itext.sourceforge.net/downloads/iTextAsian.jar 下载这个包。
然后把它直接放到你的ClassPath中就可以了。如何设置字体呢? BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font FontChinese = new Font(bfChinese, 12, Font.NORMAL); 在上面的代码中设置了中文字体的显示,你只要使用下面的代码就可以包中文加到PDF中了 String title = "我爱喝咖啡"; Paragraph t = new Paragraph(title, FontChinese); doc.add(t); 原文链接地址: http://www.360doc.com/content/07/0824/18/25392_692506.shtml。
怎样把文本文档里存储的数据存到数据库
将带有格式的文本保存到数据库中的方法/步骤:
1、在jsp中,页面的带有格式的文本内容外面用一个大的标签,给定表签名。
2、页面做提交的时候用上面的表签名点innerHTML的方式来获取页面带有标签和样式的内容。
3、将上面取得的内容作为一个字符串保存到数据库即可,下次把数据库里的内容直接输出到页面就可以了。
对于要输出到word里保存样式的方法也是类似的,只是需要去看一下word解析文本的方式与jsp有何区别,在输出到word的时候做一下变换即可。
如何将word整个文档存到oracle数据库中
字段类型用blob,先把blob设成空然后再操作,我用的是upload_5xsoft.inc(asp人都知道),显示的时候就用数据流写出即可。代码如下
写入:
sql = "update d_doc_modi_rec set doc_content=empty_blob() where doc_no='"&doc_no&"' and version='"&maxver&"'"
Oradatabase.dbEXECUTESQL(sql)
Selsql ="select * from d_doc_modi_rec where doc_no='"&doc_no&"' and version='"&maxver&"'"
file.SavetoDB Selsql,feild
Public function SavetoDB(Selstr,area)
dim rsDB,ErrorChar,dr
if selstr="" or area="" then exit function
set rsDB=Oradatabase.dbcreatedynaset(Selstr,0)
set dr=CreateObject("Adodb.Stream")
dr.Mode=3
dr.Type=1
dr.Open
dr.position=0
'dr.Type = 2
'dr.Charset = "gb2312"
path = filepath&filename
dr.LoadFromFile path
chunk=dr.read
chunksize=lenb(chunk)
'response.end
set infoblob=rsDB.fields(area).value
rsDB.Edit
infoblob.offset=1
amount_written=infoblob.write (chunk,chunksize)
rsDB.update
dr.close
set amount_written = nothing
rsDB.close
end function
如何利用C#从WORD中读取内容并存入数据库
前提: 导入COM库:Microsoft word 11。
0 Object Library。 引用里面就增加了: 打开文档: object oMissing = System。
Reflection。Missing。
Value; Word。_Application oWord; Word。
_Document oDoc; oWord = new Word。Application(); oWord。
Visible = true; object fileName = @"E: c"; oDoc = oWord。Documents。
Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 导入模板 object oMissing = System。 Reflection。
Missing。Value; Word。
_Application oWord; Word。_Document oDoc; oWord = new Word。
Application(); oWord。Visible = true; object fileName = @"E: c"; oDoc = oWord。
Documents。Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);。
怎么在JSP页面直接读取SQL Server2000的image字段,这个字段是存
你可以把字段的路径作为参数放在数据库的某一个字段中,需要用时提取出来即可.
例如:
"image/pic1.jpg" 这是一个相对路径,你把这个字符串存入数据库后,需要用时只需从数据库提取出来就行,
<img src="
<%
String str=select * from 表名 where 条件;
ResultSet rs = null;
Statement stmt = conn.createStatement();
rs=stmt.executeQuery(str);
str=rs.getString("字段名");
out.print(str);
%>">
找个高手吧 不好意思朋友
转载请注明出处51数据库 » jspword文档存入数据库