java抓取word值_在JAVA下实现数据库对word文件的读取与字段的提取

这篇博客介绍了如何使用Java的POI库来读取Word(2003和2007版本)文件的内容,并从字段中抓取信息,以便将数据导入数据库。博主通过示例代码展示了如何处理包含多个学院课程资料的Word文件,提取如课程名称、课程代码等关键字段,然后将这些信息写入数据库。
摘要由CSDN通过智能技术生成

实现excel中的数据导入数据库,在java或是C#下是不难实现的,即使想实现在txt中的数据导入也不是什么难事,但是,最近接了个任务,学校要建一个英文版的教学平台,然后各个学院的课程资料与简介什么的都是英文的,学校有20个学院,每个学院多的有两百来个科目,少的也有百八十种,可他偏偏就是个word,而且,做的格式很不规范。这可愁着我了,我首先想到的是POI,于是google了一下,原来真的很容易实现,这个后面的代码可以发上去,可以实现03版,和07版的。差别主要是jar包的问题。03的要3个jar包,07的需要7个jar包。

* POI 读取 word 2003 和 word 2007 中文字内容的测试类

* @createDate 2009-07-25

* @author Carl He

*/

public class Test {

public static void main(String[] args) {

try {

word 2003: 图片不会被读取

InputStream is = new FileInputStream(new File("files\\2003.doc"));

WordExtractor ex = new WordExtractor(is);//is是WORD文件的InputStream

String text2003 = ex.getText();

System.out.println(text2003);

//对字符串进行分解

//word 2007 图片不会被读取, 表格中的数据会被放在字符串的最后

OPCPackage opcPackage = POIXMLDocument.openPackage("files\\2007.docx");

POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);

String text2007 = extractor.getText();

System.out.println(text2007);

} catch (Exception e) {

e.printStackTrace();

}

}

}

然后,重要的问题是,如何从word的字段中抓去文件才是关键,因为他们提供的word文件并不是excel,并不能直接导入,我还是果断上一个word文件吧,这样好理解:

Course Description ofBiochemistry

Course Name: Biochemistry Nature of Course:Compulsory course

Course Code: B1700025 Total Credits: 5.0

Total Credit Hours:80 Lecture Hours:80

Experimental Hours: 0 Oriented Majors: Bioscience, Biotechnology

Prerequisite Courses:

Penner: Validator(s):

Briefing of Course Content:

Biochemistry is a science exploring thechemical compositions and chemical reactions during life activitiesof livingorganisms. It is an important compulsive fundamental course for undergraduates majoring in bioscience and biotechnology. The main content of this course includes 1. The structure, function and the relationship between the structure and function of biological macromolecule such as protein and nucleic acid; 2. The metabolisms and regulation of biological macromolecules including carbohydrate, lipid, protein, nucleic acid etc.; 3. The transfer and expression of genetic information.

import java.io.File;

import java.util.ArrayList;

public class Directory {

private ArrayList nameList = new ArrayList();

private static String dirName = "d:\\Eclip

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值