JAVA读取word内表格的图片,将几行几列写入图片名(非POI) ...

打印 上一主题 下一主题

主题 1012|帖子 1012|积分 3040

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. package org.springblade;
  2. import com.spire.doc.Document;
  3. import com.spire.doc.Section;
  4. import com.spire.doc.TableCell;
  5. import com.spire.doc.TableRow;
  6. import com.spire.doc.documents.Paragraph;
  7. import com.spire.doc.fields.DocPicture;
  8. import com.spire.doc.interfaces.ITable;
  9. import javax.imageio.ImageIO;
  10. import java.awt.image.RenderedImage;
  11. import java.io.BufferedWriter;
  12. import java.io.File;
  13. import java.io.FileWriter;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. public class GetTable {
  19.         public static void main(String[] args)throws IOException {
  20.                 //加载Word测试文档
  21.                 Document doc = new Document();
  22.                 doc.loadFromFile("E:\\测试导入\\导入模板.docx");
  23.                 //获取第一节
  24.                 Section section = doc.getSections().get(0);
  25.                 //获取第一个表格
  26.                 ITable table = section.getTables().get(0);
  27.                 //创建txt文件(用于写入表格中提取的文本)
  28.                 String output = "ReadTextFromTable.txt";
  29.                 File textfile = new File(output);
  30.                 if (textfile.exists())
  31.                 {
  32.                         textfile.delete();
  33.                 }
  34.                 textfile.createNewFile();
  35.                 FileWriter fw = new FileWriter(textfile, true);
  36.                 BufferedWriter bw = new BufferedWriter(fw);
  37.                 //创建List
  38.                 List images = new ArrayList();
  39.                 HashMap<String, Object> photoMap = new HashMap<>();
  40.                 //遍历表格中的行
  41.                 for (int i = 0; i < table.getRows().getCount(); i++)
  42.                 {
  43.                         TableRow row = table.getRows().get(i);
  44.                         //遍历每行中的单元格
  45.                         for (int j = 0; j < row.getCells().getCount(); j++)
  46.                         {
  47.                                 TableCell cell = row.getCells().get(j);
  48.                                 //遍历单元格中的段落
  49.                                 for (int k = 0; k < cell.getParagraphs().getCount(); k++)
  50.                                 {
  51.                                         Paragraph paragraph = cell.getParagraphs().get(k);
  52.                                         bw.write(paragraph.getText() + "\t");//获取文本内容
  53.                                         //遍历段落中的所有子对象
  54.                                         for (int x = 0; x < paragraph.getChildObjects().getCount(); x++)
  55.                                         {
  56.                                                 Object object = paragraph.getChildObjects().get(x);
  57.                                                 //判定对象是否为图片
  58.                                                 if (object instanceof DocPicture)
  59.                                                 {
  60.                                                         //获取图片
  61.                                                         DocPicture picture = (DocPicture) object;
  62.                                                         images.add(picture.getImage());
  63.                                                         //得到当前表格的行和列
  64.                                                         String photoName = String.format("第%s行第%s列的图片.png", i + 1, j);
  65.                                                         photoMap.put(photoName,picture.getImage());
  66.                                                 }
  67.                                         }
  68.                                 }
  69.                         }
  70.                         bw.write("\r\n");//写入内容到txt文件
  71.                 }
  72.                 bw.flush();
  73.                 bw.close();
  74.                 fw.close();
  75.                 //将图片以PNG文件格式保存
  76. //                for (int z = 0; z < images.size(); z++)
  77. //                {
  78. //                        File imagefile = new File(String.format("提取的表格图片-%d.png", z));
  79. //                        ImageIO.write((RenderedImage) images.get(z), "PNG", imagefile);
  80. //                }
  81.                 for (String s : photoMap.keySet()) {
  82.                         File imageFile = new File(s);
  83.                         ImageIO.write((RenderedImage) photoMap.get(s), "PNG", imageFile);
  84.                 }
  85.         }
  86. }
复制代码
在此之前必须引入maven
  1.       <dependency>
  2.             <groupId>e-iceblue</groupId>
  3.             <artifactId>spire.doc.free</artifactId>
  4.             <version>5.2.0</version>
  5.         </dependency>
  6.     <repositories>
  7.         <repository>
  8.             <id>com.e-iceblue</id>
  9.             <name>e-iceblue</name>
  10.             <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
  11.         </repository>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

刘俊凯

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表