poi 顺序解析word_JavaPOI解析word提取数据到excel
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&¶text2.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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell脚本给mysql创建表_she
- 下一篇: java美元兑换,(Java实现) 美元