POI之读写Word

HWPFDocument之Doc

一、开发须知

Apache poi的hwpf模块是专门用来对word(doc格式)进行读写操作的,只能对简单地进行模板替换复制(包含图片),但要新增图片则需要用到XWPFDocument(docx格式),在HWPFDocument里面有这么几个概念:

l  Range:它表示一个范围,这个范围可以是整个文档,也可以是里面的某一小节(Section),也可以是某一个段落(Paragraph),还可以是拥有共同属性的一段文本(CharacterRun)。

l  Section:word文档的一个小节,一个word文档可以由多个小节构成。

l  Paragraph:word文档的一个段落,一个小节可以由多个段落构成。

l  CharacterRun:具有相同属性的一段文本,一个段落可以由多个CharacterRun组成。

l  Table:一个表格。

l  TableRow:表格对应的行。

l  TableCell:表格对应的单元格。

二、模块Word(doc格式)替换文本


import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.junit.Test;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * hwpf根据模板替换
 *
 * @author Harley Hong
 * @created 2017 /08/09 17:37:31
 */
public class HWPFDocumentTest {
    public static void main(String[] args) throws IOException {
        InputStream is = new FileInputStream("E:\\test.doc");
        HWPFDocument doc = new HWPFDocument(is);
        Range range = doc.getRange();
        range.replaceText("#time", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        OutputStream os = new FileOutputStream("E:\\testcopy.doc");
        doc.write(os);
        is.close();
        os.close();
    }
}

 

 

XWPFDocument之Docx

一、开发须知

Apache poi的xwpf模块是专门用来对word(docx格式)进行读写操作的,在XWPFDocument里面有这么几个概念:

l  XWPFParagraph:代表一个段落。

l  XWPFRun:代表具有相同属性的一段文本。

l  XWPFTable:代表一个表格。

l  XWPFTableRow:表格的一行。

l  XWPFTableCell:表格对应的一个单元格。

二、模块Word(docx格式)替换文本


import net.evecom.core.util.CustomXWPFDocument;
import net.evecom.core.util.WordUtil;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/9.
 *
 * @author Harley Hong
 * @created 2017 /08/09 17:37:12
 */
public class XWPFDocumentTest {
    public static void main(String[] args) throws Exception {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("#author", "huangqiqing");
        param.put("#time", new Date().toString());

        Map<String, Object> header = new HashMap<String, Object>();
        header.put("width", 100);
        header.put("height", 150);
        header.put("type", "jpg");
        header.put("content", WordUtil.inputStream2ByteArray(new FileInputStream("c:\\goddess.jpg"), true));
        param.put("#image", header);

        CustomXWPFDocument doc = WordUtil.generateWord(param, "E:\\测试文本\\test.docx");
        FileOutputStream fopts = new FileOutputStream("E:\\测试文本\\test2.docx");
        doc.write(fopts);
        fopts.close();
    }
}
package net.evecom.core.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

/**
 * 适用于word 2007 poi 版本 3.7
 */
public class WordUtil {

