献上全部可用分,求java操作word文档模板的解决方案!

如一宝宝 2009-02-05 03:57:01
需求是这样的
公司的销售人员与客户有一个订单合同,word文档.
这个文档中的客户信息,产品信息当然都是数据库里面动态写入的,并且这个文档是基于公司标准文件的.
有规定的页眉页脚,合同打印出来后一般是2张A4的纸,如果特殊情况,合同中的产品很多,会有3张纸的可能.

网上找了很例子,
准备用网页的形式把合同模板写好,然后用Velocity填充动态的数据的,直接在网页上面点击打印,但是打印出来的没有页眉
页脚,Boss不爽,并且合同一般是2张纸以上.如果人为的写上页眉页脚,还要判断网页内容的长度
还有apache的POI,对excel的操作还好,对word文档就不怎么样了,特别是写入动态的数据,具体不知道怎么弄...
网上还有教程说将word文档的内容另存为Web页,我看看了,我保存后,打开的网页布局跟以前不一样了

难道没有更好的方案吗? 在线等...
...全文
5133 43 打赏 收藏 转发到动态 举报
写回复
用AI写文章
43 条回复
切换为时间正序
请发表友善的回复…
发表回复
rexuexinyidai 2011-11-22
  • 打赏
  • 举报
回复
http://18316025.blog.hexun.com/70421333_d.html
我的博客上有详谈,可以参考下
fuxingguang2008 2011-10-26
  • 打赏
  • 举报
回复
哎,伤感情哦,楼主你现在有什么好的办法解决了吗,我也在弄和你类似的功能,望指教!
Codefans_Fan 2011-08-28
  • 打赏
  • 举报
回复
我也寻求解决办法
lxh1962 2011-08-02
  • 打赏
  • 举报
回复
学习了,正在找这类处理方法
缘梦王子 2010-07-05
  • 打赏
  • 举报
回复
yong docx4j这个jar包 你试试 不错的
luochao5700 2010-06-02
  • 打赏
  • 举报
回复
docx4j这个jar包 你试试 不错的
pmlxp 2010-05-24
  • 打赏
  • 举报
回复
[Quote=引用 36 楼 zhglei_85 的回复:]
同样的问题啊 急需解决
[/Quote]
我都解决了一个星期了啊
zhglei_85 2010-04-26
  • 打赏
  • 举报
回复
同样的问题啊 急需解决
  • 打赏
  • 举报
回复
用JavaScript比较好!
fly2wind 2009-09-29
  • 打赏
  • 举报
回复
我也遇到同样问题。楼主解决了望分享以下。
lc328 2009-02-13
  • 打赏
  • 举报
回复
1。用jacob.
其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。
jacob下载:http://www.matrix.org.cn/down_view.asp?id=13
下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个例子:
import java.io.File;
import com.jacob.com.*;
import com.jacob.activeX.*;

public class FileExtracter{

public static void main(String[] args) {

ActiveXComponent app = new ActiveXComponent("Word.Application");
String inFile = "c:\\test.doc";
String tpFile = "c:\\temp.htm";
String otFile = "c:\\temp.xml";
boolean flag = false;
try {
app.setProperty("Visible", new Variant(false));
Object docs = app.getProperty("Documents").toDispatch();
Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();
Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}

}
}
2。用apache的poi来抽取word,excel。
poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你:
下载经过封装后的poi包:http://www.matrix.org.cn/down_view.asp?id=14
下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子:
import java.io.*;
import org.textmining.text.extraction.WordExtractor;
/**
* <p>Title: pdf extraction </p>
* <p>Description: email:chris@matrix.org.cn </p>
* <p>Copyright: Matrix Copyright (c) 2003 </p>
* <p>Company: Matrix.org.cn </p>
* @author chris
* @version 1.0,who use this example pls remain the declare
*/

public class PdfExtractor {
public PdfExtractor() {
}
public static void main(String args[]) throws Exception
{
FileInputStream in = new FileInputStream ("c:\\a.doc");
WordExtractor extractor = new WordExtractor();
String str = extractor.extractText(in);
System.out.println("the result length is"+str.length());
System.out.println("the result is"+str);
}
}

3。pdfbox-用来抽取pdf文件
但是pdfbox对中文支持还不好,先下载pdfbox:http://www.matrix.org.cn/down_view.asp?id=12
下面是一个如何使用pdfbox抽取pdf文件的例子:
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdfparser.PDFParser;
import java.io.*;
import org.pdfbox.util.PDFTextStripper;
import java.util.Date;
/**
* <p>Title: pdf extraction </p>
* <p>Description: email:chris@matrix.org.cn </p>
* <p>Copyright: Matrix Copyright (c) 2003 </p>
* <p>Company: Matrix.org.cn </p>
* @author chris
* @version 1.0,who use this example pls remain the declare
*/

public class PdfExtracter{

public PdfExtracter(){
}
public String GetTextFromPdf(String filename) throws Exception
{
String temp=null;
PDDocument pdfdocument=null;
FileInputStream is=new FileInputStream(filename);
PDFParser parser = new PDFParser( is );
parser.parse();
pdfdocument = parser.getPDDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter( out );
PDFTextStripper stripper = new PDFTextStripper();
stripper.writeText(pdfdocument.getDocument(), writer );
writer.close();
byte[] contents = out.toByteArray();

String ts=new String(contents);
System.out.println("the string length is"+contents.length+"\n");
return ts;
}
public static void main(String args[])
{
PdfExtracter pf=new PdfExtracter();
PDDocument pdfDocument = null;

try{
String ts=pf.GetTextFromPdf("c:\\a.pdf");
System.out.println(ts);
}
catch(Exception e)
{
e.printStackTrace();
}
}

}

