JavaWord导⼊(doc、docx导⼊,图⽚提取,表格处理,⽂本
处理)
废话不说,直接⼲货
⽤到的maven:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.15</version>
</dependency>
public void importStaffInfo(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException { try {
String name = OriginalFilename();
HashMap<String,Object> map = null;
String url = "";
ains("docx")){
byte [] byteArr = Bytes();
InputStream inputStream = new ByteArrayInputStream(byteArr);
XWPFDocument doc = new XWPFDocument(inputStream);
//提取表格,返回表格内容集合
map = WordUtil.tableTQDocx(doc);
//提取图⽚,返回访问链接
url = WordUtil.pictureTQDocx(doc,request);
}else ains("doc")){
File f = multipartFileToFile(file);
FileInputStream is = new FileInputStream(f);
HWPFDocument document = new HWPFDocument(is);
//提取Doc表格内容
map = WordUtil.tableTQDoc(document);
/
/提取Doc图⽚
url = WordUtil.pictureTQDoc(document,request);
}
}
}
//Docx表格及⽂本数据提取处理
public static HashMap<String, Object> tableTQDocx(XWPFDocument doc) {
//处理word表格内容
Iterator<XWPFTable> it = TablesIterator();
// 过滤前⾯不需要的表格
// if (it.hasNext()) {
/
/ it.next();
// }
// 得到需要的第⼆个表格,业务数据
HashMap<String,Object> map = new HashMap<String,Object>();
while (it.hasNext()){
XWPFTable xwpfTable = it.next();
// 读取每⼀⾏
for (int i = 0; i < Rows().size(); i++) {
XWPFTableRow row = Row(i);
if (row != null) {
//根据模板读取需要的数据单元格,从第⼆列开始读取
for (int j = 0; j < TableCells().size(); j++) {
XWPFTableCell cell = Cell(j);
XWPFTableCell cell2 = Cell(j + 1);
if (cell != null && cell2 != null) {
String cellText = Text();
String cellText2 = Text();
//这⾥对于22部分的表格进⾏单独处理
if(cellText.equals("22")){
ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
while (true){
i++;
XWPFTableRow row2 = Row(i);
String cellText8 = Cell(0).getText();
//这⾥判断:如果获取到的内容等于23部分,说明22部分内容已经获取完毕,关闭当前循环
if(cellText8.equals("23")){
i--;
break;
}
//22部分每⾏五个,直接获取五个,开启下⼀⾏
String cellText3 = Cell(1).getText();
String cellText4 = Cell(2).getText();
String cellText4 = Cell(2).getText();
String cellText5 = Cell(3).getText();
String cellText6 = Cell(4).getText();
String cellText7 = Cell(5).getText();
ArrayList<String> List2 = new ArrayList<String>(); List2.add(cellText3);
List2.add(cellText4);
List2.add(cellText5);
List2.add(cellText6);
List2.add(cellText7);
list.add(List2);
}
map.put("22",list);
break;
}
if(cellText.equals("1")){
("1") == null){
map.put("1",cellText2);
}
}
if(cellText.equals("2")){
map.put("2",cellText2);
}
ains("3")){
map.put("3",cellText2);
}
//下同
}
j++;
}
}
}
}
//提取图⽚,并保存,返回可访问路径
public static String pictureTQDocx(XWPFDocument doc, HttpServletRequest request) throws IOException {
List<XWPFPictureData> allPictures = AllPictures();
// ⼀:读取word中的照⽚ docx,把得到的data写⼊你要写⼊的⽂件
if(allPictures.size() == 0){
return "";
}
XWPFPictureData picture = (0);
byte[] data = Data();
String oriFileName = FileName();
//图⽚重命名
String fileSuffix = oriFileName.substring(oriFileName.lastIndexOf(".") + 1); // 后缀
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String uuid = UUID.randomUUID().toString().replaceAll("-","");
String noZh_CN_FileName = uuid + format.format(new Date()) + "." + fileSuffix;
//图⽚保存
String returnUrl = Scheme() + "://" + ServerName() + ":" + ServerPort() + ContextPath();//存储路径 String dirPath = Session().getServletContext().getRealPath("images"); //⽂件存储位置
String targetPath = dirPath + "/" + noZh_CN_FileName;
String url = returnUrl + "/images/" + noZh_CN_FileName;
//如果⽬录不存在
File fileDirPath = new File(dirPath);
if (!ists()) {
//创建⽬录
fileDirPath.mkdirs();
}
replaceall()File targetFile = new File(targetPath);
if (!ists()) {
if (!ParentFile().exists()) {
}
}
FileOutputStream out = new FileOutputStream(targetFile);
out.write(data);
out.close();
return url;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论