解析word(docx格式的)获取数据存数据库

 读取word,根据标题来区分标题1标题2还是正文,然后分别存,采用的是边读边存的形式

 

package com.zhonghui.taxdoc.webapp.facade;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFStyle;
import org.apache.poi.xwpf.usermodel.XWPFStyles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.zhonghui.taxdoc.webapp.po.DocContentWithBLOBs;
import com.zhonghui.taxdoc.webapp.po.DocWord;
import com.zhonghui.taxdoc.webapp.service.DocWordService;
import com.zhonghui.taxdoc.webapp.utils.ChineseToEnglish;
import com.zhonghui.taxdoc.webapp.vo.UserVo;

@Service
public class DocWordFacade {

    @Autowired
    private DocWordService docWordService;

    Map<Integer, Long> map = new HashMap<>();

    @Transactional(rollbackFor = Exception.class)
    public boolean readDocx(File file, UserVo user, String fileName, int type) {
        Long id = 0L;
        Long cpid = 0L;
        boolean flag = true;
        String content = "";
        InputStream in = null;
        XWPFParagraph para = null;
        try {
            // 主表
            fileName = StringUtils.substringBeforeLast(fileName, ".");
            String nameSpell = ChineseToEnglish.getPingYin(fileName);
            DocWord word = new DocWord();
            word.setDocName(fileName);
            word.setUserId(user.getId());
            word.setUserName(user.getRealname());
            word.setStatus(1);
            word.setSpell(nameSpell);
            word.setType(type);
            Long wordId = docWordService.insertWord(word);
            in = new FileInputStream(file);
            XWPFDocument doc = new XWPFDocument(in);
            XWPFStyles style = doc.getStyles();
            Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
            // initMap();
            while (iterator.hasNext()) {
                para = iterator.next();
                String pstyleId = para.getStyleID();
                if (pstyleId != null) {
                    XWPFStyle xstyle = style.getStyle(pstyleId);
                    String styleName = xstyle.getName();
                    boolean isTitle = styleName.indexOf("1") >= 0 || styleName.indexOf("2") >= 0
                            || styleName.indexOf("3") >= 0 || styleName.indexOf("4") >= 0;
                    if (flag == false && isTitle == true) {
                        DocContentWithBLOBs w = new DocContentWithBLOBs();
                        w.setWordId(wordId);
                        w.setContent(content);
                        w.setParentId(cpid);
                        w.setType(2);
                        docWordService.insertContent(w);
                        content = "";
                    }
                    if (styleName.indexOf("1") >= 0) {
                        DocContentWithBLOBs w = new DocContentWithBLOBs();
                        w.setWordId(wordId);
                        w.setTitle(para.getText());
                        w.setParentId(0L);
                        w.setType(1);
                        id = docWordService.insertContent(w);
                        initMap();
                        map.put(1, id);
                    } else if (styleName.indexOf("2") >= 0) {
                        DocContentWithBLOBs w = new DocContentWithBLOBs();
                        w.setWordId(wordId);
                        w.setTitle(para.getText());
                        w.setParentId(map.get(1));
                        w.setType(1);
                        id = docWordService.insertContent(w);
                        map.put(2, id);
                    } else if (styleName.indexOf("3") >= 0) {
                        DocContentWithBLOBs w = new DocContentWithBLOBs();
                        w.setWordId(wordId);
                        w.setType(1);
                        w.setTitle(para.getText());
                        if (!map.get(2).equals(0L)) {
                            w.setParentId(map.get(2));
                        } else {
                            w.setParentId(map.get(1));
                        }
                        id = docWordService.insertContent(w);
                        map.put(3, id);
                    } else if (styleName.indexOf("4") >= 0) {
                        DocContentWithBLOBs w = new DocContentWithBLOBs();
                        w.setWordId(wordId);
                        w.setType(1);
                        w.setTitle(para.getText());
                        if (!map.get(3).equals(0L)) {
                            w.setParentId(map.get(3));
                        } else if (!map.get(2).equals(0L)) {
                            w.setParentId(map.get(2));
                        } else {
                            w.setParentId(map.get(1));
                        }
                        id = docWordService.insertContent(w);
                        map.put(4, id);
                    } else {
                        flag = false;
                        cpid = id;
                        content = content + "\r\n" + para.getText().replaceAll("\n", "\r\n");
                    }

                } else {
                    flag = false;
                    cpid = id;
                    content = content + "\r\n" + para.getText().replaceAll("\n", "\r\n");
                }
            }
            if (flag == false) {
                DocContentWithBLOBs w = new DocContentWithBLOBs();
                w.setWordId(wordId);
                w.setContent(content);
                w.setParentId(cpid);
                w.setType(2);
                docWordService.insertContent(w);
                content = "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public void initMap() {
        map.put(0, 0L);
        map.put(1, 0L);
        map.put(2, 0L);
        map.put(3, 0L);
        map.put(4, 0L);
    }

}
 

Java解析Word文件并数据库的方法可以通过使用Apache POI库来实现。Apache POI是一个用于处理Microsoft Office文件的开源Java库。 首先,需要引入Apache POI库的依赖。可以通过在项目的构建文件(如pom.xml)中添加以下依赖来实现: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 接下来,可以使用Apache POI库提供的类来解析读取Word文件。首先,创建一个`File`对象来表示要解析Word文件,然后使用`XWPFDocument`类来加载该文件: ```java File file = new File("path/to/word/document.docx"); XWPFDocument doc = new XWPFDocument(new FileInputStream(file)); ``` 然后,可以使用`XWPFParagraph`类来获取Word文档中的段落,使用`XWPFRun`类来获取段落中的文本内容。 ```java List<XWPFParagraph> paragraphs = doc.getParagraphs(); for (XWPFParagraph paragraph : paragraphs) { List<XWPFRun> runs = paragraph.getRuns(); for (XWPFRun run : runs) { String text = run.getText(0); // 将文本内容数据库 // ... } } ``` 在循环中,可以将获取到的文本内容数据库,可以使用Java的数据库连接API(如JDBC)来实现。 此外,还可以使用`XWPFTable`类来获取Word文档中的表格,使用`XWPFTableRow`和`XWPFTableCell`类来获取表格中的行和单元格。 综上所述,上述代码片段演示了如何使用Java解析Word文件并将其数据库。具体的存储逻辑和数据库连接的实现需要根据实际情况进行调整和完善。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值