poi 读取word 遍历表格和单元格中的图片
背景
项目需要解析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 遍历表格和单元格中的图片的更多相关文章
- 如何在excel单元格中插入图片批注
在excel单元格中插入图片批注的方法: 1.选定要插入图片的单元格,然后右键选择插入批注. 2.然后会插入一个批注框,为了不影响图片效果,可以将批注文字都删除.然后鼠标移动到批注框边角再右键. 3. ...
- Aspose.Word 操作word复杂表格 拆分单元格 复制行 插入行 文字颜色
private void button3_Click(object sender, EventArgs e) { object savePathWord =&q ...
- Swift - 给表格的单元格UITableViewCell添加图片,详细文本标签
表格UITableView中,每一单元格都是一个UITableViewCell.其支持简单的自定义,比如在单元格的内部,添加图片和详细文本标签. 注意UITableViewCell的style: (1 ...
- c#在Excel指定单元格中插入图片
方法一: /// 将图片插入到指定的单元格位置,并设置图片的宽度和高度./// 注意:图片必须是绝对物理路径/// </summary>/// <param name="R ...
- POI读取带有公式的Excel单元格-xssf
if(CellType.FORMULA == row.getCell(j).getCellTypeEnum()) { try { cellValue = String.valueOf(row.getC ...
- jQuery,遍历表格每个单元格数据。
<table class="table table-hover table-bordered"> <thead> <tr> <th > ...
- 使用POI创建word表格合并单元格兼容wps
poi创建word表格合并单元格代码如下: /** * @Description: 跨列合并 */ public void mergeCellsHorizontal(XWPFTable table, ...
- 使用POI创建word表格-在表格单元格中创建子表格
要实现的功能如下:表格中的单元格中有子表格 实现代码如下: XWPFParagraph cellPara = row.getCell(j).getParagraphArray(0); //row.ge ...
- 将Word表格中单元格中的文字替换成对应的图片
示例 原文件结构: 替换后文档结构: 软件截图: 代码: using System;using System.Collections.Generic;using System.ComponentMod ...
随机推荐
- WebGL高级编程:开发Web3D图形 PDF(中文版带书签)
WebGL高级编程:开发Web3D图形 目录 WebGL简介11.1 WebGL基础11.2 浏览器3D图形吸引人的原因21.3 设计一个图形API31.3.1 即时模式API31.3.2 保留模式A ...
- Python - Django - 组件
网站中通常会有一个导航条,如下图 这个导航条在很多页面都会存在 可以把导航条做成一个组件,让要显示导航条的网页包含 导航条组件 nav.html: <h1>假装这是一个导航条</h1 ...
- (二十三)IDEA 构建一个springboot工程,以及可能遇到的问题
一.下载安装intellij IEDA 需要破解 二.创建springboot工程 其他步骤省略,创建好的工程结构如下图: 三.配置springoboot工程 3.1 如上图src/main目录下只有 ...
- Win10安装Oracle Database 18c (18.3)
下载链接:https://www.oracle.com/technetwork/cn/database/enterprise-edition/downloads/index.html 我这里选择最新的 ...
- 03点睛Spring MVC 4.1-REST
转发:https://www.iteye.com/blog/wiselyman-2214290 3.1 REST REST:Representational State Transfer; REST是 ...
- Nginx负载均衡-如何自定义URL中的hash key
"例如请求的url为http://www.a.com/{path_var1}/{path_var2}path_var1和path_var2是两个path variable如果现在只想根据pa ...
- 【网络开发】winsock组播
https://my.oschina.net/lopo/blog/260685 //客户端 #include <winsock2.h> #include <stdio.h> # ...
- shell学习及脚步编写
目录: shell基础变量逻辑运算符 scp基础用法脚本 while+for+case基础用法脚本 内核优化脚本 自动修改本机ip脚本 for+case 查询日志脚本 一键yum安装lamp脚本 源码 ...
- LeetCode 64. 最小路径和(Minimum Path Sum) 20
64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...
- 综述论文翻译:A Review on Deep Learning Techniques Applied to Semantic Segmentation
近期主要在学习语义分割相关方法,计划将arXiv上的这篇综述好好翻译下,目前已完成了一部分,但仅仅是尊重原文的直译,后续将继续完成剩余的部分,并对文中提及的多个方法给出自己的理解. 论文地址:http ...