用java生成word文档
poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你:
下载经过封装后的poi包:
这个包就是:tm-extractors-0.4.jar

下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子:
import java.io.*;
import org.textmining.text.extraction.WordExtractor;
/**
*

Title: pdf extraction


*

Description: email:chris@matrix.org.cn


*

Copyright: Matrix Copyright (c) 2003


*

Company: Matrix.org.cn


* @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);
}
}

如果没有这个包呢?就是下面这段了

read word:

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

<script>render_code();</script>

write word

代码
  1. public boolean writeWordFile(String path, String content) {
  2. boolean w = false;
  3. try {
  4. //  byte b[] = content.getBytes("ISO-8859-1");
  5. byte b[] = content.getBytes();
  6. ByteArrayInputStream bais = new ByteArrayInputStream(b);
  7. POIFSFileSystem fs = new POIFSFileSystem();
  8. DirectoryEntry directory = fs.getRoot();
  9. DocumentEntry de = directory.createDocument("WordDocument", bais);
  10. FileOutputStream ostream = new FileOutputStream(path);
  11. fs.writeFilesystem(ostream);
  12. bais.close();
  13. ostream.close();
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }
  17. return w;
  18. }

<script>render_code();</script>


写操作的代码还是有些问题:打开WORD时提示要选择字符类型
希望能改进!

 这样写文件有问题,因为不是word格式。

当然这几个jar是少不了的
poi-2.5.1-final-20040804.jar
poi-contrib-2.5.1-final-20040804.jar
poi-scratchpad-2.5.1-final-20040804.jar

如果要直接用Jakarta POI HWPF,没提供编译好的下载,只是原码了,自己编译吧
jakarta POI开源项目组HWPF(在下载后的scratchpad目录里)是操作word文档,在这里作了个简单的例子
下载地址: http://www.apache.org/dist/jakarta/Poi/

<!--age contentType="text/html; charset=GBK" import="java.io.*,org.apache.poi.hwpf.HWPFDocument,org.apache.poi.hwpf.usermodel.*,org.apache.poi.hwpf.model.*"-->

<!--r /> HWPFDocument doc = new HWPFDocument(new FileInputStream("g://a.doc"));<br /> Range r = doc.getRange ();   //取得word文档的范围<br />   StyleSheet styleSheet = doc.getStyleSheet ();<br /> <br />   int sectionLevel = 0;<br />   int lenParagraph = r.numParagraphs ();//取得段落数<br />   int c=r.numCharacterRuns();<br />   int b=r.numSections();<br />   String s=r.text();<br />   boolean inCode = false;<br />   // Paragraph p;<br />   for (int x = 0; x < lenParagraph; x++)<br />   {<br />     Paragraph p = r.getParagraph (x);<br /> <br />     String text = p.text ();<br />    -->

<!--ex-->

<!--r />     if (text.trim ().length () == 0)<br />     {<br />     continue;<br />     }<br /> <br /> <br />     }<br /> <br />   //doc.write(new FileOutputStream("g://b.doc"));<br /> <br /> <br /> <br /> -->
char:

section:

text:

java操作word,可以试试java2word

java2word 是一个在java程序中调用 MS Office Word 文档的组件(类库)。该组件提供了一组简单的接口,以便java程序调用他的服务操作Word 文档。
这些服务包括:
打开文档、新建文档、查找文字、替换文字,
插入文字、插入图片、插入表格,
在书签处插入文字、插入图片、插入表格等。
填充数据到表格中读取表格数据
1.1版增强的功能:
@指定文本样式,指定表格样式。如此,则可动态排版word文档。
@填充表格数据时,可指定从哪行哪列开始填充。配合输入数据的大小,你可以修改表中的任意部分,甚至只修改一个单元格的内容。
@合并单元格。
更多激动人心的功能见详细说明: http://www.heavenlake.com/java2word/doc
免费下载:http://dev.heavenlake.com:81/developer/listthreads?forum=8

用java生成word文档

  • java

作者 javasky @ 2006-06-10 14:22:04

这几日, 公司有个项目, 要用java生成word文档, 在网上找来找去也没有找到好的生成word文档的库, 找到apache的POI可以使用, 但是所有的release版中也没有支持word的class. 只能从svn上下载源代码编译.
后来发现java支持rtf格式的文档, word也支持, 于是乎便使用此产生word文档了. 呵呵..
java支持的rtf文档功能不是很强大, 我们可以借助于一些开源的库, 比如: itext就可以很好的支持. itext上有很多例子, 有兴趣的可以上去看一下, 这里就不摘录了.
但是itext比较大要1.4M, 不是很喜欢. 在sf上找来找去, 发现一个更小的库, 尽管功能不是很强大, 基本的功能都有, 他就是srw(Simple RTF Writer目前它的版本是0.6,好久都没有人维护了).
srw内置了很多例子,  例如: 我们要写一个简单的rtf, 我们只需要这么写:
public class TestSimpleRtf {
    
    private static final String FILE_NAME = "out_testsimplertf.rtf";
    
    public static void main(String[] args) {
        try {
            // RTF Dokument generieren (in Querformat)
            RTFDocument doc = new RTFDocument(PageSize.DIN_A4_QUER);
            // Anzeige-Zoom und Ansicht definieren
            doc.setViewscale(RTFDocument.VIEWSCALE_FULLPAGE);    // Anzeige-Zoom auf "komplette Seite" einstellen
            doc.setViewkind(RTFDocument.VIEWKIND_PAGELAYOUT);    // ViewMode auf Seitenlayout stellen
            
            Paragraph absatz = new Paragraph(18, 0, 16, Font.ARIAL, new TextPart("Simple RTF Writer Testdokument"));
            absatz.setAlignment(Paragraph.ALIGN_CENTER);
            doc.addParagraph(absatz);
            File savefile = new File(FILE_NAME);
            doc.save(savefile);
            System.out.println("Neues RTF Dokument erstellt: " + savefile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }            
    }
}
用法很简单, 但是功能很少, 比如没有table的功能, 不能设置打印方向等问题. 不过这个基本上就够用了.
后来, 我们的项目要求横向打印, 这可难坏了. 没办法, 自己查找word的rtf格式库, 拓展横向打印功能, 目前已经完成...
import com.itseasy.rtf.RTFDocument;
import com.itseasy.rtf.text.PageSize;

public class MyRTFDocument extends RTFDocument {
    public static final int ORIENTATION_PORTRAIT = 0;
    public static final int ORIENTATION_LANDSCAPE = 1;
    private int orientation;
    
    /**
     * 
     */
    public MyRTFDocument() {
        super();
    }
    /**
     * @param arg0
     */
    public MyRTFDocument(PageSize arg0) {
        super(arg0);
    }
    /* (non-Javadoc)
     * @see com.itseasy.rtf.RTFDocument#getDocumentAsString()
     */
    protected String getDocumentAsString() {
        StringBuffer sb = new StringBuffer(super.getDocumentAsString());
        int pos = -1;
        if (ORIENTATION_LANDSCAPE == orientation) {
            pos = sb.indexOf("paperw");
            if (pos > 0) {
                sb.insert(pos, "lndscpsxn");
            }
        }
        pos = 0;
        while((pos = sb.indexOf("pardplain", pos)) > 0){
            pos = sb.indexOf("{", pos);
            sb.insert(pos, "dbchaf2");
        }
        return sb.toString();
    }
    /**
     * @return Returns the orientation.
     */
    public int getOrientation() {
        return orientation;
    }
    /**
     * @param orientation The orientation to set.
     */
    public void setOrientation(int orientation) {
        this.orientation = orientation;
    }
    
}

