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这个包对于设置字体什么的起了关键作用上网可以搜到的.
- packagecom.rye.test;
- importjava.awt.Color;
- importjava.io.FileNotFoundException;
- importjava.io.FileOutputStream;
- importjava.io.IOException;
- importcom.lowagie.text.Cell;
- importcom.lowagie.text.Document;
- importcom.lowagie.text.DocumentException;
- importcom.lowagie.text.Font;
- importcom.lowagie.text.PageSize;
- importcom.lowagie.text.Paragraph;
- importcom.lowagie.text.Table;
- importcom.lowagie.text.rtf.RtfWriter2;
- /**
- * 创建word文档 步骤:
- * 1,建立文档
- * 2,创建一个书写器
- * 3,打开文档
- * 4,向文档中写入数据
- * 5,关闭文档
- */
- publicclassWordDemo {
- publicWordDemo() {
- }
- /**
- * @param args
- */
- publicstaticvoidmain(String[] args) {
- // 创建word文档,并设置纸张的大小
- Document document = newDocument(PageSize.A4);
- try{
- RtfWriter2.getInstance(document,
- newFileOutputStream("E:/word.doc"));
- document.open();
- //设置合同头
- Paragraph ph = newParagraph();
- Font f = newFont();
- Paragraph p = newParagraph("出口合同",
- newFont(Font.NORMAL,18, Font.BOLDITALIC,newColor(0,0,0)) );
- p.setAlignment(1);
- document.add(p);
- ph.setFont(f);
- // 设置中文字体
- // BaseFont bfFont =
- // BaseFont.createFont("STSongStd-Light",
- "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
- // Font chinaFont = new Font();
- /*
- * 创建有三列的表格
- */
- Table table = newTable(4);
- document.add(newParagraph("生成表格"));
- table.setBorderWidth(1);
- table.setBorderColor(Color.BLACK);
- table.setPadding(0);
- table.setSpacing(0);
- /*
- * 添加表头的元素
- */
- Cell cell = newCell("表头");//单元格
- cell.setHeader(true);
- cell.setColspan(3);//设置表格为三列
- cell.setRowspan(3);//设置表格为三行
- table.addCell(cell);
- table.endHeaders();// 表头结束
- // 表格的主体
- cell = newCell("Example cell 2");
- cell.setRowspan(2);//当前单元格占两行,纵向跨度
- table.addCell(cell);
- table.addCell("1,1");
- table.addCell("1,2");
- table.addCell("1,3");
- table.addCell("1,4");
- table.addCell("1,5");
- table.addCell(newParagraph("用java生成的表格1"));
- table.addCell(newParagraph("用java生成的表格2"));
- table.addCell(newParagraph("用java生成的表格3"));
- table.addCell(newParagraph("用java生成的表格4"));
- document.add(newParagraph("用java生成word文件"));
- document.add(table);
- document.close();
- } catch(FileNotFoundException e) {
- e.printStackTrace();
- } catch(DocumentException e) {
- e.printStackTrace();
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- }
代码2:
- <span style="">importjava.awt.Color;
- importjava.io.FileNotFoundException;
- importjava.io.FileOutputStream;
- importjava.io.IOException;
- importcom.lowagie.text.Cell;
- importcom.lowagie.text.Document;
- importcom.lowagie.text.DocumentException;
- importcom.lowagie.text.Element;
- importcom.lowagie.text.Font;
- importcom.lowagie.text.PageSize;
- importcom.lowagie.text.Paragraph;
- importcom.lowagie.text.Table;
- importcom.lowagie.text.pdf.BaseFont;
- importcom.lowagie.text.rtf.RtfWriter2;
- publicclassCreateWordDemo {
- publicvoidcreateDocContext(String file,String contextString)throwsDocumentException, IOException{
- //设置纸张大小
- Document document = newDocument(PageSize.A4);
- //建立一个书写器,与document对象关联
- RtfWriter2.getInstance(document, newFileOutputStream(file));
- document.open();
- //设置中文字体
- BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
- //标题字体风格
- Font titleFont = newFont(bfChinese,12,Font.BOLD);
- //正文字体风格
- Font contextFont = newFont(bfChinese,10,Font.NORMAL);
- Paragraph title = newParagraph("标题");
- //设置标题格式对齐方式
- title.setAlignment(Element.ALIGN_CENTER);
- title.setFont(titleFont);
- document.add(title);
- Paragraph context = newParagraph(contextString);
- context.setAlignment(Element.ALIGN_LEFT);
- context.setFont(contextFont);
- //段间距
- context.setSpacingBefore(3);
- //设置第一行空的列数
- context.setFirstLineIndent(20);
- document.add(context);
- //设置Table表格,创建一个三列的表格
- Table table = newTable(3);
- intwidth[] = {25,25,50};//设置每列宽度比例
- table.setWidths(width);
- table.setWidth(90);//占页面宽度比例
- table.setAlignment(Element.ALIGN_CENTER);//居中
- table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
- table.setAutoFillEmptyCells(true);//自动填满
- table.setBorderWidth(1);//边框宽度
- //设置表头
- Cell haderCell = newCell("表格表头");
- haderCell.setHeader(true);
- haderCell.setColspan(3);
- table.addCell(haderCell);
- table.endHeaders();
- Font fontChinese = newFont(bfChinese,12,Font.NORMAL,Color.GREEN);
- Cell cell = newCell(newParagraph("这是一个3*3测试表格数据",fontChinese));
- cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- table.addCell(cell);
- table.addCell(newCell("#1"));
- table.addCell(newCell("#2"));
- table.addCell(newCell("#3"));
- document.add(table);
- document.close();
- }
- publicstaticvoidmain(String[] args) {
- CreateWordDemo word = newCreateWordDemo();
- String file = "test.doc";
- try{
- word.createDocContext(file, "测试iText导出Word文档");
- } catch(DocumentException e) {
- e.printStackTrace();
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- }</span>
图片版:
- <span style="font-size: medium;">/**
- * @param args
- */
- publicstaticvoidmain(String[] args) {
- // TODO Auto-generated method stub
- try{
- RTFCreate rtfMain = newRTFCreate();
- rtfMain.createRTFContext(FILE_NAME);
- } catch(FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch(DocumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch(IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- publicvoidcreateRTFContext(String path)throwsDocumentException,
- IOException {
- Document document = newDocument(PageSize.A4);
- RtfWriter2.getInstance(document, newFileOutputStream(path));
- document.open();
- // 设置中文字体
- BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
- "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
- // 标题字体风格
- Font titleFont = newFont(bfChinese,12, Font.BOLD);
- // 正文字体风格
- Font contextFont = newFont(bfChinese,10, Font.NORMAL);
- Paragraph title = newParagraph("标题");
- // 设置标题格式对齐方式
- title.setAlignment(Element.ALIGN_CENTER);
- title.setFont(titleFont);
- document.add(title);
- String contextString = "iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。使用iText与PDF能够使你正确的控制Servlet的输出。";
- Paragraph context = newParagraph(contextString);
- // 正文格式左对齐
- context.setAlignment(Element.ALIGN_LEFT);
- context.setFont(contextFont);
- // 离上一段落(标题)空的行数
- context.setSpacingBefore(20);
- // 设置第一行空的列数
- context.setFirstLineIndent(20);
- document.add(context);
- // //在表格末尾添加图片
- Image png = Image.getInstance("c:/fruit.png");
- document.add(png);
- document.close();
- }
- }
- </span>
java 生成word表格相关推荐
- java使用POI导出word数据以及生成word表格
暑期在杭州实习了两个月,主要是使用vue+SpringMVC进行一个网页开发. 而在开发的过程中,也遇到了比较常见的文件导出问题–以固定格式将数据存储在word.excel等office文件格式中. ...
- apache poi 修改docx表格_word 模板内容的替换和生成word 表格(使用poi)
1. maven 相关依赖 和模板 org.apache.poi poi-ooxml 3.15-beta2 org.apache.poi ooxml-schemas 1.1 2. 工具类中的方法 / ...
- [摘]用Java生成Word文档
开发中隔三叉五的就要用到Word,经常被搞得不胜其烦,不过这次找到了不少好例子,干脆将他们都摘了过来,内容如下: 1. poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这 ...
- java导出word表格 行列合并
生成word表格 新建模板如下(使用offic,不要用wps) 另存为xm格式 将文件放入resource/template目录下,复制一份,将后缀名改为ftl,也可以不复制直接改后缀名(这里忽视我其 ...
- 用java生成word文档(转载)
用java生成word文档 poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: 下载经过封装后的poi包: 这个包就是:tm-extrac ...
- java生成word几种解决方案
1. Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用 ...
- java生成word和pdf的几种方法的优缺点对比
JAVA生成word优缺点对比 所用技术 优点 缺点 Jacob 功能强大 代码量大,设置样式繁琐:需要windows平台支持,无法跨平台 Apache POI 读写excel功能强大.操作简单 一般 ...
- 记录一次用Java生成word文档的经验
业务背景 最近接到一个需求需要将学员的基本信息生成word格式的内容,word的格式如下图所示 开发的任务就是将学员的信息替换掉表格中的** 即可,感觉还蛮简单的. 相信大家以前做的最多的是Java和 ...
- Java生成word通报(使用echart、poi-tl、PhantomJS)
Java生成word通报(使用echart.poi-tl.PhantomJS) 前段时间客户需要系统自动生成服务通报,word文档中要有图片.表格.文字.第一次做这种通报,项目经理又想用以前的方式,只 ...
最新文章
- 2022-2028年中国半导体用环氧塑封料(EMC)行业市场全景调查及投资前景分析报告
- C# 编码约定(C# 编程指南)
- 用BusyBox制作Linux根文件系统
- Gradle的好处:运行单个测试
- 混合使用Objective-C,C++和Objective-C++
- 题解-bzoj3901 棋盘游戏
- iOS开发那些-如何打包iOS应用程序
- C++工业设备日志记录系统
- linux 进程 cpu 100,清理linux中占用CPU 100%的病毒
- (android开发http拦截)fiddler2抓包工具
- 树莓派 Ubuntu 18.04 启动2.4Ghz或5Ghz热点及部分5G信道启动失败解决方法
- 云服务器win10系统搭建ftp,win10系统搭建ftp服务器
- 不会编程,别着急!免编程工具助你快速开发App
- 129、易燃气体的分级
- google 趣事面试题
- python生成简单名片二维码(不带图片)
- 网络加速_蓝汛安全CDN加速解决方案,维稳网络安全
- PRML读书会第七章 Sparse Kernel Machines(支持向量机, support vector machine ,KKT条件,RVM)...
- 使用 vs 查看dll 方法
- concurrent