java 生成word表格

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

一种是jacob 但是局限于windows平台 往往许多JAVA程序运行于其他操作系统 在此不讨论该方案。(需要下载jacob.jar以及jacob.dll)

一种是poi,他对excel处理的很好(读和写),poi对word的操作,基本上处在读word模板阶段,对于写word文件就更弱项了,特别是word上画表格等复杂操作。

还有就是itext,用它生成rtf文件并保存格式为word ;(itext主要是用来生成pdf的文档)


iText-1.2.7.jar和支持rtf的iText-rtf-2.1.7.jar这两个貌似对了,其实还有一个包是比较重要的iTextAsian.jar这个包对于设置字体什么的起了关键作用上网可以搜到的.


  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(000)) );    
  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>  

  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值