在最近的一个项目中需要将一段字符类型的文本存为word,html并要将word的内容保存在数据库中,于是就有了如下的一个工具类,希望能对碰到这样需求的朋友提供点帮助。
       匆匆忙忙的就copy上来了,没有做一些删减,有一些多余的东西,有兴趣的朋友可以自行略去。我的注释相对比较清楚,可以按照自己的需求进行组合。
      在操作word的地方使用了jacob(jacob_1.9),这个工具网上很容易找到,将jacob.dll放置系统Path中,直接放在system32下也可以,jacob.jar放置在classPath中。

代码如下:WordBridge.java

/**
 * WordBridge.java
 */
package com.kela.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.kela.db.PoolingDataSource;

/**
 * 说明: 对word的操作

*
 * @author   kela.kf@gmail.com
 */
public class WordBridge {
 
   Log log = LogFactory.getLog("WordBridgt");
 
   private ActiveXComponent MsWordApp = null; 
   private Dispatch document = null;
   
    /**
     * 打开word
     * @param makeVisible, true显示word, false不显示word
     */
    public void openWord(boolean makeVisible) {
       if (MsWordApp == null) {
         MsWordApp = new ActiveXComponent("Word.Application");
       }
  
       Dispatch.put(MsWordApp, "Visible", new Variant(makeVisible));
    }

/**
     * 创建新的文档
     *
     */
    public void createNewDocument() {
       Dispatch documents = Dispatch.get(MsWordApp, "Documents").toDispatch();
       document = Dispatch.call(documents, "Add").toDispatch();
    }

/**
     * 关闭文档
     */
    public void closeDocument() {
      // 0 = wdDoNotSaveChanges
      // -1 = wdSaveChanges
      // -2 = wdPromptToSaveChanges
      Dispatch.call(document, "Close", new Variant(0));
      document = null;
    }

/**
     * 关闭word
     *
     */
    public void closeWord() {
       Dispatch.call(MsWordApp, "Quit");
       MsWordApp = null;
       document = null;
    }
 
