利用itext導出word表格,處理圖片

  


    在實際的項目開發中我們需要將後臺大量數據導出爲word或者是excel方便用戶操作,當然能完成這一功能的有freemarker,itext,poi等技術,本文講述以itext導出word。

   首先我們需要明白的是無論是freemarker,itext,poi都是先做好模板或者是先畫出模板然後在其中填充內容,那麼有了這種思想,就能做好這一功能。


    開發必須的三個架包:

wKiom1LY2vvANViGAACZHD5WK4M639.jpg





下面我們直接上代碼:

package com.csg.action;
import java.awt.Color;
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.Image;
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.lowagie.text.rtf.style.RtfFont;
public class WordTest2 {
    public void createDocContext(String file) throws DocumentException,
            IOException {
        // 設置紙張大小
        Document document = new Document(PageSize.A4);
        // 建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁盤中
        RtfWriter2.getInstance(document, new FileOutputStream(file));
        document.open();
        // 設置中文字體
        BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
                "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        // 標題字體風格
        RtfFont titleFont = new RtfFont("宋體", 21, Font.BOLD, Color.BLACK);
        /* Font titleFont = new Font(bfChinese, 21, Font.BOLD); */
        // 正文字體風格
        Font contextFont = new Font(bfChinese, 12, Font.NORMAL);
        Paragraph title = new Paragraph("xx畫院藝術品收藏審覈表");
        // 設置標題格式對齊方式
        title.setAlignment(Element.ALIGN_CENTER);
        title.setFont(titleFont);
        document.add(title);
        // 設置 Table 表格
        Table aTable = new Table(2);// 設置表格爲2列
        int width[] = { 4, 96 };// 每列的佔比例
        aTable.setWidths(width);// 設置每列所佔比例
        aTable.setWidth(100); // 佔頁面寬度 100%
        aTable.setAlignment(Element.ALIGN_CENTER);// 居中顯示
        aTable.setAlignment(Element.ALIGN_MIDDLE);// 縱向居中顯示
        aTable.setAutoFillEmptyCells(true); // 自動填滿
        aTable.setBorderWidth(1); // 邊框寬度
        aTable.setBorderColor(Color.BLACK); // 邊框顏色
        aTable.setPadding(0);// 襯距,看效果就知道什麼意思了
        aTable.setSpacing(0);// 即單元格之間的間距
        aTable.setBorder(1);// 邊框
        Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.black);
        Cell cell = new Cell(new Phrase("藝術品情況說明", fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBorderColor(Color.BLACK);
        aTable.addCell(cell);
        cell = new Cell(new Phrase("收藏xxx版畫作品14張合共6.5萬元,從本人手中直接購買。",
                fontChinese));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        aTable.addCell(cell);
        cell = new Cell("#5");
        aTable.addCell(cell);
        cell = new Cell(new Phrase("填表人:_________ 日期:___年___月___日" + "   ",
                fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        aTable.addCell(cell);
        document.add(aTable);
        // 設置Table表格,創建一個10列的表格,後臺循環數據
        aTable = new Table(10);
        aTable.setWidths(new int[] { 4, 10, 20, 10, 20, 10, 20, 10, 20, 6 });
        aTable.setWidth(100);
        aTable.addCell(new Cell("#1"));
        aTable.addCell(new Cell("序號"));
        aTable.addCell(new Cell("作品名稱"));
        aTable.addCell(new Cell("作者"));
        aTable.addCell(new Cell("年代"));
        aTable.addCell(new Cell("品名"));
        aTable.addCell(new Cell("尺寸"));
        aTable.addCell(new Cell("件數"));
        aTable.addCell(new Cell("收藏金額(不含稅)"));
        aTable.addCell(new Cell("稅收"));
        for (int i = 0; i < 10; i++) {
            aTable.addCell(new Cell(""));
            aTable.addCell(new Cell("第" + (i + 1) + "件"));
            aTable.addCell(new Cell("1234"));
            aTable.addCell(new Cell("1234"));
            aTable.addCell(new Cell("123"));
            aTable.addCell(new Cell("123"));
            aTable.addCell(new Cell("123"));
            aTable.addCell(new Cell("123"));
            aTable.addCell(new Cell("123"));
            aTable.addCell(new Cell("123"));
        }
        ;
        document.add(aTable);
        aTable = new Table(2);// 設置表格爲2列
        aTable.setWidths(new int[] { 5, 95 });// 設置每列所佔比例
        aTable.setWidth(100); // 佔頁面寬度 100%
        cell = new Cell(new Phrase("徵集小組組員執行徵集情況說明", fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBorderColor(Color.BLACK);
        aTable.addCell(cell);
        cell = new Cell(
                new Phrase(
                        "志強、學靈經手向xxx徵集版畫作品14張,合共6.5萬元,具體明細如上。經綜合分析,這14件作品建議列入“藏品”收藏,發收藏證書。當否,請審覈",
                        fontChinese));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        aTable.addCell(cell);
        cell = new Cell(new Phrase("徵集小組組員簽名", fontChinese));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setRowspan(8);// 合併行
        aTable.addCell(cell);
        cell = new Cell(new Phrase("", fontChinese));
        cell.setRowspan(8);
        aTable.addCell(cell);
        cell = new Cell(new Phrase("徵集小組副組長審覈", fontChinese));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setRowspan(9);
        aTable.addCell(cell);
        cell = new Cell(new Phrase("", fontChinese));
        cell.setRowspan(9);
        aTable.addCell(cell);
        cell = new Cell(new Phrase("徵集小組組長審覈", fontChinese));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setRowspan(8);
        aTable.addCell(cell);
        cell = new Cell(new Phrase("", fontChinese));
        cell.setRowspan(8);
        aTable.addCell(cell);
        cell = new Cell(new Phrase("藝術委員會意見", fontChinese));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setRowspan(8);
        aTable.addCell(cell);
        cell = new Cell(new Phrase("", fontChinese));
        cell.setRowspan(8);
        aTable.addCell(cell);
        document.add(aTable);
        // 設置5個表格 循環5次
        for (int i = 0; i < 5; i++) {
            // 打印圖像
            aTable = new Table(2);// 設置表格爲2列3行
            aTable.setWidths(new int[] { 50, 50 });// 設置每列所佔比例
            aTable.setWidth(100); // 佔頁面寬度 100%
            // 添加圖像1
            cell = new Cell();
            cell.setBorderWidth(0);// 邊框線設爲0
            Image img = Image.getInstance("d:/201305.jpg");
            img.setAbsolutePosition(0, 0);
            img.scaleAbsolute(12, 35);// 直接設定顯示尺寸
            img.scalePercent(50);// 表示顯示的大小爲原尺寸的50%
            img.scalePercent(25, 50);// 圖像高寬的顯示比例
            img.setRotation(30);// 圖像旋轉一定角度
            cell.add(img);
            aTable.addCell(cell);
            // 添加圖像2
            cell = new Cell();
            cell.setBorderWidth(0);
            cell.setVerticalAlignment(Element.ALIGN_RIGHT);
            img.setAlignment(Image.RIGHT);// 設置圖片顯示位置
            img = Image.getInstance("d:/201307.jpg");
            /*
             * img.scaleAbsolute(12,50);//直接設定顯示尺寸
             */img.scalePercent(50);// 表示顯示的大小爲原尺寸的50%
            img.scalePercent(25, 50);// 圖像高寬的顯示比例
            img.setRotation(30);// 圖像旋轉一定角度
            cell.add(img);
            aTable.addCell(cell);
            // 信息
            cell = new Cell(new Phrase("作品1:《文明符號迴響》", fontChinese));
            cell.setBorderWidth(0);
            aTable.addCell(cell);
            cell = new Cell(new Phrase("作品2:《石語之一》", fontChinese));
            cell.setBorderWidth(0);
            aTable.addCell(cell);
            cell = new Cell(
                    new Phrase("落款:23/25 文明符號迴響 xxx 2009", fontChinese));
            cell.setBorderWidth(0);
            aTable.addCell(cell);
            cell = new Cell(new Phrase("落款:7/15石語之一 石版畫 xxx 2011", fontChinese));
            cell.setBorderWidth(0);
            aTable.addCell(cell);
            document.add(aTable);
        }
        document.add(new Paragraph("\n" + "\n"));
        title = new Paragraph("作者簡介");
        // 設置標題格式對齊方式
        title.setAlignment(Element.ALIGN_CENTER);
        titleFont = new RtfFont("宋體", 16, Font.BOLD, Color.BLACK);
        title.setFont(titleFont);
        title.setSpacingAfter(10);
        document.add(title);
        // 正文
        String contextString = "xxx1969-年生於北京南靖,籍貫廣東潮州。1996年畢業於廣州美術學院版畫系並留校工作, 現爲廣州美術學院版畫系副教授,山西大學客座教授;中國美術家協會會員,中國美術家協會藏書票研究會常務副主席,廣東青年畫院畫家。"
                + "多件作品入選全國美展、全國版畫展、國際版畫展等學術展。曾獲第九屆全國美術作品展優秀獎、當代中國青年作品展二等獎、國務院慶祝澳門迴歸祖國宣傳招貼畫十佳作品獎、全軍廉政文化優秀作品展二等獎、廣東省美術作品展銅獎、廣東版畫獎等學術獎項。"
                + " \n"// 換行
                + "作品、論文發表於《美術》、《美術觀察》、《中國版畫》、《美術學報》、《北方美術》等學術刊物。出版教材《石版畫》、《版畫》(合著)等 ;出版作品集《中銳xxx作品集》;入編《中國美術大事記》、《當代中國藝術》、《中國當代中青年版畫家石版畫精品集》等。"
                + " \n"// 換行
                + "作品被廣東美術館、神州版畫博物館、中華世紀壇、紹興魯迅紀念館、觀瀾美術館、汕頭博物館、廣州美術學院、伊斯坦布爾國際版畫館等學術機構及個人收藏。";
        Paragraph context = new Paragraph(contextString);
        // 正文格式左對齊
        context.setAlignment(Element.ALIGN_LEFT);
        context.setFont(contextFont);
        // 離上一段落(標題)空的行數
        context.setSpacingBefore(10);
        // 設置第一行空的列數
        context.setFirstLineIndent(20);
        document.add(context);
        document.close();
    }
    public static void main(String[] args) {
        WordTest2 b = new WordTest2();
        try {
            b.createDocContext("d:/demo.doc");
            System.out.println("導出成功");
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

   本文示例圖片請直接拷貝到 D:盤!

本文以測試爲主,實戰本代碼只做參考。匆忙之作,歡迎指正!

   源碼下載地址:http://down.51cto.com/data/1071741


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章