Itext 报表Word(纯JAVA)

来源:互联网 发布:淘宝达人任务 编辑:程序博客网 时间:2024/06/03 08:15

以前写过用poi导出报表,最近有这么一个需求,不仅仅是文字数据的导出,还有图片的导出到Word... 发现poi无法导出图片!

查询一番,用iText.jar这个开源框架还不错、完成图片特别简单。支持URL、本地、I/o....

好了不多说了,一个Dome让你"拿来就能用"... 

先上一个效果图片.. (有妹子喔!!吐舌头



接着代码:

package com.li.dome;import java.awt.Color;import java.io.FileOutputStream;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import com.lowagie.text.Cell;import com.lowagie.text.Chunk;import com.lowagie.text.Document;import com.lowagie.text.Element;import com.lowagie.text.Font;import com.lowagie.text.FontFactory;import com.lowagie.text.Image;import com.lowagie.text.PageSize;import com.lowagie.text.Paragraph;import com.lowagie.text.Table;import com.lowagie.text.rtf.RtfWriter2;public class ItextDome {public static void main(String[] args) throws Exception {try {long begin=System.currentTimeMillis();new ItextDome().CreateWorde();System.out.println("耗时:"+(System.currentTimeMillis()-begin));} catch (Exception e) {e.printStackTrace();}}private  void CreateWorde() throws Exception {// 创建word文档,并设置纸张的大小Document document = new Document(PageSize.A4);String  fileName="D:/Word/"+"Word_"+System.currentTimeMillis()+".doc";//建立一个书写器与document对象关联,通过书写器可以将文档写入到输出流中  RtfWriter2.getInstance(document, new FileOutputStream(fileName));document.open();    /** 第一行(标题) */String titleString = "报表信息TextName";Paragraph paragraphTable =  new Paragraph(titleString, FontFactory.getFont(FontFactory.HELVETICA, 20, Font.BOLD, Color.black));// 设置标题格式对其方式paragraphTable.setAlignment(Element.ALIGN_CENTER);document.add(paragraphTable);/** 第二行(时间)*/String contextString =new Date().toLocaleString();//下划线 【 最后一个参数:Font.ITALIC | Font.STRIKETHRU (错误下划线)】Chunk chunk = new Chunk(contextString, FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE));Paragraph paragraphDate=new Paragraph(chunk);// 正文格式对齐方式paragraphDate.setAlignment(Element.ALIGN_RIGHT);document.add(paragraphDate);/** 第三行(正文Tsble)*///获取数据源Map<String, String> map=new ItextDome().getDate();System.out.println("行数:"+map.size());// 表格设置Table table=new Table(2, map.size());int[] withs = { 1, 1 };//设置每列所占比例 table.setWidths(withs);//表格所占页面宽度 table.setWidth(90);//居中显示 table.setAlignment(Element.ALIGN_CENTER);//自动填满table.setAutoFillEmptyCells(true);//边框宽度  table.setBorderWidth(5); //边框颜色table.setBorderColor(Color.WHITE);table.setPadding(30);            Cell cell=null;Iterator it = map.entrySet().iterator();          while (it.hasNext()) {              Map.Entry e = (Map.Entry) it.next();              System.out.println("Key: " + e.getKey() + "; Value: " + e.getValue());             //第一列(Name)          cell=new Cell(e.getKey().toString());          cell.setVerticalAlignment(Element.ALIGN_CENTER);          cell.setHorizontalAlignment(Element.ALIGN_CENTER);          table.addCell(cell);          //第二列(数据)          cell=new Cell(e.getValue().toString());          cell.setVerticalAlignment(Element.ALIGN_CENTER);          cell.setHorizontalAlignment(Element.ALIGN_CENTER);          table.addCell(cell);      }  document.add(table);/** 第四行(图片)*///网络URLImage png = Image.getInstance("http://f.hiphotos.baidu.com/image/w%3D2048/sign=de925e5d014f78f0800b9df34d090b55/29381f30e924b8998c7eb9316c061d950a7bf67d.jpg");//本地路径Image png2 = Image.getInstance("D:/20140117094330.png");      png.setAlignment(Image.ALIGN_CENTER);        document.add(png);document.close();}/** * 数据源 * @return */private Map<String, String>  getDate() {Map<String, String> map=new HashMap<String, String>();for(int i=0;i<6;i++){map.put("测试Name"+i, "个数:"+i);}return map;}}


以上Dome + iText.jar包 :http://download.csdn.net/detail/cs_li1126/6859609

参考资源:

资源一:点击打开链接

资源二:点击打开链接

资源三:点击打开链接

0 0
原创粉丝点击