4.抽取支持中文的pdf文件-xpdf
xpdf是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。
下载xpdf函数包:http://www.matrix.org.cn/down_view.asp?id=15
同时需要下载支持中文的补丁包:http://www.matrix.org.cn/down_view.asp?id=16
按照readme放好中文的patch,就可以开始写调用本地方法的java程序了
下面是一个如何调用的例子:
import java.io.*;
/**
* <p>Title: pdf extraction </p>
* <p>Description: email:chris@matrix.org.cn </p>
* <p>Copyright: Matrix Copyright (c) 2003 </p>
* <p>Company: Matrix.org.cn </p>
* @author chris
* @version 1.0,who use this example pls remain the declare
*/


public class PdfWin {
public PdfWin() {
}
public static void main(String args[]) throws Exception
{
String PATH_TO_XPDF="C:\\Program Files\\xpdf\\pdftotext.exe";
String filename="c:\\a.pdf";
String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};
Process p = Runtime.getRuntime().exec(cmd);
BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
InputStreamReader reader = new InputStreamReader(bis, "UTF-8");
StringWriter out = new StringWriter();
char [] buf = new char[10000];
int len;
while((len = reader.read(buf))>= 0) {
//out.write(buf, 0, len);
System.out.println("the length is"+len);
}
reader.close();
String ts=new String(buf);
System.out.println("the str is"+ts);
}
}
「已注销」 2009-02-13
  • 打赏
  • 举报
回复
看了一下你的问题,我觉得你可能有必要先看MSDN来熟悉一下Word的文档对象结构本身是个什么样子。
herowzz 2009-02-13
  • 打赏
  • 举报
回复
itext也可以生成word

附例:



import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;

public class RTFCreate {

public RTFCreate() {
}

public static void main(String[] args) { //main函数
RTFCreate rtfCreate = new RTFCreate();
try {
rtfCreate.createRTF();
JOptionPane.showMessageDialog(null, "表格已经成功创建");
} catch (MalformedURLException ex) {
JOptionPane.showMessageDialog(null, "表格导出出错,错误信息:" + ex
+ "\n错误原因可能是表格已经打开!");
ex.printStackTrace();
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, "表格导出出错,错误信息:" + ex
+ "\n错误原因可能是表格已经打开!");
ex.printStackTrace();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "表格导出出错,错误信息:" + ex
+ "\n错误原因可能是表格已经打开!");
ex.printStackTrace();
} catch (DocumentException ex) {
JOptionPane.showMessageDialog(null, "表格导出出错,错误信息:" + ex
+ "\n错误原因可能是表格已经打开!");
ex.printStackTrace();
}
}


/**
* 生成word的方法
*/
public void createRTF() throws FileNotFoundException, DocumentException,
MalformedURLException, IOException {
// 创建word文档
Document document = new Document(PageSize.A4);
// 输入word文档
RtfWriter2.getInstance(document, new FileOutputStream("d:\\word.doc"));
document.open();
// 中文字体
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.HELVETICA);
// 创建有3列的表格
Table table = new Table(3);
document.add(new Paragraph("生成rft文档!", fontChinese));
table.setBorderWidth(0);
table.setBorderColor(new Color(0, 0, 255));
table.setPadding(5);
table.setSpacing(0);

// 添加表头元素
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(new Paragraph("测试1", fontChinese));
table.addCell("big cell");
cell.setRowspan(2);
cell.setColspan(2);
table.addCell(cell);
table.addCell(new Paragraph("测试2", fontChinese));

Cell cell1 = new Cell("aaa");
cell1.setColspan(2);

Cell cell2 = new Cell("bbb");
cell2.setColspan(1);
table.addCell(cell1);
table.addCell(cell2);




document.add(table);
// 在表格末尾添加图片
// Image png = Image.getInstance("d:\\duck.jpg");
// document.add(png);





document.close();
}
}

Yuhao818 2009-02-13
  • 打赏
  • 举报
回复
mark
fanyuanwaifdl 2009-02-13
  • 打赏
  • 举报
回复
gz
javagxc 2009-02-12
  • 打赏
  • 举报
回复
没做过,学习学习啦
如一宝宝 2009-02-07
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 lowson0 的回复:]
引用 21 楼 lakesea 的回复:
如果是web的程序,可以把你的word2005以上文档另存为xml格式的,然后改后缀为.jsp,然后在相应的地方用 <%%>模式插入值。
注意头部引入的类不要换行,如下:
<%@page contentType="application/msword; charset=UTF-8" %> <%@ page import="com.hesoft.database.DBManager" %> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<%
//这个地方是代码,、
%>
<?mso-application pr…
[/Quote]
现在又出现一个问题,就是word文档里面的产品信息是表格形式存放的
这些产品的种类个数不确定,怎么控制表格的行数呢?\
生成的xml又大又难看得懂
zhjf02 2009-02-06
  • 打赏
  • 举报
回复
有一个方法LZ可以试一下
将模板用word写好另存为html格式;
将html格式改为jsp,并将打开方式调整为用msword方式;
在正文那用java字符串写你要写的正文
不知方法是否可行,仅供参考;
如一宝宝 2009-02-06
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 kingdomofhavean 的回复:]
我的jdk就是1.6的。
[/Quote]
我的word合同里面必须有页面页脚也行吗?
如果可以的话,能不能发一个小的demo给我看看.非常感谢
kingdomofhavean 2009-02-06
  • 打赏
  • 举报
回复
我的jdk就是1.6的。
加载更多回复(21)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