5 个解决方案
#1
重新解析写入word
#2
从数据库读出来的是html数据?这数据咋这样呢?
用itext是可以生成word的
我正好这几天在做这个,简单帖的例子:
用itext是可以生成word的
我正好这几天在做这个,简单帖的例子:
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.style.RtfFont;
public class CreateWord_Company {
public static void main(String[] args) throws Exception {
/** 创建Document对象(word文档) author:yyli Sep 15, 2010 */
Rectangle rectPageSize = new Rectangle(PageSize.A4);
rectPageSize = rectPageSize.rotate();
// 创建word文档,并设置纸张的大小
Document doc = new Document(PageSize.A4);
String fileName="E:/企业详细信息登记表_"+System.currentTimeMillis()+".doc";
/** 建立一个书写器与document对象关联,通过书写器可以将文档写入到输出流中 author:yyli Sep 15, 2010 */
RtfWriter2.getInstance(doc, new FileOutputStream(new File(fileName)));
doc.open();
/** 标题字体 author:yyli Sep 15, 2010 */
RtfFont titleFont = new RtfFont("仿宋_GB2312", 15, Font.BOLD,
Color.BLACK);
/** 正文字体 author:yyli Sep 15, 2010 */
RtfFont contextFont = new RtfFont("仿宋_GB2312", 9, Font.NORMAL,
Color.BLACK);
/** 表格设置 author:yyli Sep 15, 2010 */
Table table = new Table(4, 7);
int[] withs = { 15, 35, 15, 35};
/** 设置每列所占比例 author:yyli Sep 15, 2010 */
table.setWidths(withs);
/** 表格所占页面宽度 author:yyli Sep 15, 2010 */
table.setWidth(100);
/** 居中显示 author:yyli Sep 15, 2010 */
table.setAlignment(Element.ALIGN_CENTER);
/** 自动填满 author:yyli Sep 15, 2010 */
table.setAutoFillEmptyCells(true);
table.setBorderWidth(5); // 边框宽度
table.setBorderColor(new Color(0, 125, 255)); // 边框颜色
table.setPadding(12);// 衬距,看效果就知道什么意思了
table.setSpacing(0);// 即单元格之间的间距
table.setBorder(5);// 边框
/** 第一行(标题) author:yyli Sep 15, 2010 */
String titleString = "企业详细信息登记表";
Paragraph title = new Paragraph(titleString);
// 设置标题格式对其方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
doc.add(title);
/** 第二行(正文) author:yyli Sep 15, 2010 */
String contextString = "登记人:"+"admin "+"登记时间:"+new Date().toLocaleString();
Paragraph context = new Paragraph(contextString);
// 正文格式对齐方式
context.setAlignment(Element.ALIGN_RIGHT);
context.setFont(contextFont);
// 与上一段落(标题)的行距
context.setSpacingBefore(10);
// 设置第一行空的列数(缩进)
// context.setFirstLineIndent(20);
doc.add(context);
Cell cell=null;
cell=new Cell("企业名称:");
cell.setHeader(true);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("中国安芯");
cell.setColspan(3);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("企业地址:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("淮安市洪泽县");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("企业代码:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("zxc797ddd797979");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("联系电话:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("0512-78945612");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("传真:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("5689741");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("经济类型:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("国有全资");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("行业管理部门:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("123");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("危险等级:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("2");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("是否监控:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("是");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("成立时间:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell(new Date().toLocaleString());
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("职工总数:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("52");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("固定资产总值:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("1200万");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("占地面积:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("52万平方米");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
doc.add(table);
doc.close();
}
}
#3
用 itext 下的htmlWorker 得到的是个pdfptable. 而word 中 需要一个table形式的. 怎么转?
#4
请贴出你的代码,你说的太专业了。。。还是来点直接的
#5
你这个jar包是什么版本的,我用的版本有两句话报错
#1
重新解析写入word
#2
从数据库读出来的是html数据?这数据咋这样呢?
用itext是可以生成word的
我正好这几天在做这个,简单帖的例子:
用itext是可以生成word的
我正好这几天在做这个,简单帖的例子:
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.style.RtfFont;
public class CreateWord_Company {
public static void main(String[] args) throws Exception {
/** 创建Document对象(word文档) author:yyli Sep 15, 2010 */
Rectangle rectPageSize = new Rectangle(PageSize.A4);
rectPageSize = rectPageSize.rotate();
// 创建word文档,并设置纸张的大小
Document doc = new Document(PageSize.A4);
String fileName="E:/企业详细信息登记表_"+System.currentTimeMillis()+".doc";
/** 建立一个书写器与document对象关联,通过书写器可以将文档写入到输出流中 author:yyli Sep 15, 2010 */
RtfWriter2.getInstance(doc, new FileOutputStream(new File(fileName)));
doc.open();
/** 标题字体 author:yyli Sep 15, 2010 */
RtfFont titleFont = new RtfFont("仿宋_GB2312", 15, Font.BOLD,
Color.BLACK);
/** 正文字体 author:yyli Sep 15, 2010 */
RtfFont contextFont = new RtfFont("仿宋_GB2312", 9, Font.NORMAL,
Color.BLACK);
/** 表格设置 author:yyli Sep 15, 2010 */
Table table = new Table(4, 7);
int[] withs = { 15, 35, 15, 35};
/** 设置每列所占比例 author:yyli Sep 15, 2010 */
table.setWidths(withs);
/** 表格所占页面宽度 author:yyli Sep 15, 2010 */
table.setWidth(100);
/** 居中显示 author:yyli Sep 15, 2010 */
table.setAlignment(Element.ALIGN_CENTER);
/** 自动填满 author:yyli Sep 15, 2010 */
table.setAutoFillEmptyCells(true);
table.setBorderWidth(5); // 边框宽度
table.setBorderColor(new Color(0, 125, 255)); // 边框颜色
table.setPadding(12);// 衬距,看效果就知道什么意思了
table.setSpacing(0);// 即单元格之间的间距
table.setBorder(5);// 边框
/** 第一行(标题) author:yyli Sep 15, 2010 */
String titleString = "企业详细信息登记表";
Paragraph title = new Paragraph(titleString);
// 设置标题格式对其方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
doc.add(title);
/** 第二行(正文) author:yyli Sep 15, 2010 */
String contextString = "登记人:"+"admin "+"登记时间:"+new Date().toLocaleString();
Paragraph context = new Paragraph(contextString);
// 正文格式对齐方式
context.setAlignment(Element.ALIGN_RIGHT);
context.setFont(contextFont);
// 与上一段落(标题)的行距
context.setSpacingBefore(10);
// 设置第一行空的列数(缩进)
// context.setFirstLineIndent(20);
doc.add(context);
Cell cell=null;
cell=new Cell("企业名称:");
cell.setHeader(true);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("中国安芯");
cell.setColspan(3);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("企业地址:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("淮安市洪泽县");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("企业代码:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("zxc797ddd797979");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("联系电话:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("0512-78945612");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("传真:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("5689741");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("经济类型:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("国有全资");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("行业管理部门:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("123");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("危险等级:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("2");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("是否监控:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("是");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("成立时间:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell(new Date().toLocaleString());
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("职工总数:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("52");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("固定资产总值:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("1200万");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell=new Cell("占地面积:");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell=new Cell("52万平方米");
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
doc.add(table);
doc.close();
}
}
#3
用 itext 下的htmlWorker 得到的是个pdfptable. 而word 中 需要一个table形式的. 怎么转?
#4
请贴出你的代码,你说的太专业了。。。还是来点直接的
#5
你这个jar包是什么版本的,我用的版本有两句话报错