`
zjx2388
  • 浏览: 1305775 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

非poi实现word中导入pic图片代码示例

阅读更多

根据项目需求,客户想生成一个带统计表格、图片和说明文字的word文件,今天整了很久,终于弄好了,下面给出代码,原本想用POI,但找了很久只实现了excel中添加图片的处理,最后不得不使用的是iText2.1.7实现的,最新版不一定好使,附件提供相关jar包

 

package com.test;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
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 WordDemo {

	public WordDemo() {
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		WordDemo.createDoc();
	}

	/**
	 * 创建word文档 步骤: 1,建立文档 2,创建一个书写器 3,打开文档 4,向文档中写入数据(表格,图片) 5,关闭文档
	 */
	public static void createDoc() {

		// 创建word文档,并设置纸张的大小
		Document document = new Document(PageSize.A4);
		try {

			RtfWriter2.getInstance(document,
					new FileOutputStream("E:/word.doc"));

			document.open();

			// 设置合同头
			Paragraph ph = new Paragraph();
			Font f = new Font();

			Paragraph p = new Paragraph("出口合同", new Font(Font.NORMAL, 18,Font.BOLDITALIC, new Color(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 = new Table(4);
			document.add(new Paragraph("生成表格"));
			table.setBorderWidth(1);
			table.setBorderColor(Color.BLACK);
			table.setPadding(0);
			table.setSpacing(0);

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

			// 表格的主体
			cell = new Cell("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(new Paragraph("用java生成的表格1"));
			table.addCell(new Paragraph("用java生成的表格2"));
			table.addCell(new Paragraph("用java生成的表格3"));
			table.addCell(new Paragraph("用java生成的表格4"));
			document.add(new Paragraph("用java生成word文件"));
			document.add(table);

			/* 测试添加一张图片 */
			Image img = Image.getInstance("E:/ok.png");
			document.add(img);

			/* 测试再添加一张图片 */
			Image img2 = Image.getInstance("E:/ok.png");
			document.add(img2);

			document.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics