java提取word目录并生成层级文件夹

Shaka 17天前 ⋅ 30 阅读

今天有个需求,根据word目录生成文件夹.
百度搜了下,java的没有,只有C#的,无奈只能自己写了.还好比较简单.
word结构
|─ 一级目录
| |─ 二级目录
| | |─ 三级目录
| | | |─ 四级目录
| | | | |─ 五级目录
| | | | | |─ 六级目录
| | | | | | |─ 七级目录
.........
@Test
public void test1(){
XWPFDocument docx = null;
String path = "C:\\Users\\hailong.zhao\\Desktop\\1.docx";
try {
OPCPackage pack = POIXMLDocument.openPackage(path);
docx = new XWPFDocument(pack);
} catch (Exception e) {
e.printStackTrace();
}
List list = docx.getParagraphs();
int i = 0;
String nowPath = "C:\\Users\\hailong.zhao\\Desktop\\1";
File file = new File(nowPath);
for(XWPFParagraph li:list){
// System.out.print(li.getText().replaceAll(" ","").indexOf("─"));
String runStr = li.getText();
int j = runStr.replaceAll(" ", "").indexOf("─");
String name = runStr.substring(runStr.indexOf("─")+1);
if(j>i){
file = new File(file.getAbsolutePath()+"\\"+name);
}else if(j<=i){
int k = i-j+1;
for(int l = 0;l<k;l++){
file = file.getParentFile();
}
file = new File(file.getAbsolutePath()+"\\"+name);
}
if(!file.exists()){
file.mkdirs();
}
i=j;
nowPath = file.getAbsolutePath();
// System.out.println(li.getText());
System.out.println(nowPath);
}
}


注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: