poi 生成word 表格,并向表格单元格中插入多个图片

2019.05.28 - 30

接这上一篇,导入数据,也要完整导出来。话不多说,直接上代码。
效果图

  //根据实体对象 ,生成XWPFDocument 
    public static XWPFDocument exportDataInfoWord(List<DataInfoEntity> list) throws NoSuchFieldException,IllegalAccessException {
        MyXWPFDocument doc = new MyXWPFDocument();
        XWPFTable table = doc.createTable(list.size() + 1, 12);
        List<TSType> types = ResourceUtil.getCacheTypes("primaryUse".toLowerCase());

        for(int colsIndex=0;colsIndex<fieldsNames.length;colsIndex++){
            XWPFTableCell cell = table.getRow(0).getCell(colsIndex);
            cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
            XWPFParagraph p1 = cell.getParagraphs().get(0);
            XWPFRun r1 = p1.createRun();
            r1.setBold(true);
            r1.setText(fieldsNames[colsIndex]);
        }

        for(int rowIndex =1, listIndex =0; listIndex<list.size();rowIndex++,listIndex++){
            Class entityClass = list.get(listIndex).getClass();
            for(int colsIndex=0;colsIndex<fields.length;colsIndex++){
                Field field = entityClass.getDeclaredField(fields[colsIndex]);
                field.setAccessible(true);
                Object value = field.get(list.get(listIndex));


                XWPFTableCell cell = table.getRow(rowIndex).getCell(colsIndex);

                if(value instanceof Date){
                    cell.setText(new SimpleDateFormat("yyyy-MM-dd").format((Date)value));
                }else if(colsIndex == 4){
                    cell.setText(list.get(listIndex).getFgTypeName());
                }else if(colsIndex == 5){
                    cell.setText(list.get(listIndex).getFgVarietiesName());
                }
                else if(colsIndex == 7){
                    for(TSType tsType:types){
                        if(tsType.getTypecode().equals(list.get(listIndex).getPrimaryUse())){
                            cell.setText(tsType.getTypename());
                        }
                    }
                }
                else if(value instanceof Float){
                    cell.setText(String.valueOf(value));
                } else if (colsIndex == 11 && value instanceof String && value!=null) {
                    setCellImage(cell,value.toString());
                }else {
                    if(value!=null){
                        cell.setText(value.toString());
                    }
                }
            }
        }


        return doc;
    }

