在客户端生成word文件

在web-oa系统中,公文管理好象不可或缺,有时需要从数据库中查询一些数据以某种格式输出来,并以word文档的形式展现,有时许多word文档保存到数据库中的某个表的Blob字段里,服务器再把保存在Blob字段中的图片文件展现给用户。通过网上查找发现很少有关于此类的文章,现在整理起来供大家参考。

 

在web-oa系统中,公文管理好象不可或缺,有时需要从数据库中查询一些数据以某种格式输出来,并以word文档的形式展现,有时许多word文档保存到数据库中的某个表的Blob字段里,服务器再把保存在Blob字段中的图片文件展现给用户。通过网上查找发现很少有关于此类的文章,现在整理起来供大家参考。

1 在client端直接生成word文档
在jsp页面上生成word文档非常简单,只需把contentType=”text/html”改为contentType=”application/msword; charset=gb2312″即可,代码如下:

通过设置可以使原来页面的内容在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=gb2312″;

//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() {

}

}

—–

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Recent Comments