背景

项目需要解析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);
} }

poi 读取word 遍历表格和单元格中的图片的更多相关文章

  1. 如何在excel单元格中插入图片批注

    在excel单元格中插入图片批注的方法: 1.选定要插入图片的单元格,然后右键选择插入批注. 2.然后会插入一个批注框,为了不影响图片效果,可以将批注文字都删除.然后鼠标移动到批注框边角再右键. 3. ...

  2. Aspose.Word 操作word复杂表格 拆分单元格 复制行 插入行 文字颜色

    private void button3_Click(object sender, EventArgs e)         {             object savePathWord =&q ...

  3. Swift - 给表格的单元格UITableViewCell添加图片,详细文本标签

    表格UITableView中,每一单元格都是一个UITableViewCell.其支持简单的自定义,比如在单元格的内部,添加图片和详细文本标签. 注意UITableViewCell的style: (1 ...

  4. c#在Excel指定单元格中插入图片

    方法一: /// 将图片插入到指定的单元格位置,并设置图片的宽度和高度./// 注意:图片必须是绝对物理路径/// </summary>/// <param name="R ...

  5. POI读取带有公式的Excel单元格-xssf

    if(CellType.FORMULA == row.getCell(j).getCellTypeEnum()) { try { cellValue = String.valueOf(row.getC ...

  6. jQuery,遍历表格每个单元格数据。

    <table class="table table-hover table-bordered"> <thead> <tr> <th > ...

  7. 使用POI创建word表格合并单元格兼容wps

    poi创建word表格合并单元格代码如下: /** * @Description: 跨列合并 */ public void mergeCellsHorizontal(XWPFTable table, ...

  8. 使用POI创建word表格-在表格单元格中创建子表格

    要实现的功能如下:表格中的单元格中有子表格 实现代码如下: XWPFParagraph cellPara = row.getCell(j).getParagraphArray(0); //row.ge ...

  9. 将Word表格中单元格中的文字替换成对应的图片

    示例 原文件结构: 替换后文档结构: 软件截图: 代码: using System;using System.Collections.Generic;using System.ComponentMod ...

随机推荐

  1. DEBUG技巧里的问题1 双击某个变量不能显示

    DEBUG模式  双击 ls_return-type 变量不能显示,提示警告消息 好像说明的不是这个问题, 把字段复制到右边的变量框里可以显示 这个确实有点奇怪了

  2. 删除SQL约束的方法

    在SQL数据库中,如果需要删除表约束,应该如何操作呢?下面就将为您介绍删除SQL表约束的方法,供您参考,希望对您有所帮助. --1)禁止所有表约束的SQL select 'alter table '+ ...

  3. RabbitMQ官方教程三 Publish/Subscribe(GOLANG语言实现)

    RabbitMQ官方教程三 Publish/Subscribe(GOLANG语言实现) 在上一个教程中,我们创建了一个工作队列. 工作队列背后的假设是,每个任务都恰好交付给一个worker处理. 在这 ...

  4. 一段话让你理解vuex的工作模式!

    vuex 个人理解:管理各组件公共状态的vue插件,也是个组件相互通信的插件. 组成:1.State:状态树. 2.Getters:操作state. 3.Mutation:唯一改变state状态的操作 ...

  5. springboot 通过docker 打包编译镜像

    添加plugin <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht ...

  6. 分布式 ID

    [参考文章] Leaf——美团点评分布式ID生成系统 分布式全局唯一ID生成策略 从一次 Snowflake 异常说起 [雪花算法问题] 微服务架构下 机器码如何生成? 如何处理时钟回调问题?

  7. [转帖]Hadoop、Hive、Spark 之间关系

    Hadoop.Hive.Spark 之间关系 https://www.cnblogs.com/jins-note/p/9513426.html 很的很诙谐有趣. 作者:Xiaoyu Ma ,大数据工程 ...

  8. [转帖]我最近研究了hive的相关技术,有点心得,这里和大家分享下。

    我最近研究了hive的相关技术,有点心得,这里和大家分享下. https://www.cnblogs.com/sharpxiajun/archive/2013/06/02/3114180.html 首 ...

  9. 洛谷P4145——上帝造题的七分钟2 / 花神游历各国

    题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...

  10. synchronized的不足与redis分布式锁的使用

    这里是一个简单模拟秒杀的逻辑,stock和orders为两个Map,分别模拟库存表和订单表 public void orderProductMockDiffUser(String productId) ...