    /**
     * 根据指定的参数值、模板,生成 word 文档
     *
     * @param param
     *            需要替换的变量
     * @param template
     *            模板
     */
    public static CustomXWPFDocument generateWord(Map<String, Object> param, String template) {
        CustomXWPFDocument doc = null;
        try {
            OPCPackage pack = POIXMLDocument.openPackage(template);
            doc = new CustomXWPFDocument(pack);
            if (param != null && param.size() > 0) {

                // 处理段落
                List<XWPFParagraph> paragraphList = doc.getParagraphs();
                processParagraphs(paragraphList, param, doc);

                // 处理表格
                Iterator<XWPFTable> it = doc.getTablesIterator();
                while (it.hasNext()) {
                    XWPFTable table = it.next();
                    List<XWPFTableRow> rows = table.getRows();
                    for (XWPFTableRow row : rows) {
                        List<XWPFTableCell> cells = row.getTableCells();
                        for (XWPFTableCell cell : cells) {
                            List<XWPFParagraph> paragraphListTable = cell.getParagraphs();
                            processParagraphs(paragraphListTable, param, doc);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return doc;
    }

    /**
     * 处理段落
     *
     * @param paragraphList
     */
    public static void processParagraphs(List<XWPFParagraph> paragraphList, Map<String, Object> param,
            CustomXWPFDocument doc) {
        if (paragraphList != null && paragraphList.size() > 0) {
            for (XWPFParagraph paragraph : paragraphList) {
                List<XWPFRun> runs = paragraph.getRuns();
                for (int i = 0; i < runs.size(); i++) {
                    XWPFRun run = runs.get(i);
                    String text = run.getText(0);
                    if (text != null) {
                        boolean isSetText = false;
                        for (Entry<String, Object> entry : param.entrySet()) {
                            String key = entry.getKey();
                            if (text.indexOf(key) != -1) {
                                isSetText = true;
                                Object value = entry.getValue();
                                if (value instanceof String) {// 文本替换
                                    text = text.replace(key, value.toString());
                                } else if (value instanceof Map) {// 图片替换
                                    text = text.replace(key, "");
                                    Map pic = (Map) value;
                                    int width = Integer.parseInt(pic.get("width").toString());
                                    int height = Integer.parseInt(pic.get("height").toString());
                                    int picType = getPictureType(pic.get("type").toString());
                                    byte[] byteArray = (byte[]) pic.get("content");
                                    ByteArrayInputStream byteInputStream = new ByteArrayInputStream(byteArray);
                                    try {
                                        String blipId = doc.addPictureData(byteInputStream, picType);
                                        doc.createPicture(blipId, doc.getNextPicNameNumber(picType), width, height,
                                                paragraph);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                        if (isSetText) {
                            run.setText(text, 0);
                        }
                    }
                }
            }
        }
    }

    /**
     * 根据图片类型,取得对应的图片类型代码
     *
     * @param picType
     * @return int
     */
    private static int getPictureType(String picType) {
        int res = CustomXWPFDocument.PICTURE_TYPE_PICT;
        if (picType != null) {
            if (picType.equalsIgnoreCase("png")) {
                res = CustomXWPFDocument.PICTURE_TYPE_PNG;
            } else if (picType.equalsIgnoreCase("dib")) {
                res = CustomXWPFDocument.PICTURE_TYPE_DIB;
            } else if (picType.equalsIgnoreCase("emf")) {
                res = CustomXWPFDocument.PICTURE_TYPE_EMF;
            } else if (picType.equalsIgnoreCase("jpg") || picType.equalsIgnoreCase("jpeg")) {
                res = CustomXWPFDocument.PICTURE_TYPE_JPEG;
            } else if (picType.equalsIgnoreCase("wmf")) {
                res = CustomXWPFDocument.PICTURE_TYPE_WMF;
            }
        }
        return res;
    }

    /**
     * 将输入流中的数据写入字节数组
     *
     * @param in
     * @return
     */
    public static byte[] inputStream2ByteArray(InputStream in, boolean isClose) {
        byte[] byteArray = null;
        try {
            int total = in.available();
            byteArray = new byte[total];
            in.read(byteArray);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (isClose) {
                try {
                    in.close();
                } catch (Exception e2) {
                    System.out.println("关闭流失败");
                }
            }
        }
        return byteArray;
    }
}
package net.evecom.core.util;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlToken;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;

/**
 * 自定义 XWPFDocument,并重写 createPicture()方法
 */
public class CustomXWPFDocument extends XWPFDocument {
    public CustomXWPFDocument(InputStream in) throws IOException {
        super(in);
    }

    public CustomXWPFDocument() {
        super();
    }

    public CustomXWPFDocument(OPCPackage pkg) throws IOException {
        super(pkg);
    }

    /**
     * @param id
     * @param width 宽
     * @param height 高
     * @param paragraph  段落
     */
    public void createPicture(String blipId,int id, int width, int height,XWPFParagraph paragraph) {
        final int EMU = 9525;
        width *= EMU;
        height *= EMU;
        CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline();
        String picXml = ""
                + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"
                + "   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
                + "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
                + "         <pic:nvPicPr>" + "            <pic:cNvPr id=\""
                + id
                + "\" name=\"Generated\"/>"
                + "            <pic:cNvPicPr/>"
                + "         </pic:nvPicPr>"
                + "         <pic:blipFill>"
                + "            <a:blip r:embed=\""
                + blipId
                + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"
                + "            <a:stretch>"
                + "               <a:fillRect/>"
                + "            </a:stretch>"
                + "         </pic:blipFill>"
                + "         <pic:spPr>"
                + "            <a:xfrm>"
                + "               <a:off x=\"0\" y=\"0\"/>"
                + "               <a:ext cx=\""
                + width
                + "\" cy=\""
                + height
                + "\"/>"
                + "            </a:xfrm>"
                + "            <a:prstGeom prst=\"rect\">"
                + "               <a:avLst/>"
                + "            </a:prstGeom>"
                + "         </pic:spPr>"
                + "      </pic:pic>"
                + "   </a:graphicData>" + "</a:graphic>";

        inline.addNewGraphic().addNewGraphicData();
        XmlToken xmlToken = null;
        try {
            xmlToken = XmlToken.Factory.parse(picXml);
        } catch (XmlException xe) {
            xe.printStackTrace();
        }
        inline.set(xmlToken);

        inline.setDistT(0);
        inline.setDistB(0);
        inline.setDistL(0);
        inline.setDistR(0);

        CTPositiveSize2D extent = inline.addNewExtent();
        extent.setCx(width);
        extent.setCy(height);

        CTNonVisualDrawingProps docPr = inline.addNewDocPr();
        docPr.setId(id);
        docPr.setName("Picture" + id);
        docPr.setDescr("Generated");
    }
}

三、Word单元格合并方法与样式

1、合并单元格

 /**
     * 描述 跨列合并单元格
     *
     * @param table    the table
     * @param row      the row
     * @param fromCell the from cell
     * @param toCell   the to cell
     * @author Harley Hong
     * @created 2017 /09/13 22:11:48 Merge cells horizontal.
     */
    public static void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
        for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
            XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
            if ( cellIndex == fromCell ) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
            }
        }
    }

    /**
     * 描述 跨行合并单元格
     *
     * @param table   the table
     * @param col     the col
     * @param fromRow the from row
     * @param toRow   the to row
     * @author Harley Hong
     * @created 2017 /09/13 22:12:21 Merge cells vertically.
     */
    public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
        for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
            XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
            if ( rowIndex == fromRow ) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
            }
        }
    }

2、各种样式

  /**
     * 描述 设置单元格,包含字体大小与颜色
     *
     * @param cell     the cell
     * @param text     the text
     * @param bgcolor  the bgcolor
     * @param fontSize the font size
     * @param width    the width
     * @author Harley Hong
     * @created 2017 /09/13 21:45:03
     */
    public static void setCellTextAndFont(XWPFTableCell cell, String text, String bgcolor, String fontSize,
            String width,String fontFamily) {
        cell.removeParagraph(0);
        XWPFParagraph newPara = new XWPFParagraph(cell.getCTTc().addNewP(), cell);
        XWPFRun run = newPara.createRun();
        newPara.setAlignment(ParagraphAlignment.CENTER); // 水平居中
        cell.getCTTc().addNewTcPr().addNewVAlign().setVal(STVerticalJc.CENTER);  // 垂直居中
        run.setText(text);
        if (StringUtils.isNotEmpty(bgcolor)) {
            run.getCTR().addNewRPr().addNewColor().setVal(bgcolor);
        }        // 单元格颜色
        if (StringUtils.isNotEmpty(fontSize)) {
            run.setFontSize(Integer.parseInt(fontSize));
        }       // 单元格字体大小
        if (StringUtils.isNotEmpty(fontFamily)) {
            run.setFontFamily(fontFamily);
        }       // 单元格字体样式
        if (StringUtils.isNotEmpty(width)) {
            CTTc cttc = cell.getCTTc();
            CTTcPr tcPr = cttc.addNewTcPr();
            CTTblWidth tcw = tcPr.isSetTcW() ? tcPr.getTcW() : tcPr.addNewTcW();
            tcw.setW(new BigInteger(width));
        }
    }

    /**
     * 描述 设置表格宽度
     * 默认TblW的type属性为STTblWidth.AUTO,即自动伸缩。所以要调整为指定类型:STTblWidth.DXA
     *
     * @param table the table
     * @param width the width
     * @author Harley Hong
     * @created 2017 /09/13 22:06:00 Set table width.
     */
    public static void setTableWidth(XWPFTable table,String width){
        CTTbl ttbl = table.getCTTbl();
        CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
        CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
        CTJc cTJc = tblPr.addNewJc();
        cTJc.setVal(STJc.Enum.forString("center"));
        tblWidth.setW(new BigInteger(width));
        tblWidth.setType(STTblWidth.DXA);
    }


    /**
     * 描述 设置单元格宽度与排列顺序
     *
     * @param cell     the cell
     * @param width    the width
     * @param typeEnum the type enum
     * @param vAlign   the v align
     * @author Harley Hong
     * @created 2017 /09/26 16:17:12
     */
    public static void setCellWidthAndVAlign(XWPFTableCell cell, String width, STTblWidth.Enum typeEnum,
            STVerticalJc.Enum vAlign) {
        CTTc cttc = cell.getCTTc();
        CTTcPr tcPr = cttc.addNewTcPr();
        CTTblWidth tcw = tcPr.isSetTcW() ? tcPr.getTcW() : tcPr.addNewTcW();
        if (width != null) {
            tcw.setW(new BigInteger(width));
        }
        if (typeEnum != null) {
            tcw.setType(typeEnum);
        }
        if (vAlign != null) {
            CTVerticalJc vJc = tcPr.isSetVAlign() ? tcPr.getVAlign() : tcPr.addNewVAlign();
            vJc.setVal(vAlign);
        }
    }

 

发帖时间: POI

发表评论

电子邮件地址不会被公开。 必填项已用*标注