java 生成word


public class RTFCreate {

public static void main(String[] args) throws MalformedURLException,
IOException {
RTFCreate rtfCreate = new RTFCreate();
try { String bodyText="abc";
//byte[] b= null;
//rtfCreate.createRTF(bodyText,"WebRoot/images/yjg.png");
// rtfCreate.createRTF(bodyText,b);
// } catch (FileNotFoundException e) {
/
// e.printStackTrace();
// } catch (DocumentException e) {
//
// e.printStackTrace();
// }
// }

public String createRTF(String bodyText, byte[] image) throws DocumentException, BadElementException,
MalformedURLException, IOException {

String filePath = "c:/testW.rtf";
Document document = new Document(PageSize.A4);
// document.addTitle("Title");
// document.addHeader("header","Header");

//生成html格式文件
// HtmlWriter.getInstance(document, new FileOutputStream("C:/testW.html"));

//生成rtf格式文件doc文件
RtfWriter2.getInstance(document, new FileOutputStream("C:/testW.rtf"));

//生成pdf格式文件
// PdfWriter.getInstance(document, new FileOutputStream("C:/testW.pdf"));


生成xml格式文件

// XmlWriter.getInstance(document, new FileOutputStream("C:/testW.xml"));
document.open();
// add a word
document.add(new Paragraph(bodyText));
// add a table
Table table = new Table(3);
table.setBorderWidth(1);
table.setBorderColor(new Color(0, 0, 255));
table.setPadding(5);
table.setSpacing(5);
Cell cell = new Cell("header");
cell.setHeader(true);
cell.setColspan(3);
table.addCell(cell);
table.endHeaders();
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.setRowspan(2);
cell.setBorderColor(new Color(255, 0, 0));
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("cell test1");
cell = new Cell("big cell");
cell.setRowspan(2);
cell.setColspan(2);
table.addCell(cell);
table.addCell("cell test2");
document.add(table);

//这里可以是图片路径,也可以把图片读为是byte[] 类型传进来,我这里用的是byte 因为我的图片从数据表取出
//Image png = Image.getInstance(imageUrl);
Image img = Image.getInstance(image);
document.add(img);
document.close();

//返回生成word文档的路径,为了在Action 中读出此word文件
return filePath;

}

Action 方法中读取word文件,可以读取图片,部分代码

String bodyText = null;
if (clsb != null) {
bodyText = clsb.getBodyText();
}

if(bodyText !=null){
RTFCreate rtfCreate = new RTFCreate();
try{
//TODO word letter form
CamDoc cdoc = new CamDoc();
cdoc = camDocControl.findByRecordID(3);
byte[] cimg = cdoc.getDocObject();
request.setAttribute("cimg", cimg);
String body = TestHTMLParser.testHtml(bodyText);//使用htmlparser来解析html中的文本

String filePath = rtfCreate.createRTF(body, cimg);
FileInputStream fis = new FileInputStream(filePath);
// 1 k = 1024 byte , 1 m = 1024 k = 1048576 byte
// 10000000 byte = 9.5 m
byte[] bytes = new byte[10000000];
while (fis.read(bytes) != -1) // 将文件读入byte数组
{

}

response.reset();
response.setContentType("application/rtf;charset=UTF-8");
response.setContentLength(bytes.length);
response.setHeader("Content-disposition",
"attachment; filename=bankAuthForm.doc");

ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();

//这段是提取word 文件中的文本
// String st = null;
// DefaultStyledDocument styledDoc = new DefaultStyledDocument();
// InputStream is = new FileInputStream(new File("c:/testW.rtf"));
// new RTFEditorKit().read(is, styledDoc, 0);
// st = styledDoc.getText(0, styledDoc.getLength());
// System.out.println("---rtf content>>>"+st);
// String(styledDoc.getText(0, styledDoc.getLength()).getBytes("UTF-8"));
}catch(Exception e){
e.printStackTrace();
}
}
return null;

}



jsp 直接頁面直接生成word 文檔來下載保存,這個基本可以滿足了。

Action :中的方法

public ActionForward downloadWord(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
return mapping.findForward("printWord");}

轉到這個生成word 文檔的頁面。

在此JSP 頁面中加上:<%@ page contentType="application/msword; charset=UTF-8" %>
<%
response.setHeader("Content-Disposition", "attachment;filename=application.doc");
%>這兩句后即可以下載了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值