    /**
     * 插入文本
     * @param textToInsert 文本内容
     */
    public void insertText(String textToInsert) {
       Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
       Dispatch.put(selection, "Text", textToInsert);
    }

/**
     * 保存文件
     * @param filename
     */
    public void saveFileAs(String filename) {
      Dispatch.call(document, "SaveAs", filename);
    }

/**
     * 将word转换成html
     * @param htmlFilePath
     */
    public void wordToHtml(String htmlFilePath) {
         Dispatch.invoke(document,"SaveAs", Dispatch.Method, new Object[]{htmlFilePath,new Variant(8)}, new int[1]);
     }

/**
     * 保存word的同时,保存一个html
     * @param text 需要保存的内容
     * @param wordFilePath word的路径
     * @param htmlFilePath html的路径
     * @throws LTOAException
     */
    public void wordAsDbOrToHtml(String text, String wordFilePath, String htmlFilePath) throws LTOAException {
  
      try {
         openWord(false);
         createNewDocument();
         insertText(text);
         saveFileAs(wordFilePath);
         wordToHtml(htmlFilePath);
     } catch (Exception ex) {
         log.error("错误 - 对word的操作发生错误");
         log.error("原因 - " + ex.getMessage());
         throw new LTOAException(LTOAException.ERR_UNKNOWN, "对word的操作发生错误("
                    + this.getClass().getName() + ".wordAsDbOrToHtml())", ex);
     } finally {
         closeDocument();
         closeWord();
     }
  
   }

/**
    * 将word保存至数据库
    * @param wordFilePath
    * @param RecordID
    * @throws LTOAException
    */
    public void wordAsDatabase(String wordFilePath, String RecordID) throws LTOAException {

Connection conn = null;
       PreparedStatement pstmt = null;
       PoolingDataSource pool = null;
       
       File file = null;
    
       String sql = "";
       try {
           sql = " UPDATE Document_File SET FileBody = ? WHERE RecordID = ? ";
            
           pool = new PoolingDataSource();
           conn = pool.getConnection();
        
           file = new File(wordFilePath);
           InputStream is = new FileInputStream(file);
           byte[] blobByte = new byte[is.available()];
           is.read(blobByte);
           is.close();

pstmt = conn.prepareStatement(sql);
          pstmt.setBinaryStream(1,(new ByteArrayInputStream(blobByte)), blobByte.length);
          pstmt.setString(2, RecordID); 
          pstmt.executeUpdate();
     
        } catch (Exception ex) {
            log.error("错误 - 表 Document_File 更新数据发生意外错误");
            log.error("原因 - " + ex.getMessage());
            throw new LTOAException(LTOAException.ERR_UNKNOWN,
                   "表Document_File插入数据发生意外错误("
                    + this.getClass().getName() + ".wordAsDatabase())", ex);
         } finally {
             pool.closePrepStmt(pstmt);
             pool.closeConnection(conn);
        }
    }
 
   /**
    * 得到一个唯一的编号
    * @return 编号
    */
   public String getRecordID() {
  
     String sRecordID = "";
  
     java.util.Date dt=new java.util.Date();
        long lg=dt.getTime();
        Long ld=new Long(lg);
        sRecordID =ld.toString();
       
        return sRecordID;
    }
 
