Code compilation tool: IntelliJ IDEA
<repositories> <repository> <id>com.e-iceblue</id> <url>http://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupid> e-iceblue </groupid> <artifactid>free.spire.doc</artifactid> <version>3.9.0</version> </dependency> </dependencies>
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.DocPicture; import com.spire.doc.interfaces.ITable; import javax.imageio.ImageIO; import java.awt.image.RenderedImage; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class GetTable { public static void main(String[] args)throws IOException { //加载Word测试文档 Document doc = new Document(); doc.loadFromFile("inputfile.docx"); //获取第一节 Section section = doc.getSections().get(0); //获取第一个表格 ITable table = section.getTables().get(0); //创建txt文件(用于写入表格中提取的文本) String output = "ReadTextFromTable.txt"; File textfile = new File(output); if (textfile.exists()) { textfile.delete(); } textfile.createNewFile(); FileWriter fw = new FileWriter(textfile, true); BufferedWriter bw = new BufferedWriter(fw); //创建List List images = new ArrayList(); //遍历表格中的行 for (int i = 0; i
Text data reading result:
Image reading result:
The above is the detailed content of How to read text and pictures in Word tables using Java. For more information, please follow other related articles on the PHP Chinese website!