poi 顺序解析word_JavaPOI解析word提取数据到excel
Java POI解析Word提取數據存儲在Excel
一、了解POI
POI以前有了解,這次需求是解析word讀取其中標題,還有內容賽選獲取自己想要的內容
經過兩天的學習,開始熟悉Java這么讀取word和解析。
本文中運用是讀取整個頁面模塊的range,通過對range里面的數據進行篩選,獲取自己想要的數據。
主要是了解POI的數據調用的解析。
想要實現的效果
以下測試的IP或是域名都是隨便找不同類型:IP+port /url/http://www.XXX.com/www.xxx.net:8080等等存在的url組合。
目前需求是抓取這個幾個關鍵的內容。
二、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
關鍵代碼
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;//用來存儲第一行內容的屬性
CharacterRun run2 = null;//用來存儲第二行內容的屬性
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; //記錄當前分析的段落數
String paratext1 = para1.text(); //當前段落和下一段
String paratext2 = para2.text();
run1=para1.getCharacterRun(0);
run2=para2.getCharacterRun(0);
if (paratext1.length() > 0&¶text2.length() > 0) {
//這個if語句為的是去除大標題,連續三個段落字體大小遞減就跳過
if(run1.getFontSize()>run2.getFontSize()&&run2.getFontSize()>range.getParagraph(i+2).getCharacterRun(0).getFontSize()) {
break;
}
//連續兩段字體格式不同
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()) {
//連續的相同
Content+=range.getParagraph(t+1).text();
if(content.matches("是否系統自身滲透結果")) break;
if(content.contains(":")||content.contains("/")){//如果是http或者是IP+端口形式
word.setName(getDomain(content));//ip
}else{//純IP形式
word.setName(content);//ip
}
word.setAsset_name(paratext1);//標題
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("網絡攻擊");
wordmax.add(wordend);
num++;
……
}
}
}
System.out.println("------------------Finished-詳情查看-中間文檔轉excel.xlsx------------------");
//Excel無模版導出
ExcelUtil.getInstar ().exportl(wordmax, Word.class, "中間文檔轉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;
}
}
運行結果:
完整的測試數據
輸出到excel
GUI界面化數據操作完成和輸出excel
三、總結
1、接觸POI三天左右,主要是通過對文檔的整個的range遍歷獲取數據,再通過自己的不同數據的需求進行篩選和數據處理。
2、相比HTml獲取數據java獲取word解析里面的數據更加的難一些,html里面有標簽,可以通過操作js、css、html的標簽來實現數據的獲取,但是word沒有特定的標簽去獲取,這個只能通過遍歷,對比文本的大小、文本的字體等等相關信息。
3、這次word解析,結合上次的html的解析獲取數據,對應java的操作不同的類型的文件流有了新的認識。
四、參考文獻
?公眾號:
thelostworld:
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的poi 顺序解析word_JavaPOI解析word提取数据到excel的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell脚本给mysql创建表_she
- 下一篇: form表单中根据值判断是否disabl