|
@@ -1,16 +1,19 @@
|
|
package cn.cslg.pas.common.utils;
|
|
package cn.cslg.pas.common.utils;
|
|
|
|
|
|
-import cn.hutool.core.collection.IterUtil;
|
|
|
|
-import cn.hutool.poi.excel.ExcelUtil;
|
|
|
|
-import io.swagger.v3.oas.models.security.SecurityScheme;
|
|
|
|
|
|
+import cn.cslg.pas.domain.PatentData;
|
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFPicture;
|
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFShape;
|
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
|
+import org.apache.poi.ooxml.POIXMLDocumentPart;
|
|
|
|
+import org.apache.poi.ss.usermodel.PictureData;
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
|
+import org.apache.poi.xssf.usermodel.*;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
-import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
@@ -19,83 +22,152 @@ import java.util.Map;
|
|
* @Author xiexiang
|
|
* @Author xiexiang
|
|
* @Date 2023/5/30
|
|
* @Date 2023/5/30
|
|
*/
|
|
*/
|
|
|
|
+@Service
|
|
public class ReadExcelUtils {
|
|
public class ReadExcelUtils {
|
|
- //每次读取Excel一行
|
|
|
|
- public static Map ReadExcelOneRow(String filePath, Integer row) {
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 检测Excel文件合法性
|
|
|
|
+ *
|
|
|
|
+ * @param filePath 文件路径
|
|
|
|
+ * @return 返回文件总行数
|
|
|
|
+ */
|
|
|
|
+ public static Integer textExcel(String filePath) throws IOException {
|
|
|
|
+ //判断文件是否存在
|
|
|
|
+ if (filePath == null || filePath.equals("")) {
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+ File file = new File(filePath);
|
|
|
|
+ if (!file.exists()) {
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 检测是否为excel文件
|
|
|
|
+ if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx") && !filePath.endsWith(".XLS") && !filePath.endsWith(".XLSX")) {
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ InputStream inputStream = new FileInputStream(file);
|
|
|
|
+ //XSSFWorkbook可以处理xlsx文件
|
|
|
|
+ Workbook workbook = null;
|
|
|
|
+ //当文件以.xls结尾时
|
|
|
|
+ if (filePath.endsWith(".xls") || filePath.endsWith(".XLS")) {
|
|
|
|
+ workbook = new HSSFWorkbook(inputStream);
|
|
|
|
+ } else if (filePath.endsWith(".xlsx") || filePath.endsWith(".XLSX")) {
|
|
|
|
+ workbook = new XSSFWorkbook(inputStream);
|
|
|
|
+ }
|
|
|
|
+ //读取第几个sheet
|
|
|
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
|
|
+ //读取总行数
|
|
|
|
+ int rows = sheet.getPhysicalNumberOfRows();
|
|
|
|
+ if (rows <= 0) {
|
|
|
|
+ return -2;
|
|
|
|
+ }
|
|
|
|
+ Row firstRow = sheet.getRow(0);
|
|
|
|
+ if (!firstRow.getCell(0).getStringCellValue().equals("公开(公告)号")) {
|
|
|
|
+ return -2;
|
|
|
|
+ }
|
|
|
|
+ return rows - 1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取一行专利的全部数据(专利内容数据 + 摘要附图)
|
|
|
|
+ *
|
|
|
|
+ * @param filePath Excel文件路径
|
|
|
|
+ * @param row 行数
|
|
|
|
+ * @return 返回装载专利数据(专利内容数据 + 摘要附图)的对象
|
|
|
|
+ */
|
|
|
|
+ public static PatentData readExcelOneRow(String filePath, Integer row) {
|
|
|
|
+ //返回最终结果的对象
|
|
|
|
+ PatentData patentData = new PatentData();
|
|
|
|
+ //装载专利数据(除了摘要附图)的map:(key:表头如 "公开(公告)号" value:表头对应内容如 "CN1307082B")
|
|
Map<Object, Object> map = new HashMap<>();
|
|
Map<Object, Object> map = new HashMap<>();
|
|
|
|
+ //装载摘要附图的对象
|
|
|
|
+ PictureData pictureData = null;
|
|
|
|
+
|
|
File file = new File(filePath);
|
|
File file = new File(filePath);
|
|
try {
|
|
try {
|
|
InputStream inputStream = new FileInputStream(file);
|
|
InputStream inputStream = new FileInputStream(file);
|
|
- //XSSFWorkbook可以处理xlsx文件
|
|
|
|
- Workbook workbook =null;
|
|
|
|
|
|
+ //POI可以处理Excel文件
|
|
|
|
+ Workbook workbook = null;
|
|
//当文件以.xls结尾时
|
|
//当文件以.xls结尾时
|
|
- if(filePath.endsWith(".xls")||filePath.endsWith(".XLS")) {
|
|
|
|
- workbook = new HSSFWorkbook(inputStream);
|
|
|
|
- }
|
|
|
|
- else if(filePath.endsWith(".xlsx")||filePath.endsWith(".XLSX"))
|
|
|
|
- {
|
|
|
|
- workbook = new XSSFWorkbook(inputStream);
|
|
|
|
|
|
+ if (filePath.endsWith(".xls") || filePath.endsWith(".XLS")) {
|
|
|
|
+ workbook = new HSSFWorkbook(inputStream);
|
|
|
|
+ } else if (filePath.endsWith(".xlsx") || filePath.endsWith(".XLSX")) {
|
|
|
|
+ workbook = new XSSFWorkbook(inputStream);
|
|
}
|
|
}
|
|
|
|
+
|
|
//读取第几个sheet
|
|
//读取第几个sheet
|
|
Sheet sheet = workbook.getSheetAt(0);
|
|
Sheet sheet = workbook.getSheetAt(0);
|
|
//读取总行数
|
|
//读取总行数
|
|
int rows = sheet.getPhysicalNumberOfRows();
|
|
int rows = sheet.getPhysicalNumberOfRows();
|
|
- if(rows - 1 <= row){
|
|
|
|
|
|
+ if (rows - 1 <= row) {
|
|
ThrowException.throwXiaoShiException("row超出Excel文档中数量");
|
|
ThrowException.throwXiaoShiException("row超出Excel文档中数量");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ //开始装载专利数据
|
|
Row firstRow = sheet.getRow(0);
|
|
Row firstRow = sheet.getRow(0);
|
|
Row needRow = sheet.getRow(row);
|
|
Row needRow = sheet.getRow(row);
|
|
//读取第一行的时候会多读一列
|
|
//读取第一行的时候会多读一列
|
|
- Integer firstColumns = firstRow.getLastCellNum() - 0;
|
|
|
|
- for(int i = 0; i < firstColumns; i++){
|
|
|
|
- map.put(firstRow.getCell(i),needRow.getCell(i));
|
|
|
|
|
|
+ int firstColumns = firstRow.getLastCellNum() - 0;
|
|
|
|
+ for (int i = 0; i < firstColumns; i++) {
|
|
|
|
+ map.put(firstRow.getCell(i) + "", needRow.getCell(i) + "");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //开始装载专利摘要附图(判断用07还是03的方法获取图片)
|
|
|
|
+ if (filePath.endsWith(".xls") || filePath.endsWith(".XLS")) {
|
|
|
|
+ pictureData = getPictures1((HSSFSheet) sheet, row - 1);
|
|
|
|
+ } else if (filePath.endsWith(".xlsx") || filePath.endsWith(".XLSX")) {
|
|
|
|
+ pictureData = getPictures2((XSSFSheet) sheet, row - 1);
|
|
}
|
|
}
|
|
- } catch (IOException e) {
|
|
|
|
|
|
+ workbook.close();
|
|
|
|
+
|
|
|
|
+ //返回结果对象装载结果
|
|
|
|
+ patentData.setMap(map);
|
|
|
|
+ patentData.setPictureData(pictureData);
|
|
|
|
+
|
|
|
|
+ } catch (IOException e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
- return map;
|
|
|
|
|
|
+
|
|
|
|
+ return patentData;
|
|
}
|
|
}
|
|
|
|
|
|
- //检测Excel文件合法性
|
|
|
|
- public static Integer textExcel(String filePath) throws IOException {
|
|
|
|
- //判断文件是否存在
|
|
|
|
- if(filePath == null && filePath == ""){
|
|
|
|
- return -1;
|
|
|
|
- }
|
|
|
|
- File file = new File(filePath);
|
|
|
|
- if(!file.exists())
|
|
|
|
- {
|
|
|
|
- return -1;
|
|
|
|
- }
|
|
|
|
- // 检测是否是excel文件
|
|
|
|
- if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx") && !filePath.endsWith(".XLS") && !filePath.endsWith(".XLSX")) {
|
|
|
|
- return -1;
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 03版本Excel取附图
|
|
|
|
+ *
|
|
|
|
+ * @param sheet Excel工作簿
|
|
|
|
+ * @return 返回附图map
|
|
|
|
+ */
|
|
|
|
+ public static PictureData getPictures1(HSSFSheet sheet, Integer row) throws IOException {
|
|
|
|
+ if (sheet.getDrawingPatriarch() != null) {
|
|
|
|
+ List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();
|
|
|
|
+ HSSFShape shape = list.get(row);
|
|
|
|
+ if (shape instanceof HSSFPicture) {
|
|
|
|
+ HSSFPicture picture = (HSSFPicture) shape;
|
|
|
|
+ return picture.getPictureData();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
|
|
- InputStream inputStream = new FileInputStream(file);
|
|
|
|
- //XSSFWorkbook可以处理xlsx文件
|
|
|
|
- Workbook workbook =null;
|
|
|
|
- //当文件以.xls结尾时
|
|
|
|
- if(filePath.endsWith(".xls")||filePath.endsWith(".XLS")) {
|
|
|
|
- workbook = new HSSFWorkbook(inputStream);
|
|
|
|
- }
|
|
|
|
- else if(filePath.endsWith(".xlsx")||filePath.endsWith(".XLSX"))
|
|
|
|
- {
|
|
|
|
- workbook = new XSSFWorkbook(inputStream);
|
|
|
|
- }
|
|
|
|
- //读取第几个sheet
|
|
|
|
- Sheet sheet = workbook.getSheetAt(0);
|
|
|
|
- //读取总行数
|
|
|
|
- int rows = sheet.getPhysicalNumberOfRows();
|
|
|
|
- if(rows<=0)
|
|
|
|
- {
|
|
|
|
- return -2;
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 07版本Excel取附图
|
|
|
|
+ *
|
|
|
|
+ * @param sheet Excel工作簿
|
|
|
|
+ * @return 返回附图map
|
|
|
|
+ */
|
|
|
|
+ public static PictureData getPictures2(XSSFSheet sheet, Integer row) throws IOException {
|
|
|
|
+ List<POIXMLDocumentPart> list = sheet.getRelations();
|
|
|
|
+ for (POIXMLDocumentPart part : list) {
|
|
|
|
+ if (part instanceof XSSFDrawing) {
|
|
|
|
+ XSSFDrawing drawing = (XSSFDrawing) part;
|
|
|
|
+ List<XSSFShape> shapes = drawing.getShapes();
|
|
|
|
+ //获取指定行row的图片形状
|
|
|
|
+ XSSFShape shape = shapes.get(row);
|
|
|
|
+ XSSFPicture picture = (XSSFPicture) shape;
|
|
|
|
+ return picture.getPictureData();
|
|
}
|
|
}
|
|
- Row firstRow = sheet.getRow(0);
|
|
|
|
- if(!firstRow.getCell(0).getStringCellValue().equals("公开(公告)号")){
|
|
|
|
- return -2;
|
|
|
|
- }
|
|
|
|
- return rows-1;
|
|
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|