0
点赞
收藏
分享

微信扫一扫

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. import com.lowagie.text.Cell;
  7. import com.lowagie.text.Document;
  8. import com.lowagie.text.DocumentException;
  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.rtf.RtfWriter2;
  14. /**
  15. * 创建word文档 步骤:
  16. * 1,建立文档
  17. * 2,创建一个书写器
  18. * 3,打开文档
  19. * 4,向文档中写入数据
  20. * 5,关闭文档
  21. */
  22. public class WordDemo {

  23. public WordDemo() {
  24. }

  25. /**
  26. * @param args
  27. */
  28. public static void main(String[] args) {
  29. // 创建word文档,并设置纸张的大小
  30. Document document = new Document(PageSize.A4);
  31. try {
  32. RtfWriter2.getInstance(document,
  33. new FileOutputStream("E:/word.doc"));

  34. document.open();

  35. //设置合同头

  36. Paragraph ph = new Paragraph();
  37. Font f  = new Font();

  38. Paragraph p = new Paragraph("出口合同",
  39. new Font(Font.NORMAL, 18, Font.BOLDITALIC, new Color(0, 0, 0)) );
  40. p.setAlignment(1);
  41. document.add(p);
  42. ph.setFont(f);

  43. // 设置中文字体
  44. // BaseFont bfFont =
  45. // BaseFont.createFont("STSongStd-Light",
  46. "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
  47. // Font chinaFont = new Font();
  48. /*
  49. * 创建有三列的表格
  50. */
  51. Table table = new Table(4);
  52. document.add(new Paragraph("生成表格"));
  53. table.setBorderWidth(1);
  54. table.setBorderColor(Color.BLACK);
  55. table.setPadding(0);
  56. table.setSpacing(0);

  57. /*
  58. * 添加表头的元素
  59. */
  60. Cell cell = new Cell("表头");//单元格
  61. cell.setHeader(true);
  62. cell.setColspan(3);//设置表格为三列
  63. cell.setRowspan(3);//设置表格为三行
  64. table.addCell(cell);
  65. table.endHeaders();// 表头结束

  66. // 表格的主体
  67. cell = new Cell("Example cell 2");
  68. cell.setRowspan(2);//当前单元格占两行,纵向跨度
  69. table.addCell(cell);
  70. table.addCell("1,1");
  71. table.addCell("1,2");
  72. table.addCell("1,3");
  73. table.addCell("1,4");
  74. table.addCell("1,5");
  75. table.addCell(new Paragraph("用java生成的表格1"));
  76. table.addCell(new Paragraph("用java生成的表格2"));
  77. table.addCell(new Paragraph("用java生成的表格3"));
  78. table.addCell(new Paragraph("用java生成的表格4"));
  79. document.add(new Paragraph("用java生成word文件"));
  80. document.add(table);
  81. document.close();
  82. } catch (FileNotFoundException e) {
  83. e.printStackTrace();
  84. } catch (DocumentException e) {
  85. e.printStackTrace();
  86. } catch (IOException e) {
  87. e.printStackTrace();
  88. }
  89. }

  90. }




代码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. Font fontChinese = new Font(bfChinese,12,Font.NORMAL,Color.GREEN);
  57. Cell cell = new Cell(new Paragraph("这是一个3*3测试表格数据",fontChinese));
  58. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  59. table.addCell(cell);
  60. table.addCell(new Cell("#1"));
  61. table.addCell(new Cell("#2"));
  62. table.addCell(new Cell("#3"));

  63. document.add(table);
  64. document.close();

  65. }
  66. public static void main(String[] args) {
  67. CreateWordDemo word = new CreateWordDemo();
  68. String file = "test.doc";
  69. try {
  70. word.createDocContext(file, "测试iText导出Word文档");
  71. } catch (DocumentException e) {
  72. e.printStackTrace();
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. }</span>


图片版:


[java] 

​​view plain​​

​​copy​​


  1. <span style="font-size: medium;">/**

  2. * @param args

  3. */

  4. public static void main(String[] args) {

  5. // TODO Auto-generated method stub

  6. try {

  7. RTFCreate rtfMain = new RTFCreate();

  8. rtfMain.createRTFContext(FILE_NAME);




  9. } catch (FileNotFoundException e) {

  10. // TODO Auto-generated catch block

  11. e.printStackTrace();

  12. } catch (DocumentException e) {

  13. // TODO Auto-generated catch block

  14. e.printStackTrace();

  15. } catch (IOException e) {

  16. // TODO Auto-generated catch block

  17. e.printStackTrace();

  18. }

  19. }




  20. public void createRTFContext(String path) throws DocumentException,

  21. IOException {

  22. Document document = new Document(PageSize.A4);

  23. RtfWriter2.getInstance(document, new FileOutputStream(path));

  24. document.open();

  25. // 设置中文字体

  26. BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",

  27. "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

  28. // 标题字体风格

  29. Font titleFont = new Font(bfChinese, 12, Font.BOLD);




  30. // 正文字体风格

  31. Font contextFont = new Font(bfChinese, 10, Font.NORMAL);




  32. Paragraph title = new Paragraph("标题");

  33. // 设置标题格式对齐方式

  34. title.setAlignment(Element.ALIGN_CENTER);

  35. title.setFont(titleFont);

  36. document.add(title);




  37. String contextString = "iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。使用iText与PDF能够使你正确的控制Servlet的输出。";

  38. Paragraph context = new Paragraph(contextString);

  39. // 正文格式左对齐

  40. context.setAlignment(Element.ALIGN_LEFT);

  41. context.setFont(contextFont);

  42. // 离上一段落(标题)空的行数

  43. context.setSpacingBefore(20);

  44. // 设置第一行空的列数

  45. context.setFirstLineIndent(20);




  46. document.add(context);




  47. // //在表格末尾添加图片

  48. Image png = Image.getInstance("c:/fruit.png");

  49. document.add(png);

  50. document.close();

  51. }




  52. }

  53. </span>



举报

相关推荐

0 条评论