    /**
     * 得到保存word和html需要的路径
     * @param systemType 模块类型 givInfo, sw, fw
     * @param fileType 文件类型 doc, html
     * @param recID 文件编号
     * @return 路径
     */
     public String getWordFilePath(String systemType, String fileType, String recID) {
  
       String filePath = "";

用java生成word文档(转载)相关推荐

  1. [摘]用Java生成Word文档

    开发中隔三叉五的就要用到Word,经常被搞得不胜其烦,不过这次找到了不少好例子,干脆将他们都摘了过来,内容如下: 1. poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这 ...

  2. java生成word文档freemarker

    java freemarker + word 模板 生成 word 文档 (变量替换,数据的循环,表格数据的循环,以及图片的替换) 1,最近有个需求,动态生成 Word 文当并供前端下载,网上找了一下 ...

  3. Java 生成Word文档 — 简单示例

    前言 这篇文章将介绍如何使用免费Java Word组件Free Spire.Doc for Java在Java应用程序中生成Word文档,插入文本,并设置段落的字体格式.对齐方式以及段后间距等. Fr ...

  4. Java生成Word文档

    在开发文档系统或办公系统的过程中,有时候我们需要导出word文档.在网上发现了一个用PageOffice生成word文件的功能,就将这块拿出来和大家分享. 生成word文件与我们编辑word文档本质上 ...

  5. java生成word文档(最简单)

    首先说明,使用该方法时,尽量不要使用wps新建word文档,经测试,手机不能兼容,出现很多格式问题,office则手机可以很好的兼容,所以,本文以office做教程 1 首先新建一个word文档,然后 ...

  6. 记录一次用Java生成word文档的经验

    业务背景 最近接到一个需求需要将学员的基本信息生成word格式的内容,word的格式如下图所示 开发的任务就是将学员的信息替换掉表格中的** 即可,感觉还蛮简单的. 相信大家以前做的最多的是Java和 ...

  7. java word模板poi生成文件_poi读写word模板 / java生成word文档

    有一word文档表格 形如: 姓名 ${name} 电话 ${tel} 下载包链接:点击进入 从数据库读取记录替换上述变量 import java.io.FileOutputStream; impor ...

  8. java生成word文档_Java多种方式动态生成doc文档

    本来是要在Android端生成doc的(这需求...),最后方法没有好的方法能够在Android上做到完美,最后还是只能搬迁到服务器.不浪费,还是记录下各框架不支持Android的原因以及他们的特点. ...

  9. java生成word文档 图片_java生成带有图片的word的文档-Go语言中文社区

    生成带有图片的word的文档 @RequestMapping(params ="getWordByDate",produces = "text/html;charset= ...

最新文章

  1. sql 导航函数 lag
  2. altium 去掉部分铺铜_干货|HFSS器件导入Altium进行PCB制作教程!!!
  3. [JavaWeb-JavaScript]JavaScript运算符
  4. php mysql_query预处理,php+mysqli使用预处理技术进行数据库查询的方法
  5. Problem B: 字符类的封装
  6. linux 安装rmp服务,Linux LAMP服务的rpm包安装与配置
  7. count返回0_MySQL实战 | 14 为什么count(*)越来越慢?
  8. 【2018.10.2】Note of CXM
  9. 析构函数无法命中断点. 当前无法命中断点: 没有与此行关联的调试程序的目标代码类型的可执行代码. 可能的原因包括;条件编译, 编译器优化或者......
  10. 使用 strtok 提取 ip (点分十进制)网段
  11. zip 后压缩包带路径
  12. Oracle常见索引扫描方式总结
  13. 收藏了8年的PHP优秀资源,都给你整理好了
  14. 程序员价值观和网络复杂性-网络不可达排查过程
  15. Ingenuous Cubrency UVA 11137
  16. 弹出 think-cell 许可证密钥窗口丨使用教程
  17. 水下SLAM论文!!!
  18. 前端传图片file给后端,后端接收为空,0kb
  19. 【科普向】谁都能看懂的CRC(循环冗余校验)原理
  20. C语言字符串使用指南

热门文章

  1. 摄像头 UIImagePickerController拍照和视频录制
  2. 使用dnsmasq,实现本地的DNS服务
  3. Uni App input 手机键盘回车搜索
  4. 第1章 游戏之乐——连连看游戏设计
  5. 计算机基础教育对工科学生的意义,深入进行高校工科非计算机专业计算机基础教育改革…...
  6. 如何用软件测试电源好坏,电脑电源怎么测试好坏?如何测试电脑电源好坏?
  7. 测鬼记(上)——回岗(十)
  8. Docker自定义镜像上传阿里云
  9. 犀牛中斑马纹分析的作用
  10. SSM综合复习之MyBatis