【问题标题】:Error reading word document into processing将 word 文档读入处理时出错
【发布时间】:2020-03-21 18:56:37
【问题描述】:

我正在尝试从 word 文件中读取文本数据,并找到以下指向 java 答案的链接

How read Doc or Docx file in java?

我决定编写一些代码。

import java.io.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;

String s = "C:\\Users\\admin\\Desktop\\doc.doc";

String []doc = null;


String [] func(String Loc){
  WordExtractor extractor = null;
  String s = "sketch_200318a\\";
  String [] fileData = null;
  File file = null;

        try{

            file = new File(Loc);
            FileInputStream fis = new FileInputStream(file.getAbsolutePath());

            HWPFDocument document = new HWPFDocument(fis);
            extractor = new WordExtractor(document);

            fileData = extractor.getParagraphText();

            for (int i = 0; i < fileData.length; i++){

                if (fileData[i] != null)System.out.println(fileData[i]);

            }}
            catch (Exception exep){
            //exep.printStackTrace();
        }
        return fileData;
};

void setup(){
  size(400,400);
  doc = func(s);
};

void draw(){
  background(51);

  fill(255);
  for(int i=0;i<doc.length;i++){
   text(doc[i],10,10+10*i); 
  }

};

但此代码不起作用

并产生以下错误

java.lang.NoClassDefFoundError: org/apache/commons/math3/util/ArithmeticUtils

    at org.apache.poi.poifs.property.RootProperty.setSize(RootProperty.java:59)
    at org.apache.poi.poifs.property.DirectoryProperty.<init>(DirectoryProperty.java:52)
    at org.apache.poi.poifs.property.RootProperty.<init>(RootProperty.java:31)
    at org.apache.poi.poifs.property.PropertyTable.<init>(PropertyTable.java:58)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:102)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:274)
    at org.apache.poi.hwpf.HWPFDocumentCore.verifyAndBuildPOIFS(HWPFDocumentCore.java:129)
    at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:165)
    at sketch_200318a.func(sketch_200318a.java:272)
    at sketch_200318a.setup(sketch_200318a.java:290)
    at processing.core.PApplet.handleDraw(PApplet.java:2432)
    at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
    at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.util.ArithmeticUtils
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 13 more

我是否明白我在某处或库中缺少一些依赖项?

【问题讨论】:

    标签: java ms-word processing document


    【解决方案1】:

    是的,它缺少一个库,poi 包含一个需要添加到项目代码文件夹的 lib 文件夹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      相关资源
      最近更新 更多