//单元格写入图片
private static  void setCellImage(XWPFTableCell cell,String urls)  {
        if(StringUtils.isBlank(urls))
            return;
        String [] urlArray = urls.split(",");
        String ctxPath=ResourceUtil.getConfigByName("webUploadpath");
        List<XWPFParagraph> paragraphs = cell.getParagraphs();
        XWPFParagraph newPara = paragraphs.get(0);
        XWPFRun imageCellRunn = newPara.createRun();
        for(String url:urlArray){
            String downLoadPath = ctxPath+File.separator + url;
            File image = new File(downLoadPath);
            if(!image.exists()){
                continue;
            }

            int format;
            if (url.endsWith(".emf")) {
                format = XWPFDocument.PICTURE_TYPE_EMF;
            } else if (url.endsWith(".wmf")) {
                format = XWPFDocument.PICTURE_TYPE_WMF;
            } else if (url.endsWith(".pict")) {
                format = XWPFDocument.PICTURE_TYPE_PICT;
            } else if (url.endsWith(".jpeg") || url.endsWith(".jpg")) {
                format = XWPFDocument.PICTURE_TYPE_JPEG;
            } else if (url.endsWith(".png")) {
                format = XWPFDocument.PICTURE_TYPE_PNG;
            } else if (url.endsWith(".dib")) {
                format = XWPFDocument.PICTURE_TYPE_DIB;
            } else if (url.endsWith(".gif")) {
                format = XWPFDocument.PICTURE_TYPE_GIF;
            } else if (url.endsWith(".tiff")) {
                format = XWPFDocument.PICTURE_TYPE_TIFF;
            } else if (url.endsWith(".eps")) {
                format = XWPFDocument.PICTURE_TYPE_EPS;
            } else if (url.endsWith(".bmp")) {
                format = XWPFDocument.PICTURE_TYPE_BMP;
            } else if (url.endsWith(".wpg")) {
                format = XWPFDocument.PICTURE_TYPE_WPG;
            } else {
                logger.error("Unsupported picture: " + url +
                        ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
                continue;
            }

            try (FileInputStream is = new FileInputStream(downLoadPath)) {
                imageCellRunn.addPicture(is, format, image.getName(), Units.toEMU(100), Units.toEMU(100)); // 200x200 pixels
            }catch (Exception e){
                logger.error(e.getMessage());
                e.printStackTrace();
            }
//            imageCellRunn.addBreak();

        }


    }

实体对象

package com.entity.garden;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.*;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.jeecgframework.poi.excel.annotation.Excel;

/**   
 * @Title: Entity
 * @Description: 上传数据信息
 * @author zhangdaihao
 * @date 2019-05-12 16:54:17
 * @version V1.0   
 *
 */
@Entity
@Table(name = "g_data_info", schema = "")
@DynamicUpdate(true)
@DynamicInsert(true)
@SuppressWarnings("serial")
public class DataInfoEntity implements java.io.Serializable {
    /**id*/
    private java.lang.String id;
    /**时间*/
    @Excel(name="年份", width = 15, format = "yyyy-MM-dd")
    private java.util.Date dataInfoTime;
    /**省*/
    @Excel(name="省", width = 15)
    private java.lang.String provinceId;
    /**市*/
    @Excel(name="市", width = 15)
    private java.lang.String cityId;
    /**区*/
    @Excel(name="区或县", width = 15)
    private java.lang.String areaId;

    @Excel(name="果蔬种类", width = 15)
    @Transient
    private  String fgTypeName;

    @Excel(name="果蔬品种", width = 15)
    @Transient
    private String fgVarietiesName;
    /**果蔬种类*/

    private FgTypeEntity fgTypeEntity;

    /**果蔬品种*/

    private FgVarietiesEntity fgVarietiesEntity;

    /**生产量*/
    @Excel(name="生产量", width = 15 ,type = 4 )
    private java.lang.Float outPus;
    /**主要用途*/
    @Excel(name="主要用途", width = 25,   dicCode="primaryUse")
    private java.lang.String primaryUse;
    /**加工量*/
    @Excel(name="加工量", width = 15 ,type = 4 )
    private java.lang.Float processCapacity;
    /**加工副产量*/
    @Excel(name="加工副产量", width = 15 ,type = 4 )
    private java.lang.Float byProduceProcess;


    /**加工副产物适应性评价*/
    @Excel(name="加工副产物适应性评价", width = 25  )
    private java.lang.String evaluate;

    /**图片*/
    @Excel(name="图片", width =50 ,type=2)
    private java.lang.String fileIds;


    private String userName;

    private Date createTime;

    /**
     *方法: 取得java.lang.String
     *@return: java.lang.String  id
     */

    @Id
    @GeneratedValue(generator = "paymentableGenerator")
    @GenericGenerator(name = "paymentableGenerator", strategy = "uuid")
    @Column(name ="ID",nullable=false,length=36)
    public java.lang.String getId(){
        return this.id;
    }

    /**
     *方法: 设置java.lang.String
     *@param: java.lang.String  id
     */
    public void setId(java.lang.String id){
        this.id = id;
    }
    /**
     *方法: 取得java.util.Date
     *@return: java.util.Date  时间
     */
    @Column(name ="DATA_INFO_TIME",nullable=true)
    public java.util.Date getDataInfoTime(){
        return this.dataInfoTime;
    }

    /**
     *方法: 设置java.util.Date
     *@param: java.util.Date  时间
     */
    public void setDataInfoTime(java.util.Date dataInfoTime){
        this.dataInfoTime = dataInfoTime;
    }
    /**
     *方法: 取得java.lang.String
     *@return: java.lang.String  省
     */
    @Column(name ="PROVINCE_ID",nullable=true,length=36)
    public java.lang.String getProvinceId(){
        return this.provinceId;
    }

    /**
     *方法: 设置java.lang.String
     *@param: java.lang.String  省
     */
    public void setProvinceId(java.lang.String provinceId){
        this.provinceId = provinceId;
    }
    /**
     *方法: 取得java.lang.String
     *@return: java.lang.String  市
     */
    @Column(name ="CITY_ID",nullable=true,length=36)
    public java.lang.String getCityId(){
        return this.cityId;
    }

    /**
     *方法: 设置java.lang.String
     *@param: java.lang.String  市
     */
    public void setCityId(java.lang.String cityId){
        this.cityId = cityId;
    }
    /**
     *方法: 取得java.lang.String
     *@return: java.lang.String  区
     */
    @Column(name ="AREA_ID",nullable=true,length=36)
    public java.lang.String getAreaId(){
        return this.areaId;
    }

    /**
     *方法: 设置java.lang.String
     *@param: java.lang.String  区
     */
    public void setAreaId(java.lang.String areaId){
        this.areaId = areaId;
    }
    /**
     *方法: 取得java.lang.String
     *@return: java.lang.String  果蔬种类
     */


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "FG_TYPE_ID")
    public FgTypeEntity getFgTypeEntity() {
        return fgTypeEntity;
    }

    public void setFgTypeEntity(FgTypeEntity fgTypeEntity) {
        this.fgTypeEntity = fgTypeEntity;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "FG_VARIETIES_ID")
    public FgVarietiesEntity getFgVarietiesEntity() {
        return fgVarietiesEntity;
    }

    public void setFgVarietiesEntity(FgVarietiesEntity fgVarietiesEntity) {
        this.fgVarietiesEntity = fgVarietiesEntity;
    }


    /**
     *方法: 取得java.lang.Float
     *@return: java.lang.Float  生产量
     */
    @Column(name ="OUT_PUS",nullable=true,precision=10,scale=2)
    public java.lang.Float getOutPus(){
        return this.outPus;
    }

    /**
     *方法: 设置java.lang.Float
     *@param: java.lang.Float  生产量
     */
    public void setOutPus(java.lang.Float outPus){
        this.outPus = outPus;
    }
    /**
     *方法: 取得java.lang.String
     *@return: java.lang.String  主要用途
     */
    @Column(name ="PRIMARY_USE",nullable=true,length=1000)
    public java.lang.String getPrimaryUse(){
        return this.primaryUse;
    }

    /**
     *方法: 设置java.lang.String
     *@param: java.lang.String  主要用途
     */
    public void setPrimaryUse(java.lang.String primaryUse){
        this.primaryUse = primaryUse;
    }
    /**
     *方法: 取得java.lang.Float
     *@return: java.lang.Float  加工量
     */
    @Column(name ="PROCESS_CAPACITY",nullable=true,precision=10,scale=2)
    public java.lang.Float getProcessCapacity(){
        return this.processCapacity;
    }

    /**
     *方法: 设置java.lang.Float
     *@param: java.lang.Float  加工量
     */
    public void setProcessCapacity(java.lang.Float processCapacity){
        this.processCapacity = processCapacity;
    }
    /**
     *方法: 取得java.lang.Float
     *@return: java.lang.Float  加工副产量
     */
    @Column(name ="BY_PRODUCE_PROCESS",nullable=true,precision=10,scale=2)
    public java.lang.Float getByProduceProcess(){
        return this.byProduceProcess;
    }

    /**
     *方法: 设置java.lang.Float
     *@param: java.lang.Float  加工副产量
     */
    public void setByProduceProcess(java.lang.Float byProduceProcess){
        this.byProduceProcess = byProduceProcess;
    }
    /**
     *方法: 取得java.lang.String
     *@return: java.lang.String  加工副产物适应性评价
     */
    @Column(name ="EVALUATE",nullable=true,length=2000)
    public java.lang.String getEvaluate(){
        return this.evaluate;
    }

    /**
     *方法: 设置java.lang.String
     *@param: java.lang.String  加工副产物适应性评价
     */
    public void setEvaluate(java.lang.String evaluate){
        this.evaluate = evaluate;
    }
    /**
     *方法: 取得java.lang.String
     *@return: java.lang.String  图片
     */
    @Column(name ="FILE_IDS",nullable=true,length=2000)
    public java.lang.String getFileIds(){
        return this.fileIds;
    }

    /**
     *方法: 设置java.lang.String
     *@param: java.lang.String  图片
     */
    public void setFileIds(java.lang.String fileIds){
        this.fileIds = fileIds;
    }
    @Transient
    public String getFgTypeName() {
        if(fgTypeName == null&&fgTypeEntity!=null){
            return this.fgTypeEntity.getName();
        }
        return fgTypeName;
    }

    public void setFgTypeName(String fgTypeName) {
        this.fgTypeName = fgTypeName;
    }
    @Transient
    public String getFgVarietiesName() {
        if(fgVarietiesName == null&&fgVarietiesEntity!=null){
            return this.fgVarietiesEntity.getName();
        }
        return fgVarietiesName;
    }

    public void setFgVarietiesName(String fgVarietiesName) {
        this.fgVarietiesName = fgVarietiesName;
    }
    @Column(name ="user_name",nullable=true)
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    @Column(name ="create_time",nullable=true)
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

- END -

54
0