java如何生成word

jun526 2009-06-14 03:33:41
现在通过poi,或是在客户端加<%@page contentType="application/msword;charset=GBK"%>实现生成word格式文档,
但是就在格式上有一些问题,特别是分页控制不了,不知如何控制分页,在线等,谢谢大家了。
...全文
899 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
dahaidao 2010-01-11
  • 打赏
  • 举报
回复
如果想和word 样,可以用控件。比如金格什么的。或找个其它公司做的。
IT_lau 2010-01-11
  • 打赏
  • 举报
回复
xuexi
visualC120 2009-12-08
  • 打赏
  • 举报
回复
哇,又学习了一次....
xiaogang_lu 2009-12-08
  • 打赏
  • 举报
回复
了解一下
steffen2008 2009-12-08
  • 打赏
  • 举报
回复
用POI可以实现.
hxy960 2009-12-08
  • 打赏
  • 举报
回复
上面的回复,有能将网页图片也生成word的没,谢谢
xql80329 2009-11-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ilysony 的回复:]
Java codeLZ去搜一下 JAVA 生成 WORD
[/Quote]
同意
lzt2008 2009-11-23
  • 打赏
  • 举报
回复



1. public class WordExtractor {
2. public WordExtractor() {
3. }
4.
5. public String extractText(InputStream in) throws IOException {
6. ArrayList text = new ArrayList();
7. POIFSFileSystem fsys = new POIFSFileSystem(in);
8.
9. DocumentEntry headerProps = (DocumentEntry) fsys.getRoot().getEntry("WordDocument");
10. DocumentInputStream din = fsys.createDocumentInputStream("WordDocument");
11. byte[] header = new byte[headerProps.getSize()];
12.
13. din.read(header);
14. din.close();
15. // Prende le informazioni dall'header del documento
16. int info = LittleEndian.getShort(header, 0xa);
17.
18. boolean useTable1 = (info & 0x200) != 0;
19.
20. //boolean useTable1 = true;
21.
22. // Prende informazioni dalla piece table
23. int complexOffset = LittleEndian.getInt(header, 0x1a2);
24. //int complexOffset = LittleEndian.getInt(header);
25.
26. String tableName = null;
27. if (useTable1) {
28. tableName = "1Table";
29. } else {
30. tableName = "0Table";
31. }
32.
33. DocumentEntry table = (DocumentEntry) fsys.getRoot().getEntry(tableName);
34. byte[] tableStream = new byte[table.getSize()];
35.
36. din = fsys.createDocumentInputStream(tableName);
37.
38. din.read(tableStream);
39. din.close();
40.
41. din = null;
42. fsys = null;
43. table = null;
44. headerProps = null;
45.
46. int multiple = findText(tableStream, complexOffset, text);
47.
48. StringBuffer sb = new StringBuffer();
49. int size = text.size();
50. tableStream = null;
51.
52. for (int x = 0; x < size; x++) {
53.
54. WordTextPiece nextPiece = (WordTextPiece) text.get(x);
55. int start = nextPiece.getStart();
56. int length = nextPiece.getLength();
57.
58. boolean unicode = nextPiece.usesUnicode();
59. String toStr = null;
60. if (unicode) {
61. toStr = new String(header, start, length * multiple, "UTF-16LE");
62. } else {
63. toStr = new String(header, start, length, "ISO-8859-1");
64. }
65. sb.append(toStr).append(" ");
66.
67. }
68. return sb.toString();
69. }
70.
71. private static int findText(byte[] tableStream, int complexOffset, ArrayList text)
72. throws IOException {
73. //actual text
74. int pos = complexOffset;
75. int multiple = 2;
76. //skips through the prms before we reach the piece table. These contain data
77. //for actual fast saved files
78. while (tableStream[pos] == 1) {
79. pos++;
80. int skip = LittleEndian.getShort(tableStream, pos);
81. pos += 2 + skip;
82. }
83. if (tableStream[pos] != 2) {
84. throw new IOException("corrupted Word file");
85. } else {
86. //parse out the text pieces
87. int pieceTableSize = LittleEndian.getInt(tableStream, ++pos);
88. pos += 4;
89. int pieces = (pieceTableSize - 4) / 12;
90. for (int x = 0; x < pieces; x++) {
91. int filePos =
92. LittleEndian.getInt(tableStream, pos + ((pieces + 1) * 4) + (x *"/images/forum/smiles/icon_cool.gif"/> + 2);
93. boolean unicode = false;
94. if ((filePos & 0x40000000) == 0) {
95. unicode = true;
96. } else {
97. unicode = false;
98. multiple = 1;
99. filePos &= ~(0x40000000); //gives me FC in doc stream
100. filePos /= 2;
101. }
102. int totLength =
103. LittleEndian.getInt(tableStream, pos + (x + 1) * 4)
104. - LittleEndian.getInt(tableStream, pos + (x * 4));
105.
106. WordTextPiece piece = new WordTextPiece(filePos, totLength, unicode);
107. text.add(piece);
108.
109. }
110.
111. }
112. return multiple;
113. }
114. public static void main(String[] args){
115. WordExtractor w = new WordExtractor();
116. POIFSFileSystem ps = new POIFSFileSystem();
117. try{
118.
119. File file = new File("C:\\test.doc");
120.
121. InputStream in = new FileInputStream(file);
122. String s = w.extractText(in);
123. System.out.println(s);
124.
125.
126. }catch(Exception e){
127. e.printStackTrace();
128. }
129.
130. }
131.
132. }
133. class WordTextPiece {
134. private int _fcStart;
135. private boolean _usesUnicode;
136. private int _length;
137.
138. public WordTextPiece(int start, int length, boolean unicode) {
139. _usesUnicode = unicode;
140. _length = length;
141. _fcStart = start;
142. }
143. public boolean usesUnicode() {
144. return _usesUnicode;
145. }
146.
147. public int getStart() {
148. return _fcStart;
149. }
150. public int getLength() {
151. return _length;
152. }
153.
154. }


write word



1. public boolean writeWordFile(String path, String content) {
2. boolean w = false;
3. try {
4.
5. // byte b[] = content.getBytes("ISO-8859-1");
6. byte b[] = content.getBytes();
7.
8. ByteArrayInputStream bais = new ByteArrayInputStream(b);
9.
10. POIFSFileSystem fs = new POIFSFileSystem();
11. DirectoryEntry directory = fs.getRoot();
12.
13. DocumentEntry de = directory.createDocument("WordDocument", bais);
14.
15. FileOutputStream ostream = new FileOutputStream(path);
16.
17. fs.writeFilesystem(ostream);
18.
19. bais.close();
20. ostream.close();
21.
22. } catch (IOException e) {
23. e.printStackTrace();
24. }
25.
26. return w;
27. }
mengshan1986 2009-11-23
  • 打赏
  • 举报
回复
http://poi.apache.org/
去看看apache基金会的开源项目吧,下面内容摘自百度百科
 三.Apache POI
  Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能。
  结构:
  HSSF - 提供读写Microsoft Excel格式档案的功能。
  XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
  HWPF - 提供读写Microsoft Word格式档案的功能。
  HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
  HDGF - 提供读写Microsoft Visio格式档案的功能。
  创建Excel 文档
  示例1将演示如何利用Jakarta POI API 创建Excel 文档。
  示例1程序如下:
  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  import org.apache.poi.hssf.usermodel.HSSFSheet;
  import org.apache.poi.hssf.usermodel.HSSFRow;
  import org.apache.poi.hssf.usermodel.HSSFCell;
  import java.io.FileOutputStream;
  public class CreateXL {
  /** Excel 文件要存放的位置,假定在D盘下*/
  public static String outputFile="D:\\test.xls";
  public static void main(String argv[]){
  try{
  // 创建新的Excel 工作簿
  HSSFWorkbook workbook = new HSSFWorkbook();
  // 在Excel工作簿中建一工作表,其名为缺省值
  // 如要新建一名为"效益指标"的工作表,其语句为:
  // HSSFSheet sheet = workbook.createSheet("效益指标");
  HSSFSheet sheet = workbook.createSheet();
  // 在索引0的位置创建行(最顶端的行)
  HSSFRow row = sheet.createRow((short)0);
  //在索引0的位置创建单元格(左上端)
  HSSFCell cell = row.createCell((short) 0);
  // 定义单元格为字符串类型
  cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  // 在单元格中输入一些内容
  cell.setCellValue("增加值");
  // 新建一输出文件流
  FileOutputStream fOut = new FileOutputStream(outputFile);
  // 把相应的Excel 工作簿存盘
  workbook.write(fOut);
  fOut.flush();
  // 操作结束,关闭文件
  fOut.close();
  System.out.println("文件生成...");
  }catch(Exception e) {
  System.out.println("已运行 xlCreate() : " + e );
  }
  }
  }
  读取Excel文档中的数据
  示例2将演示如何读取Excel文档中的数据。假定在D盘JTest目录下有一个文件名为test1.xls的Excel文件。
  示例2程序如下:
  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  import org.apache.poi.hssf.usermodel.HSSFSheet;
  import org.apache.poi.hssf.usermodel.HSSFRow;
  import org.apache.poi.hssf.usermodel.HSSFCell;
  import java.io.FileInputStream;
  public class ReadXL {
  /** Excel文件的存放位置。注意是正斜线*/
  public static String fileToBeRead="D:\\test1.xls";
  public static void main(String argv[]){
  try{
  // 创建对Excel工作簿文件的引用
  HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
  // 创建对工作表的引用。
  // 本例是按名引用(让我们假定那张表有着缺省名"Sheet1")
  HSSFSheet sheet = workbook.getSheet("Sheet1");
  // 也可用getSheetAt(int index)按索引引用,
  // 在Excel文档中,第一张工作表的缺省索引是0,
  // 其语句为:HSSFSheet sheet = workbook.getSheetAt(0);
  // 读取左上端单元
  HSSFRow row = sheet.getRow(0);
  HSSFCell cell = row.getCell((short)0);
  // 输出单元内容,cell.getStringCellValue()就是取所在单元的值
  System.out.println("左上端单元是: " + cell.getStringCellValue());
  }catch(Exception e) {
  System.out.println("已运行xlRead() : " + e );
  }
  }
  }
  设置单元格格式
  在这里,我们将只介绍一些和格式设置有关的语句,我们假定workbook就是对一个工作簿的引用。在Java中,第一步要做的就是创建和设置字体和单元格的格式,然后再应用这些格式:
  1、创建字体,设置其为红色、粗体:
  HSSFFont font = workbook.createFont();
  font.setColor(HSSFFont.COLOR_RED);
  font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  2、创建格式
  HSSFCellStyle cellStyle= workbook.createCellStyle();
  cellStyle.setFont(font);
  3、应用格式
  HSSFCell cell = row.createCell((short) 0);
  cell.setCellStyle(cellStyle);
  cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  cell.setCellValue("标题 "); 
  处理WORD文档
  import java.io.*;
  import org.textmining.text.extraction.WordExtractor;
  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  import org.apache.poi.hssf.usermodel.HSSFSheet;
  import org.apache.poi.hssf.usermodel.HSSFRow;
  import org.apache.poi.hssf.usermodel.HSSFCell;
  public class TestPoi {
  public TestPoi() {
  }
  public static void main(String args[]) throws Exception
  {
  FileInputStream in = new FileInputStream ("D:\\a.doc");
  WordExtractor extractor = new WordExtractor();
  String str = extractor.extractText(in);
  //System.out.println("the result length is"+str.length());
  System.out.println(str);
  }
  }
peizhi051287 2009-11-23
  • 打赏
  • 举报
回复
学习了一下
gcw633 2009-11-23
  • 打赏
  • 举报
回复
谁能给个比较完整的实例出来啊
jackjboss 2009-06-22
  • 打赏
  • 举报
回复
你就把你要打印的word模板另存为xml的文件。然后在java中来解释这个xml文件,把模板中要替换的值从数据库中查询出来替换掉就可以的!其实很简单。我就在做这个打印word。
bjwantong 2009-06-22
  • 打赏
  • 举报
回复
http://blog.soft6.com/187936
byxiaolin1987 2009-06-18
  • 打赏
  • 举报
回复
金格公司有在做这个控件,我们公司也是用的他们的控件,可以在页面生成word文档,数据库中用BLOB存储,可以插入图片。
jinchun1234 2009-06-18
  • 打赏
  • 举报
回复
关注
  • 打赏
  • 举报
回复
学习了
jun526 2009-06-18
  • 打赏
  • 举报
回复
难道大家都没有做过这方面的吗?
heavilyarmed 2009-06-17
  • 打赏
  • 举报
回复
poi强大d
zhaodalong 2009-06-17
  • 打赏
  • 举报
回复
poi这个东东好呀!
zou118627 2009-06-17
  • 打赏
  • 举报
回复
学习了。。
加载更多回复(8)

81,092

社区成员

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

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