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. packagecom.rye.test;
  2. importjava.awt.Color;
  3. importjava.io.FileNotFoundException;
  4. importjava.io.FileOutputStream;
  5. importjava.io.IOException;
  6. importcom.lowagie.text.Cell;
  7. importcom.lowagie.text.Document;
  8. importcom.lowagie.text.DocumentException;
  9. importcom.lowagie.text.Font;
  10. importcom.lowagie.text.PageSize;
  11. importcom.lowagie.text.Paragraph;
  12. importcom.lowagie.text.Table;
  13. importcom.lowagie.text.rtf.RtfWriter2;
  14. /**
  15. * 创建word文档 步骤:
  16. * 1,建立文档
  17. * 2,创建一个书写器
  18. * 3,打开文档
  19. * 4,向文档中写入数据
  20. * 5,关闭文档
  21. */
  22. publicclassWordDemo {
  23. publicWordDemo() {
  24. }
  25. /**
  26. * @param args
  27. */
  28. publicstaticvoidmain(String[] args) {
  29. // 创建word文档,并设置纸张的大小
  30. Document document = newDocument(PageSize.A4);
  31. try{
  32. RtfWriter2.getInstance(document,
  33. newFileOutputStream("E:/word.doc"));
  34. document.open();
  35. //设置合同头
  36. Paragraph ph = newParagraph();
  37. Font f  = newFont();
  38. Paragraph p = newParagraph("出口合同",
  39. newFont(Font.NORMAL,18, Font.BOLDITALIC,newColor(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 = newTable(4);
  52. document.add(newParagraph("生成表格"));
  53. table.setBorderWidth(1);
  54. table.setBorderColor(Color.BLACK);
  55. table.setPadding(0);
  56. table.setSpacing(0);
  57. /*
  58. * 添加表头的元素
  59. */
  60. Cell cell = newCell("表头");//单元格
  61. cell.setHeader(true);
  62. cell.setColspan(3);//设置表格为三列
  63. cell.setRowspan(3);//设置表格为三行
  64. table.addCell(cell);
  65. table.endHeaders();// 表头结束
  66. // 表格的主体
  67. cell = newCell("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(newParagraph("用java生成的表格1"));
  76. table.addCell(newParagraph("用java生成的表格2"));
  77. table.addCell(newParagraph("用java生成的表格3"));
  78. table.addCell(newParagraph("用java生成的表格4"));
  79. document.add(newParagraph("用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="">importjava.awt.Color;
  2. importjava.io.FileNotFoundException;
  3. importjava.io.FileOutputStream;
  4. importjava.io.IOException;
  5. importcom.lowagie.text.Cell;
  6. importcom.lowagie.text.Document;
  7. importcom.lowagie.text.DocumentException;
  8. importcom.lowagie.text.Element;
  9. importcom.lowagie.text.Font;
  10. importcom.lowagie.text.PageSize;
  11. importcom.lowagie.text.Paragraph;
  12. importcom.lowagie.text.Table;
  13. importcom.lowagie.text.pdf.BaseFont;
  14. importcom.lowagie.text.rtf.RtfWriter2;
  15. publicclassCreateWordDemo {
  16. publicvoidcreateDocContext(String file,String contextString)throwsDocumentException, IOException{
  17. //设置纸张大小
  18. Document document = newDocument(PageSize.A4);
  19. //建立一个书写器,与document对象关联
  20. RtfWriter2.getInstance(document, newFileOutputStream(file));
  21. document.open();
  22. //设置中文字体
  23. BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
  24. //标题字体风格
  25. Font titleFont = newFont(bfChinese,12,Font.BOLD);
  26. //正文字体风格
  27. Font contextFont = newFont(bfChinese,10,Font.NORMAL);
  28. Paragraph title = newParagraph("标题");
  29. //设置标题格式对齐方式
  30. title.setAlignment(Element.ALIGN_CENTER);
  31. title.setFont(titleFont);
  32. document.add(title);
  33. Paragraph context = newParagraph(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 = newTable(3);
  43. intwidth[] = {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 = newCell("表格表头");
  52. haderCell.setHeader(true);
  53. haderCell.setColspan(3);
  54. table.addCell(haderCell);
  55. table.endHeaders();
  56. Font fontChinese = newFont(bfChinese,12,Font.NORMAL,Color.GREEN);
  57. Cell cell = newCell(newParagraph("这是一个3*3测试表格数据",fontChinese));
  58. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  59. table.addCell(cell);
  60. table.addCell(newCell("#1"));
  61. table.addCell(newCell("#2"));
  62. table.addCell(newCell("#3"));
  63. document.add(table);
  64. document.close();
  65. }
  66. publicstaticvoidmain(String[] args) {
  67. CreateWordDemo word = newCreateWordDemo();
  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. publicstaticvoidmain(String[] args) {
  5. // TODO Auto-generated method stub
  6. try{
  7. RTFCreate rtfMain = newRTFCreate();
  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. publicvoidcreateRTFContext(String path)throwsDocumentException,
  21. IOException {
  22. Document document = newDocument(PageSize.A4);
  23. RtfWriter2.getInstance(document, newFileOutputStream(path));
  24. document.open();
  25. // 设置中文字体
  26. BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
  27. "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  28. // 标题字体风格
  29. Font titleFont = newFont(bfChinese,12, Font.BOLD);
  30. // 正文字体风格
  31. Font contextFont = newFont(bfChinese,10, Font.NORMAL);
  32. Paragraph title = newParagraph("标题");
  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 = newParagraph(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>

java 生成word表格相关推荐

  1. java使用POI导出word数据以及生成word表格

    暑期在杭州实习了两个月,主要是使用vue+SpringMVC进行一个网页开发. 而在开发的过程中,也遇到了比较常见的文件导出问题–以固定格式将数据存储在word.excel等office文件格式中. ...

  2. apache poi 修改docx表格_word 模板内容的替换和生成word 表格(使用poi)

    1. maven 相关依赖 和模板 org.apache.poi poi-ooxml 3.15-beta2 org.apache.poi ooxml-schemas 1.1 2.  工具类中的方法 / ...

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

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

  4. java导出word表格 行列合并

    生成word表格 新建模板如下(使用offic,不要用wps) 另存为xm格式 将文件放入resource/template目录下,复制一份,将后缀名改为ftl,也可以不复制直接改后缀名(这里忽视我其 ...

  5. 用java生成word文档(转载)

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

  6. java生成word几种解决方案

    1. Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用 ...

  7. java生成word和pdf的几种方法的优缺点对比

    JAVA生成word优缺点对比 所用技术 优点 缺点 Jacob 功能强大 代码量大,设置样式繁琐:需要windows平台支持,无法跨平台 Apache POI 读写excel功能强大.操作简单 一般 ...

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

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

  9. Java生成word通报(使用echart、poi-tl、PhantomJS)

    Java生成word通报(使用echart.poi-tl.PhantomJS) 前段时间客户需要系统自动生成服务通报,word文档中要有图片.表格.文字.第一次做这种通报,项目经理又想用以前的方式,只 ...

最新文章

  1. 2022-2028年中国半导体用环氧塑封料(EMC)行业市场全景调查及投资前景分析报告
  2. C# 编码约定(C# 编程指南)
  3. 用BusyBox制作Linux根文件系统
  4. Gradle的好处:运行单个测试
  5. 混合使用Objective-C,C++和Objective-C++
  6. 题解-bzoj3901 棋盘游戏
  7. iOS开发那些-如何打包iOS应用程序
  8. C++工业设备日志记录系统
  9. linux 进程 cpu 100,清理linux中占用CPU 100%的病毒
  10. (android开发http拦截)fiddler2抓包工具
  11. 树莓派 Ubuntu 18.04 启动2.4Ghz或5Ghz热点及部分5G信道启动失败解决方法
  12. 云服务器win10系统搭建ftp,win10系统搭建ftp服务器
  13. 不会编程,别着急!免编程工具助你快速开发App
  14. 129、易燃气体的分级
  15. google 趣事面试题
  16. python生成简单名片二维码(不带图片)
  17. 网络加速_蓝汛安全CDN加速解决方案,维稳网络安全
  18. PRML读书会第七章 Sparse Kernel Machines(支持向量机, support vector machine ,KKT条件,RVM)...
  19. 使用 vs 查看dll 方法
  20. concurrent

热门文章

  1. 计算机上的时钟发生装置教程,计算机的时钟发生装置叫做什么?
  2. 【linux中Telnet服务的安装】
  3. 敲七 STL队列(c++)
  4. 「BZOJ3864」Hero meet devil 题解
  5. 华南理工大学软件文化节“三七互娱杯”程序设计竞赛
  6. java自动生成物流单号
  7. mysql的my.ini常用配置
  8. LeetCode 316. Remove Duplicate Letters
  9. Java-Stream流,异常捕获
  10. 关闭Hyper-v虚拟服务