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();
}
}
}
}
}