欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

poi 读取word 遍历表格和单元格中的图片

程序员文章站 2022-07-01 15:44:54
背景 项目需要解析word表格 需要批量导入系统,并保存每行信息到数据库 并且要保存word中的图片, 并保持每条信息和图片的对应关系 一行数据可能有多条图片 解决办法 没有找到现成的代码,怎么办呐?看源码吧 分享快乐 给出代码 java package com.util; import org.a ......

背景

项目需要解析word表格
poi 读取word  遍历表格和单元格中的图片

  • 需要批量导入系统,并保存每行信息到数据库
  • 并且要保存word中的图片,
  • 并保持每条信息和图片的对应关系
  • 一行数据可能有多条图片

解决办法

没有找到现成的代码,怎么办呐?看源码吧

分享快乐

给出代码

package com.util;

import org.apache.poi.xwpf.usermodel.*;
import org.jeecgframework.core.common.model.json.ajaxjson;
import org.jeecgframework.poi.word.entity.myxwpfdocument;
import org.jeecgframework.poi.word.parse.excel.excelentityparse;
import org.slf4j.logger;
import org.slf4j.loggerfactory;

import javax.imageio.stream.fileimageoutputstream;
import java.io.*;
import java.util.iterator;
import java.util.list;

public class wordimportutil {
    private static final logger logger = loggerfactory.getlogger(wordimportutil.class);

    public static myxwpfdocument getxwpfdocumen(inputstream is) {
        try {

            myxwpfdocument doc = new myxwpfdocument(is);
            return doc;
        } catch (exception e) {
            logger.error(e.getmessage(), e);
        } finally {
            try {
                is.close();
            } catch (exception e) {
                logger.error(e.getmessage(), e);
            }
        }
        return null;
    }

    public static ajaxjson parsethistable(myxwpfdocument doc){
        iterator<xwpftable> ittable = doc.gettablesiterator();
        xwpftable table;
        while (ittable.hasnext()) {
            table = ittable.next();

            xwpftablerow row;
            list<xwpftablecell> cells;
            object listobj;

            excelentityparse excelentityparse = new excelentityparse();
            for (int i = 0; i < table.getnumberofrows(); i++) {
                if(i ==0)
                    continue;
                row = table.getrow(i);
                cells = row.gettablecells();
                for (int j = 0; j < cells.size(); j++) {
                    xwpftablecell cell = cells.get(j);
                    if(j == 10){
                        getcellimage(cell);
                    }

                    //输出当前的单元格的数据
                    system.out.print(cell.gettext() + "\t");
                }

            }

        }
        return null;
    }

    public static  string getcellimage(xwpftablecell cell){
        list<xwpfparagraph> xwpfparagraphs =  cell.getparagraphs();
        if(xwpfparagraphs == null) return null;
        for(xwpfparagraph xwpfparagraph:xwpfparagraphs){
            list<xwpfrun> xwpfrunlist = xwpfparagraph.getruns();
            if(xwpfrunlist==null) return null;
            for(xwpfrun xwpfrun:xwpfrunlist){
                list<xwpfpicture> xwpfpicturelist =  xwpfrun.getembeddedpictures();
                if(xwpfparagraph==null) return null;
                for(xwpfpicture xwpfpicture:xwpfpicturelist){
                    xwpfpicture.getpicturedata().getdata();
                    xwpfpicture.getpicturedata().getfilename();
                    byte2image( xwpfpicture.getpicturedata().getdata(),"d:/"+ xwpfpicture.getpicturedata().getfilename());
                }
            }
        }
        return "";
    }

    public static  void byte2image(byte[] data,string path){
        if(data.length<3||path.equals("")) return;
        fileimageoutputstream imageoutput = null;
        try{
            imageoutput = new fileimageoutputstream(new file(path));
            imageoutput.write(data, 0, data.length);
            system.out.println("make picture success,please find image in " + path);
        } catch(exception ex) {
            system.out.println("exception: " + ex);
            ex.printstacktrace();
        }finally {
            try {
                imageoutput.close();
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }
    }

    public static void main(string[] args) throws exception{
        myxwpfdocument myxwpfdocument = getxwpfdocumen(new fileinputstream("d:/园艺作物加工副产物适宜性评价填写.docx"));
        parsethistable(myxwpfdocument);
    }

}