iText導出word入門


最近一個項目需要導出word文檔,網上了解了一下,發現poi導出excel比較好,但是導出word就力不從心了,大家都比較推薦iText,於是試了試,感覺還不錯。

我用的是2.1.7版本。

1. 使用maven導入依賴

    

<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext-rtf</artifactId>
<version>2.1.7</version>
</dependency>

還有一個包是itextasian-1.5.2.jar, 這是在各個maven庫沒有找到,而我因為一些原因不方便建自己的maven庫,所以只好直接在項目里面添加依賴。


2. 貼上完整的代碼

   

package com.sofree.web.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
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.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
import com.sofree.business.model.TaKyxmApplyperson;
import com.sofree.business.model.TaKyxmApplyteacher;
import com.sofree.business.model.TaKyxmProjectinfo;

/**
* @author Jack
*
*/
public class ExportWordUtil {

public static ByteArrayOutputStream exportProjectWord(TaKyxmProjectinfo obj) {
try {
// 設置紙張大小
Document document = new Document(PageSize.A4);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
// 建立一個書寫器,與document對象關聯
RtfWriter2.getInstance(document, buffer);
document.open();
// 設置中文字體
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 正文字體風格
Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
document.add(getTitle(bfChinese));
document.add(getProjectName(contextFont, obj));
// 設置Table表格,創建一個六列的表格
Table table = new Table(6);
int width[] = { 5, 10, 15, 25, 15, 30 };// 設置每列寬度比例
table.setWidths(width);
table.setWidth(95);// 占頁面寬度比例
table.setAlignment(Element.ALIGN_CENTER);// 居中
//table.setAlignment(Element.ALIGN_MIDDLE);// 垂直居中
table.setAutoFillEmptyCells(true);// 自動填滿
table.setBorderWidth(1);// 邊框寬度
table.setPadding(5);
// 設置表頭
/*Cell haderCell = new Cell("表格表頭");
haderCell.setHeader(true);
haderCell.setColspan(4);
table.addCell(haderCell);
table.endHeaders();*/

Font fontChinese = new Font(bfChinese, 10, Font.NORMAL);
table.addCell(getCell("項目名稱", 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell(obj.getXmmc(), 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell("項目負責人", fontChinese));
table.addCell(getCell(obj.getOwner().getYhm(),fontChinese));
table.addCell(getCell("申請經費", 1, 2, Element.ALIGN_MIDDLE,fontChinese));
table.addCell(getCell(String.valueOf(obj.getSqjf()), 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell("依托實驗室名稱", fontChinese));
table.addCell(getCell(obj.getLab().getSysmc(), fontChinese));

List<TaKyxmApplyperson> stuList = obj.getStudentList();
table.addCell(getCell("團隊成員", stuList.size() + 1, 1, Element.ALIGN_CENTER, fontChinese));
table.addCell(getCell("姓名", fontChinese));
table.addCell(getCell("學號", fontChinese));
table.addCell(getCell("所學專業", fontChinese));
table.addCell(getCell("聯系電話", fontChinese));
table.addCell(getCell("E-mail", fontChinese));
for(TaKyxmApplyperson stu : stuList){
table.addCell(getCell(stu.getXm(), fontChinese));
table.addCell(getCell(stu.getXh(), fontChinese));
table.addCell(getCell(stu.getSxzy(), fontChinese));
table.addCell(getCell(stu.getLxdh(), fontChinese));
table.addCell(getCell(stu.getEmail(), fontChinese));
}

List<TaKyxmApplyteacher> teaList = obj.getTeacherList();
table.addCell(getCell("指導教師", teaList.size() + 1, 1, Element.ALIGN_CENTER, fontChinese));
table.addCell(getCell("姓名", fontChinese));
table.addCell(getCell("單位", fontChinese));
table.addCell(getCell("職務/職稱", fontChinese));
table.addCell(getCell("聯系電話", fontChinese));
table.addCell(getCell("E-mail", fontChinese));
for(TaKyxmApplyteacher tea : teaList){
table.addCell(getCell(tea.getXm(), fontChinese));
table.addCell(getCell(tea.getDw(), fontChinese));
table.addCell(getCell(tea.getZw(), fontChinese));
table.addCell(getCell(tea.getLxdh(), fontChinese));
table.addCell(getCell(tea.getEmail(), fontChinese));
}

table.addCell(getCell("立項的意義及預期達到的效果", 1, 2, Element.ALIGN_MIDDLE,fontChinese));
Paragraph lxyyxg = new Paragraph(obj.getLxyyxg(), fontChinese);
lxyyxg.setAlignment(Element.ALIGN_LEFT);
lxyyxg.setFirstLineIndent(20);
lxyyxg.add(Chunk.NEWLINE);
lxyyxg.add(Chunk.NEWLINE);
table.addCell(getCell(lxyyxg, 1, 4, Element.ALIGN_MIDDLE));

Paragraph xmzynr = new Paragraph(obj.getXmzynr(), fontChinese);
xmzynr.setAlignment(Element.ALIGN_LEFT);
xmzynr.setFirstLineIndent(20);
xmzynr.add(Chunk.NEWLINE);
xmzynr.add(Chunk.NEWLINE);
table.addCell(getCell("項目的主要內容、實驗方案設計及創新點", 1, 2, Element.ALIGN_MIDDLE,fontChinese));
table.addCell(getCell(xmzynr, 1, 4, Element.ALIGN_MIDDLE));

table.addCell(getCell("經費具體預算(元)", 7, 2, Element.ALIGN_MIDDLE,fontChinese));
table.addCell(getCell("科目", 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell("預算明細", 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell("實驗耗材", 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell("", 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell("實驗小工具", 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell("", 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell("加工費", 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell("", 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell("機時費", 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell("", 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell("其它", 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell("", 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell("合計:", 1, 2, Element.ALIGN_MIDDLE, fontChinese));
table.addCell(getCell("", 1, 2, Element.ALIGN_MIDDLE, fontChinese));

table.addCell(getCell("指導教師意見", 1, 2, Element.ALIGN_MIDDLE,fontChinese));
Paragraph pa = new Paragraph("", fontChinese);
pa.add(Chunk.NEWLINE);
pa.add(Chunk.NEWLINE);
pa.add(Chunk.NEWLINE);
pa.add(Chunk.NEWLINE);
pa.add(Chunk.NEWLINE);
pa.add(Chunk.NEWLINE);
Phrase p1 = new Phrase(" 負責人簽字:");
pa.add(p1);
pa.add(Chunk.NEWLINE);
pa.add(new Phrase(" 年 月 日"));



table.addCell(getCell(pa, 1, 4, Element.ALIGN_MIDDLE));

table.addCell(getCell("依托實驗室意見(可行性)", 1, 2, Element.ALIGN_MIDDLE,fontChinese));
table.addCell(getCell(pa, 1, 4, Element.ALIGN_MIDDLE));

table.addCell(getCell("組織申報單位意見", 1, 2, Element.ALIGN_MIDDLE,fontChinese));
table.addCell(getCell(pa, 1, 4, Element.ALIGN_MIDDLE));

Paragraph pa2 = new Paragraph("", fontChinese);
pa2.add(Chunk.NEWLINE);
pa2.add(Chunk.NEWLINE);
pa2.add(Chunk.NEWLINE);
pa2.add(Chunk.NEWLINE);
pa2.add(Chunk.NEWLINE);
pa2.add(Chunk.NEWLINE);
Phrase p2 = new Phrase(" 組長:");
pa2.add(p2);
pa2.add(Chunk.NEWLINE);
Phrase p3 = new Phrase(" 成員:");
pa2.add(p3);
pa2.add(Chunk.NEWLINE);
pa2.add(new Phrase(" 年 月 日"));
table.addCell(getCell("學校專家組意見", 1, 2, Element.ALIGN_MIDDLE,fontChinese));
table.addCell(getCell(pa2, 1, 4, Element.ALIGN_MIDDLE));

table.addCell(getCell("學校審批意見", 1, 2, Element.ALIGN_MIDDLE,fontChinese));
table.addCell(getCell(pa, 1, 4, Element.ALIGN_MIDDLE));

document.add(table);
document.close();
return buffer;
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

private static Cell getCell(String content, Font font){
return getCell(content, 1, 1, Element.ALIGN_MIDDLE, font);
}

private static Cell getCell(String content, int rowSpan, int colSpan, int align, Font font){
Paragraph pa = new Paragraph(content, font);
pa.setAlignment(Element.ALIGN_CENTER);
return getCell(pa, rowSpan, colSpan, align);
}

private static Cell getCell(Paragraph content, int rowSpan, int colSpan, int align){
try {
Cell cell = new Cell(content);
cell.setColspan(colSpan);
cell.setRowspan(rowSpan);
cell.setUseAscender(true);
cell.setVerticalAlignment(align);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setLeading(30);
return cell;
} catch (BadElementException e) {
e.printStackTrace();
}
return null;
}

private static Paragraph getTitle(BaseFont bfChinese){
Paragraph title = new Paragraph("某某大學教學實驗室開放基金立項申請表");
// 設置標題格式對齊方式
title.setAlignment(Element.ALIGN_CENTER);
// 標題字體風格
Font titleFont = new Font(bfChinese, 12, Font.BOLD);
title.setFont(titleFont);
return title;
}

private static Paragraph getProjectName(Font contextFont, TaKyxmProjectinfo obj){
Paragraph context = new Paragraph();
context.add(Chunk.NEWLINE);
context.add(Chunk.NEWLINE);
context.setAlignment(Element.ALIGN_LEFT);
context.setFont(contextFont);
// 段間距
//context.setSpacingBefore(1);
// 設置第一行空的列數
context.setFirstLineIndent(20);
Phrase p1 = new Phrase("申報單位名稱(公章):" + obj.getSbdw().getDwmc());
context.add(p1);
Phrase p2 = new Phrase(" 編號:" + obj.getXmbh());
context.add(p2);
return context;
}
}

生成出來的大概效果如圖:


這里有幾個地方要注意:

1. 設置表格居中,我開始是設置 cell.setVerticalAlignment(Element.ALIGN_CENTER); 但是沒有效果,后來設置對cell里面的Paragraph設置居中,就有效果了

        Paragraph pa = new Paragraph(content, font);
        pa.setAlignment(Element.ALIGN_CENTER);

2. 通過Chunk.NEWLINE 可以增加一個新行。


參考資料:

http://www.cnblogs.com/julyluo/archive/2012/06/23/2559580.html


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com