Java POI导入word, 带图片

1.导入文件示例,word中简历表格模板

 

 

2.代码示例分两部分,一部分读取图片

/**
* 导入word(基本信息,word格式)
* @param staffId
* @param baseInfoFile
*/
void importStaffInfo(Integer staffId, MultipartFile file);

-- 读取图片
        InputStream inputStream = baseInfoFile.getInputStream();
            XWPFDocument doc = new XWPFDocument(inputStream);
            // 一:读取word中的照片 docx,把得到的data写入你要写入的文件
            List<XWPFPictureData> allPictures = doc.getAllPictures();
            for (XWPFPictureData picture : allPictures) {
                byte[] data = picture.getData();
                String oriFileName = picture.getFileName();
          // 自己定义需要写入的文件地址 String targetPath
= ymlConfig.getHeadImagePath() + staffId + oriFileName.substring(oriFileName.lastIndexOf(".")); File targetFile = new File(targetPath); if (!targetFile.exists()) { if (!targetFile.getParentFile().exists()) { targetFile.getParentFile().mkdirs(); } targetFile.createNewFile(); } FileOutputStream out = new FileOutputStream(targetFile); out.write(data); out.close(); }

-- 读取表格信息

Iterator<XWPFTable> it = doc.getTablesIterator();
// 过滤前面不需要的表格
if (it.hasNext()) {
   it.next();
}
// 得到需要的第二个表格,业务数据
if (it.hasNext()) {
XWPFTable xwpfTable = it.next();
// 读取每一行
for (int i = 0; i < xwpfTable.getRows().size(); i++) {
XWPFTableRow row = xwpfTable.getRow(i);
if (row != null) {
//根据模板读取需要的数据单元格,从第二列开始读取
for (int j = 1; j < row.getTableCells().size(); j++) {
XWPFTableCell cell = row.getCell(j);
if (cell != null) {
String cellText = cell.getText();
                   System.out.println();
                  }
            }
}
}
}

 

posted @ 2019-11-20 16:26  Mark_Luo  阅读(7173)  评论(0编辑  收藏  举报