日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

poi 顺序解析word_JavaPOI解析word提取数据到excel

發(fā)布時間:2025/3/12 java 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poi 顺序解析word_JavaPOI解析word提取数据到excel 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Java POI解析Word提取數(shù)據(jù)存儲在Excel

一、了解POI

POI以前有了解,這次需求是解析word讀取其中標(biāo)題,還有內(nèi)容賽選獲取自己想要的內(nèi)容

經(jīng)過兩天的學(xué)習(xí),開始熟悉Java這么讀取word和解析。

本文中運用是讀取整個頁面模塊的range,通過對range里面的數(shù)據(jù)進行篩選,獲取自己想要的數(shù)據(jù)。

主要是了解POI的數(shù)據(jù)調(diào)用的解析。

想要實現(xiàn)的效果

以下測試的IP或是域名都是隨便找不同類型:IP+port /url/http://www.XXX.com/www.xxx.net:8080等等存在的url組合。

目前需求是抓取這個幾個關(guān)鍵的內(nèi)容。

二、POI解析word

maven添加jar依賴

全部的POI的依賴

org.apache.poi

poi

3.17

org.apache.poi

poi-ooxml

3.17

org.apache.poi

poi-scratchpad

3.17

org.apache.poi

ooxml-schemas

1.1

org.apache.poi

poi-ooxml-schemas

3.17

關(guān)鍵代碼

public class GetWord1 {

public static void main(String[] args) {

try {

InputStream is = new FileInputStream(new File("path")); //需要將文件路更改為word文檔所在路徑。

POIFSFileSystem fs = new POIFSFileSystem(is);

HWPFDocument document = new HWPFDocument(fs);

Range range = document.getRange();

CharacterRun run1 = null;//用來存儲第一行內(nèi)容的屬性

CharacterRun run2 = null;//用來存儲第二行內(nèi)容的屬性

int q=1;

for (int i = 0; i < range.numParagraphs()-2; i++) {

Paragraph para1 = range.getParagraph(i);// 獲取第i段

Paragraph para2 = range.getParagraph(i+1);// 獲取第i段

int t=i; //記錄當(dāng)前分析的段落數(shù)

String paratext1 = para1.text(); //當(dāng)前段落和下一段

String paratext2 = para2.text();

run1=para1.getCharacterRun(0);

run2=para2.getCharacterRun(0);

if (paratext1.length() > 0&&paratext2.length() > 0) {

//這個if語句為的是去除大標(biāo)題,連續(xù)三個段落字體大小遞減就跳過

if(run1.getFontSize()>run2.getFontSize()&&run2.getFontSize()>range.getParagraph(i+2).getCharacterRun(0).getFontSize()) {

break;

}

//連續(xù)兩段字體格式不同

if(run1.getFontSize()>run2.getFontSize()) {

content=paratext2;

run1=run2; //從新定位run1 run2

run2=range.getParagraph(t+2).getCharacterRun(0);

t=t+1;

while(run1.getFontSize()==run2.getFontSize()) {

//連續(xù)的相同

Content+=range.getParagraph(t+1).text();

if(content.matches("是否系統(tǒng)自身滲透結(jié)果")) break;

if(content.contains(":")||content.contains("/")){//如果是http或者是IP+端口形式

word.setName(getDomain(content));//ip

}else{//純IP形式

word.setName(content);//ip

}

word.setAsset_name(paratext1);//標(biāo)題

run1=run2;

run2=range.getParagraph(t+i).getCharacterRun(0);

t++;

……..

}

………

}

if(paratext1.matches("中危")||paratext1.matches("很高")||paratext1.matches("低危")){

list2.add(paratext1);

}

}

}

if(word1.getAsset_name()==wordname.getAsset_name())){

…..

wordend.setId(num);

wordend.setName(word1.getName());

wordend.setAsset_name(word1.getAsset_name());

if(wordname.getAvailability_assignment().equals("很高")){

wordend.setAvailability_assignment("5");

}

if(wordname.getAvailability_assignment().equals("高危")){

wordend.setAvailability_assignment("4");

}

if(wordname.getAvailability_assignment().equals("中危")){

wordend.setAvailability_assignment("3");

}

if(wordname.getAvailability_assignment().equals("低危")){

wordend.setAvailability_assignment("2");

}

wordend.setRisk("網(wǎng)絡(luò)攻擊");

wordmax.add(wordend);

num++;

……

}

}

}

System.out.println("------------------Finished-詳情查看-中間文檔轉(zhuǎn)excel.xlsx------------------");

//Excel無模版導(dǎo)出

ExcelUtil.getInstar ().exportl(wordmax, Word.class, "中間文檔轉(zhuǎn)excel.xlsx");

} catch (Exception e) {

e.printStackTrace();

}

}

} }

其中有URl獲取IP或者Domain

public class Test{

public static void main(String[] args){

String url1 = "192.168.1.100/admin";

System.out.println(getDomain(url));

System.out.println(getDomain(url1));

}

private static String getDomain(String url){

String regex = "^(?:[a-zA-Z]+[.])?(\\w+([.]\\w+)*)/.*$";

Matcher matcher = Pattern.compile(regex).matcher(url);

String result = null;

if(matcher.find()){

result = matcher.group(1);

}

return result;

}

}

運行結(jié)果:

完整的測試數(shù)據(jù)

輸出到excel

GUI界面化數(shù)據(jù)操作完成和輸出excel

三、總結(jié)

1、接觸POI三天左右,主要是通過對文檔的整個的range遍歷獲取數(shù)據(jù),再通過自己的不同數(shù)據(jù)的需求進行篩選和數(shù)據(jù)處理。

2、相比HTml獲取數(shù)據(jù)java獲取word解析里面的數(shù)據(jù)更加的難一些,html里面有標(biāo)簽,可以通過操作js、css、html的標(biāo)簽來實現(xiàn)數(shù)據(jù)的獲取,但是word沒有特定的標(biāo)簽去獲取,這個只能通過遍歷,對比文本的大小、文本的字體等等相關(guān)信息。

3、這次word解析,結(jié)合上次的html的解析獲取數(shù)據(jù),對應(yīng)java的操作不同的類型的文件流有了新的認識。

四、參考文獻

?公眾號:

thelostworld:

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的poi 顺序解析word_JavaPOI解析word提取数据到excel的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。