zoukankan      html  css  js  c++  java
  • Java使用iText生成word文件的完美解决方案(亲测可行)

    JAVA生成WORD文件的方法目前有以下种:

    一种是jacob 但是局限于windows平台 往往许多JAVA程序运行于其他操作系统 在此不讨论该方案

    一种是pio但是他的excel处理很程序 word模块还局限于读取word的文本内容,写word文件就更弱项了

     

    当我使用这个JAVA生成RTF文件时费了好大的劲,原本是想生成WORD文档的,但是WORD文档POI只支持往生成的WORD中填入文本,对于图片根本就不支持。后来想想,RTF格式的也可用WORD打开,不如生成RTF。结果上网搜了很多技术,Itext,jacob,java2word,rtftemplate,reportrunner看了近一天也没什么头绪,写这些示例的几乎没有,还好Itext的例子有那么几个,可是我上官网下了最新核心包后发现,导入例子中居然全部红叉,原本以为上错网站了,再经过核实还是对的,于是断定网上的例子肯定有误,itext或许不能用。绕了大半天其他的技术我真的没法弄了,还是回到了iText,静下心来思考觉得包肯定有问题,仔细一看原来最新版的是支持PDF版的iText-5.0.1.jar是不对的,本来以为最新的功能最全了,没想到错了,想到这里赶紧下了稍微iText-2.1.7.jar结果终于成功了,感慨不已!现贡献代码如下记住官网上只能下到核心包:iText-1.2.7.jar和支持rtf的iText-rtf-2.1.7.jar这两个貌似对了,其实还有一个包是比较重要的iTextAsian.jar这个包对于设置字体什么的起了关键作用上网可以搜到的.

    本文介绍的是itext生成rtf文件并保存格式为word 此方案本人已实践过 并已在项目中使用

    用到的jar包: 
    iText-2.1.7.jar
    iText-rtf-2.1.7.jar
    iTextAsian.jar

    [java] view plain copy
     
    1. package com.rye.test;    
    2. import java.awt.Color;    
    3. import java.io.FileNotFoundException;    
    4. import java.io.FileOutputStream;    
    5. import java.io.IOException;    
    6.    
    7. import com.lowagie.text.Cell;    
    8. import com.lowagie.text.Document;    
    9. import com.lowagie.text.DocumentException;    
    10. import com.lowagie.text.Font;    
    11. import com.lowagie.text.PageSize;    
    12. import com.lowagie.text.Paragraph;    
    13. import com.lowagie.text.Table;    
    14. import com.lowagie.text.rtf.RtfWriter2;    
    15. /**   
    16.   * 创建word文档 步骤:    
    17.   * 1,建立文档    
    18.   * 2,创建一个书写器    
    19.   * 3,打开文档    
    20.   * 4,向文档中写入数据    
    21.   * 5,关闭文档   
    22.   */   
    23.  public class WordDemo {    
    24.     
    25.   public WordDemo() {    
    26.   }    
    27.     
    28.   /**   
    29.    * @param args   
    30.    */   
    31.   public static void main(String[] args) {    
    32.  // 创建word文档,并设置纸张的大小  
    33.    Document document = new Document(PageSize.A4);   
    34.    try {    
    35.     RtfWriter2.getInstance(document,  
    36.  new FileOutputStream("E:/word.doc"));    
    37.    
    38.     document.open();    
    39.        
    40.    //设置合同头    
    41.        
    42.    Paragraph ph = new Paragraph();    
    43.    Font f  = new Font();    
    44.        
    45.    Paragraph p = new Paragraph("出口合同",   
    46.  new Font(Font.NORMAL, 18, Font.BOLDITALIC, new Color(0, 0, 0)) );    
    47.     p.setAlignment(1);    
    48.     document.add(p);    
    49.     ph.setFont(f);    
    50.     
    51.     // 设置中文字体    
    52.     // BaseFont bfFont =    
    53.     // BaseFont.createFont("STSongStd-Light",  
    54.  "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);    
    55.     // Font chinaFont = new Font();    
    56.     /*   
    57.      * 创建有三列的表格   
    58.      */   
    59.     Table table = new Table(4);    
    60.     document.add(new Paragraph("生成表格"));    
    61.     table.setBorderWidth(1);    
    62.     table.setBorderColor(Color.BLACK);    
    63.     table.setPadding(0);    
    64.     table.setSpacing(0);    
    65.         
    66.     /*   
    67.      * 添加表头的元素   
    68.      */   
    69.     Cell cell = new Cell("表头");//单元格    
    70.     cell.setHeader(true);    
    71.     cell.setColspan(3);//设置表格为三列    
    72.     cell.setRowspan(3);//设置表格为三行    
    73.     table.addCell(cell);    
    74.     table.endHeaders();// 表头结束    
    75.    
    76.     // 表格的主体    
    77.     cell = new Cell("Example cell 2");    
    78.     cell.setRowspan(2);//当前单元格占两行,纵向跨度    
    79.     table.addCell(cell);    
    80.     table.addCell("1,1");    
    81.     table.addCell("1,2");    
    82.     table.addCell("1,3");    
    83.     table.addCell("1,4");    
    84.     table.addCell("1,5");    
    85.     table.addCell(new Paragraph("用java生成的表格1"));    
    86.     table.addCell(new Paragraph("用java生成的表格2"));    
    87.     table.addCell(new Paragraph("用java生成的表格3"));    
    88.     table.addCell(new Paragraph("用java生成的表格4"));    
    89.     document.add(new Paragraph("用java生成word文件"));    
    90.     document.add(table);    
    91.     document.close();    
    92.    } catch (FileNotFoundException e) {    
    93.     e.printStackTrace();    
    94.    } catch (DocumentException e) {    
    95.     e.printStackTrace();    
    96.    } catch (IOException e) {    
    97.     e.printStackTrace();    
    98.    }    
    99.   }    
    100.     
    101.  }   



    代码2:

    [java] view plain copy
     
    1. <span style="">import java.awt.Color;  
    2. import java.io.FileNotFoundException;  
    3. import java.io.FileOutputStream;  
    4. import java.io.IOException;  
    5. import com.lowagie.text.Cell;  
    6. import com.lowagie.text.Document;  
    7. import com.lowagie.text.DocumentException;  
    8. import com.lowagie.text.Element;  
    9. import com.lowagie.text.Font;  
    10. import com.lowagie.text.PageSize;  
    11. import com.lowagie.text.Paragraph;  
    12. import com.lowagie.text.Table;  
    13. import com.lowagie.text.pdf.BaseFont;  
    14. import com.lowagie.text.rtf.RtfWriter2;  
    15. public class CreateWordDemo {  
    16.     public void createDocContext(String file,String contextString)throws DocumentException, IOException{  
    17.         //设置纸张大小  
    18.         Document document = new Document(PageSize.A4);  
    19.         //建立一个书写器,与document对象关联  
    20.         RtfWriter2.getInstance(document, new FileOutputStream(file));  
    21.         document.open();  
    22.         //设置中文字体  
    23.         BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);  
    24.         //标题字体风格  
    25.         Font titleFont = new Font(bfChinese,12,Font.BOLD);  
    26.         //正文字体风格  
    27.         Font contextFont = new Font(bfChinese,10,Font.NORMAL);  
    28.         Paragraph title = new Paragraph("标题");  
    29.         //设置标题格式对齐方式  
    30.         title.setAlignment(Element.ALIGN_CENTER);  
    31.         title.setFont(titleFont);  
    32.         document.add(title);  
    33.         Paragraph context = new Paragraph(contextString);  
    34.         context.setAlignment(Element.ALIGN_LEFT);  
    35.         context.setFont(contextFont);  
    36.         //段间距  
    37.         context.setSpacingBefore(3);  
    38.         //设置第一行空的列数  
    39.         context.setFirstLineIndent(20);  
    40.         document.add(context);  
    41.         //设置Table表格,创建一个三列的表格  
    42.         Table table = new Table(3);  
    43.         int width[] = {25,25,50};//设置每列宽度比例  
    44.         table.setWidths(width);  
    45.         table.setWidth(90);//占页面宽度比例  
    46.         table.setAlignment(Element.ALIGN_CENTER);//居中  
    47.         table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中  
    48.         table.setAutoFillEmptyCells(true);//自动填满  
    49.         table.setBorderWidth(1);//边框宽度  
    50.         //设置表头  
    51.         Cell haderCell = new Cell("表格表头");  
    52.         haderCell.setHeader(true);  
    53.         haderCell.setColspan(3);  
    54.         table.addCell(haderCell);  
    55.         table.endHeaders();  
    56.           
    57.         Font fontChinese = new Font(bfChinese,12,Font.NORMAL,Color.GREEN);  
    58.         Cell cell = new Cell(new Paragraph("这是一个3*3测试表格数据",fontChinese));  
    59.         cell.setVerticalAlignment(Element.ALIGN_MIDDLE);  
    60.         table.addCell(cell);  
    61.         table.addCell(new Cell("#1"));  
    62.         table.addCell(new Cell("#2"));  
    63.         table.addCell(new Cell("#3"));  
    64.           
    65.         document.add(table);  
    66.         document.close();  
    67.               
    68.     }  
    69.     public static void main(String[] args) {  
    70.         CreateWordDemo word = new CreateWordDemo();  
    71.         String file = "test.doc";  
    72.         try {  
    73.             word.createDocContext(file, "测试iText导出Word文档");  
    74.         } catch (DocumentException e) {  
    75.             e.printStackTrace();  
    76.         } catch (IOException e) {  
    77.             e.printStackTrace();  
    78.         }  
    79.     }  
    80. }</span>  

    图片版:

    [java] view plain copy
     
      1. <span style="font-size: medium;">/** 
      2.  
      3. * @param args 
      4.  
      5. */  
      6.   
      7. public static void main(String[] args) {  
      8.   
      9. // TODO Auto-generated method stub  
      10.   
      11. try {  
      12.   
      13. RTFCreate rtfMain = new RTFCreate();  
      14.   
      15. rtfMain.createRTFContext(FILE_NAME);  
      16.   
      17.   
      18.   
      19.   
      20. catch (FileNotFoundException e) {  
      21.   
      22. // TODO Auto-generated catch block  
      23.   
      24. e.printStackTrace();  
      25.   
      26. catch (DocumentException e) {  
      27.   
      28. // TODO Auto-generated catch block  
      29.   
      30. e.printStackTrace();  
      31.   
      32. catch (IOException e) {  
      33.   
      34. // TODO Auto-generated catch block  
      35.   
      36. e.printStackTrace();  
      37.   
      38. }  
      39.   
      40. }  
      41.   
      42.   
      43.   
      44.   
      45. public void createRTFContext(String path) throws DocumentException,  
      46.   
      47. IOException {  
      48.   
      49. Document document = new Document(PageSize.A4);  
      50.   
      51. RtfWriter2.getInstance(document, new FileOutputStream(path));  
      52.   
      53. document.open();  
      54.   
      55. // 设置中文字体  
      56.   
      57. BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",  
      58.   
      59. "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);  
      60.   
      61. // 标题字体风格  
      62.   
      63. Font titleFont = new Font(bfChinese, 12, Font.BOLD);  
      64.   
      65.   
      66.   
      67.   
      68. // 正文字体风格  
      69.   
      70. Font contextFont = new Font(bfChinese, 10, Font.NORMAL);  
      71.   
      72.   
      73.   
      74.   
      75. Paragraph title = new Paragraph("标题");  
      76.   
      77. // 设置标题格式对齐方式  
      78.   
      79. title.setAlignment(Element.ALIGN_CENTER);  
      80.   
      81. title.setFont(titleFont);  
      82.   
      83. document.add(title);  
      84.   
      85.   
      86.   
      87.   
      88. String contextString = "iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。使用iText与PDF能够使你正确的控制Servlet的输出。";  
      89.   
      90. Paragraph context = new Paragraph(contextString);  
      91.   
      92. // 正文格式左对齐  
      93.   
      94. context.setAlignment(Element.ALIGN_LEFT);  
      95.   
      96. context.setFont(contextFont);  
      97.   
      98. // 离上一段落(标题)空的行数  
      99.   
      100. context.setSpacingBefore(20);  
      101.   
      102. // 设置第一行空的列数  
      103.   
      104. context.setFirstLineIndent(20);  
      105.   
      106.   
      107.   
      108.   
      109. document.add(context);  
      110.   
      111.   
      112.   
      113.   
      114. // //在表格末尾添加图片  
      115.   
      116. Image png = Image.getInstance("c:/fruit.png");  
      117.   
      118. document.add(png);  
      119.   
      120. document.close();  
      121.   
      122. }  
      123.   
      124.   
      125.   
      126.   
      127. }  
      128.   
      129. </span>  
  • 相关阅读:
    递归函数及Java范例
    笔记本的硬盘坏了
    “References to generic type List should be parameterized”
    配置管理软件(configuration management software)介绍
    WinCE文件目录定制及内存调整
    使用Silverlight for Embedded开发绚丽的界面(3)
    wince国际化语言支持
    Eclipse IDE for Java EE Developers 与Eclipse Classic 区别
    WinCE Heartbeat Message的实现
    使用Silverlight for Embedded开发绚丽的界面(2)
  • 原文地址:https://www.cnblogs.com/telwanggs/p/5357912.html
Copyright © 2011-2022 走看看