JAVA生成WORD文件的方法目前有以下種:
一種是jacob 但是局限於windows平台 往往許多JAVA程序運行於其他操作系統 在此不討論該方案
一種是pio但是他的excel處理很程序 word模塊還局限於讀取word的文本內容,寫word文件就更弱項了
當我使用這個JAVA生成RTF文件時費了好大的勁,原本是想生成WORD文檔的,但是WORD文檔POI只支持往生成的WORD中填入文本,對於圖片根本就不支持。后來想想,RTF格式的也可用WORD打開,不如生成RTF。結果上網搜了很多技術,Itext,jacob,java2word,rtftemplate,reportrunner看了近一天也沒什么頭緒,寫這些示例的幾乎沒有,還好Itext的例子有那么幾個,可是我上官網下了最新核心包后發現,導入例子中居然全部紅叉,原本以為上錯網站了,再經過核實還是對的,於是斷定網上的例子肯定有誤,itext或許不能用。繞了大半天其他的技術我真的沒法弄了,還是回到了iText,靜下心來思考覺得包肯定有問題,仔細一看原來最新版的是支持PDF版的iText-5.0.1.jar是不對的,本來以為最新的功能最全了,沒想到錯了,想到這里趕緊下了稍微iText-2.1.7.jar結果終於成功了,感慨不已!現貢獻代碼如下記住官網上只能下到核心包:iText-1.2.7.jar和支持rtf的iText-rtf-2.1.7.jar這兩個貌似對了,其實還有一個包是比較重要的iTextAsian.jar這個包對於設置字體什么的起了關鍵作用上網可以搜到的.
本文介紹的是itext生成rtf文件並保存格式為word 此方案本人已實踐過 並已在項目中使用
用到的jar包:
iText-2.1.7.jar
iText-rtf-2.1.7.jar
iTextAsian.jar
- package com.rye.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.PageSize;
- import com.lowagie.text.Paragraph;
- import com.lowagie.text.Table;
- import com.lowagie.text.rtf.RtfWriter2;
- /**
- * 創建word文檔 步驟:
- * 1,建立文檔
- * 2,創建一個書寫器
- * 3,打開文檔
- * 4,向文檔中寫入數據
- * 5,關閉文檔
- */
- public class WordDemo {
- public WordDemo() {
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- // 創建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);
- document.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (DocumentException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
代碼2:
- <span style="">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.Element;
- import com.lowagie.text.Font;
- import com.lowagie.text.PageSize;
- import com.lowagie.text.Paragraph;
- import com.lowagie.text.Table;
- import com.lowagie.text.pdf.BaseFont;
- import com.lowagie.text.rtf.RtfWriter2;
- public class CreateWordDemo {
- public void createDocContext(String file,String contextString)throws DocumentException, IOException{
- //設置紙張大小
- Document document = new Document(PageSize.A4);
- //建立一個書寫器,與document對象關聯
- RtfWriter2.getInstance(document, new FileOutputStream(file));
- document.open();
- //設置中文字體
- BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
- //標題字體風格
- Font titleFont = new Font(bfChinese,12,Font.BOLD);
- //正文字體風格
- Font contextFont = new Font(bfChinese,10,Font.NORMAL);
- Paragraph title = new Paragraph("標題");
- //設置標題格式對齊方式
- title.setAlignment(Element.ALIGN_CENTER);
- title.setFont(titleFont);
- document.add(title);
- Paragraph context = new Paragraph(contextString);
- context.setAlignment(Element.ALIGN_LEFT);
- context.setFont(contextFont);
- //段間距
- context.setSpacingBefore(3);
- //設置第一行空的列數
- context.setFirstLineIndent(20);
- document.add(context);
- //設置Table表格,創建一個三列的表格
- Table table = new Table(3);
- int width[] = {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 = new Cell("表格表頭");
- haderCell.setHeader(true);
- haderCell.setColspan(3);
- table.addCell(haderCell);
- table.endHeaders();
- Font fontChinese = new Font(bfChinese,12,Font.NORMAL,Color.GREEN);
- Cell cell = new Cell(new Paragraph("這是一個3*3測試表格數據",fontChinese));
- cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
- table.addCell(cell);
- table.addCell(new Cell("#1"));
- table.addCell(new Cell("#2"));
- table.addCell(new Cell("#3"));
- document.add(table);
- document.close();
- }
- public static void main(String[] args) {
- CreateWordDemo word = new CreateWordDemo();
- 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
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- try {
- RTFCreate rtfMain = new RTFCreate();
- 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();
- }
- }
- public void createRTFContext(String path) throws DocumentException,
- IOException {
- Document document = new Document(PageSize.A4);
- RtfWriter2.getInstance(document, new FileOutputStream(path));
- document.open();
- // 設置中文字體
- BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
- "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
- // 標題字體風格
- Font titleFont = new Font(bfChinese, 12, Font.BOLD);
- // 正文字體風格
- Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
- Paragraph title = new Paragraph("標題");
- // 設置標題格式對齊方式
- 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 = new Paragraph(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>