|
@@ -23,31 +23,32 @@ public class ReadExcelUtils {
|
|
/**
|
|
/**
|
|
* 检测Excel文件合法性
|
|
* 检测Excel文件合法性
|
|
*
|
|
*
|
|
- * @param filePath 文件路径
|
|
|
|
|
|
+ * @param tempFile 临时文件
|
|
* @return 返回文件总行数
|
|
* @return 返回文件总行数
|
|
*/
|
|
*/
|
|
- public static Integer textExcel(String filePath) throws IOException {
|
|
|
|
|
|
+ public static Integer textExcel(File tempFile, String sourceId) throws IOException {
|
|
//判断文件是否存在
|
|
//判断文件是否存在
|
|
- if (filePath == null || filePath.equals("")) {
|
|
|
|
- ThrowException.throwXiaoShiException("文件上传失败,服务器忙请稍后再试!");
|
|
|
|
- }
|
|
|
|
- File file = new File(filePath);
|
|
|
|
- if (!file.exists()) {
|
|
|
|
|
|
+ if (!tempFile.exists() || tempFile.getPath().trim().equals("")) {
|
|
|
|
+ //删除临时文件tempFile
|
|
|
|
+ new File(tempFile.getPath()).delete();
|
|
ThrowException.throwXiaoShiException("文件上传失败,服务器忙请稍后再试!");
|
|
ThrowException.throwXiaoShiException("文件上传失败,服务器忙请稍后再试!");
|
|
}
|
|
}
|
|
|
|
|
|
// 检测是否为excel文件
|
|
// 检测是否为excel文件
|
|
- if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx") && !filePath.endsWith(".XLS") && !filePath.endsWith(".XLSX")) {
|
|
|
|
|
|
+ String suffix = tempFile.getPath().substring(tempFile.getPath().lastIndexOf("."));
|
|
|
|
+ if (!suffix.equals(".xls") && !suffix.equals(".xlsx") && !suffix.equals(".XLS") && !suffix.equals(".XLSX")) {
|
|
|
|
+ //删除临时文件tempFile
|
|
|
|
+ new File(tempFile.getPath()).delete();
|
|
ThrowException.throwXiaoShiException("文件格式错误,请上传Excel文件!");
|
|
ThrowException.throwXiaoShiException("文件格式错误,请上传Excel文件!");
|
|
}
|
|
}
|
|
|
|
|
|
- InputStream fis = new FileInputStream(file);
|
|
|
|
|
|
+ InputStream fis = new FileInputStream(tempFile);
|
|
//使用poi框架解析处理Excel文件
|
|
//使用poi框架解析处理Excel文件
|
|
Workbook workbook = null;
|
|
Workbook workbook = null;
|
|
//区分不同版本Excel,使用各自对应的工具类
|
|
//区分不同版本Excel,使用各自对应的工具类
|
|
- if (filePath.endsWith(".xls") || filePath.endsWith(".XLS")) {
|
|
|
|
|
|
+ if (suffix.equals(".xls") || suffix.equals(".XLS")) {
|
|
workbook = new HSSFWorkbook(fis);
|
|
workbook = new HSSFWorkbook(fis);
|
|
- } else if (filePath.endsWith(".xlsx") || filePath.endsWith(".XLSX")) {
|
|
|
|
|
|
+ } else if (suffix.equals(".xlsx") || suffix.equals(".XLSX")) {
|
|
workbook = new XSSFWorkbook(fis);
|
|
workbook = new XSSFWorkbook(fis);
|
|
}
|
|
}
|
|
//读取第几个sheet
|
|
//读取第几个sheet
|
|
@@ -55,6 +56,9 @@ public class ReadExcelUtils {
|
|
//读取总行数
|
|
//读取总行数
|
|
int rows = sheet.getPhysicalNumberOfRows();
|
|
int rows = sheet.getPhysicalNumberOfRows();
|
|
if (rows <= 1) {
|
|
if (rows <= 1) {
|
|
|
|
+ //删除临时文件tempFile
|
|
|
|
+ fis.close();
|
|
|
|
+ new File(tempFile.getPath()).delete();
|
|
ThrowException.throwXiaoShiException("文件内容格式不正确,请检查总行数是否有专利内容");
|
|
ThrowException.throwXiaoShiException("文件内容格式不正确,请检查总行数是否有专利内容");
|
|
}
|
|
}
|
|
|
|
|
|
@@ -62,9 +66,20 @@ public class ReadExcelUtils {
|
|
Row firstRow = sheet.getRow(0);
|
|
Row firstRow = sheet.getRow(0);
|
|
boolean flag1 = false; //是否有 "公开(公告)号"
|
|
boolean flag1 = false; //是否有 "公开(公告)号"
|
|
boolean flag2 = false; //是否有 "申请号"
|
|
boolean flag2 = false; //是否有 "申请号"
|
|
- //遍历第一行单元格抬头
|
|
|
|
|
|
+ //遍历第一行单元格抬头,检查合法性
|
|
|
|
+ String title = "", source = "";
|
|
|
|
+ if (sourceId.equals("1")) {
|
|
|
|
+ source = "智慧芽";
|
|
|
|
+ title = "公开(公告)号";
|
|
|
|
+ } else if (sourceId.equals("2")) {
|
|
|
|
+ source = "合享";
|
|
|
|
+ title = "公开(公告)号";
|
|
|
|
+ } else {
|
|
|
|
+ source = "Patentics";
|
|
|
|
+ title = "公开号";
|
|
|
|
+ }
|
|
for (Cell cell : firstRow) {
|
|
for (Cell cell : firstRow) {
|
|
- if (cell.getStringCellValue().equals("公开(公告)号")) {
|
|
|
|
|
|
+ if (cell.getStringCellValue().equals(title)) {
|
|
flag1 = true;
|
|
flag1 = true;
|
|
}
|
|
}
|
|
if (cell.getStringCellValue().equals("申请号")) {
|
|
if (cell.getStringCellValue().equals("申请号")) {
|
|
@@ -72,9 +87,15 @@ public class ReadExcelUtils {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (!flag1 || !flag2) {
|
|
if (!flag1 || !flag2) {
|
|
- ThrowException.throwXiaoShiException("文件内容格式不正确,第一行抬头必须有【公开(公告)号】和【申请号】");
|
|
|
|
|
|
+ //删除临时文件tempFile
|
|
|
|
+ fis.close();
|
|
|
|
+ new File(tempFile.getPath()).delete();
|
|
|
|
+ ThrowException.throwXiaoShiException("文件内容格式不正确,您选择【" + source + "】来源,Excel第一行抬头必须有【" + title + "】和【申请号】");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ //关闭流
|
|
|
|
+ fis.close();
|
|
|
|
+
|
|
//返回文件总行数-1(即专利总数量)
|
|
//返回文件总行数-1(即专利总数量)
|
|
return rows - 1;
|
|
return rows - 1;
|
|
}
|
|
}
|
|
@@ -82,12 +103,12 @@ public class ReadExcelUtils {
|
|
/**
|
|
/**
|
|
* 获取一行专利的全部数据(专利内容数据 + 摘要附图)
|
|
* 获取一行专利的全部数据(专利内容数据 + 摘要附图)
|
|
*
|
|
*
|
|
- * @param filePath Excel文件路径
|
|
|
|
|
|
+ * @param tempFile Excel临时文件
|
|
* @param row 行数
|
|
* @param row 行数
|
|
* @return 返回装载专利数据(专利内容数据 + 摘要附图)的对象
|
|
* @return 返回装载专利数据(专利内容数据 + 摘要附图)的对象
|
|
*/
|
|
*/
|
|
- public static PatentData readExcelOneRow(String filePath, Sheet sheet, int row) throws IOException {
|
|
|
|
- //返回最终结果的对象
|
|
|
|
|
|
+ public static PatentData readExcelOneRow(File tempFile, Sheet sheet, int row) throws IOException {
|
|
|
|
+ //创建返回最终结果的对象 patentData
|
|
PatentData patentData = new PatentData();
|
|
PatentData patentData = new PatentData();
|
|
//装载专利数据(除了摘要附图)的map:(key:表头如 "公开(公告)号" value:表头对应内容如 "CN1307082B")
|
|
//装载专利数据(除了摘要附图)的map:(key:表头如 "公开(公告)号" value:表头对应内容如 "CN1307082B")
|
|
Map<Object, Object> map = new HashMap<>();
|
|
Map<Object, Object> map = new HashMap<>();
|
|
@@ -104,9 +125,10 @@ public class ReadExcelUtils {
|
|
}
|
|
}
|
|
|
|
|
|
//开始装载专利摘要附图(判断用07还是03的方法获取图片)
|
|
//开始装载专利摘要附图(判断用07还是03的方法获取图片)
|
|
- if (filePath.endsWith(".xls") || filePath.endsWith(".XLS")) {
|
|
|
|
|
|
+ String suffix = tempFile.getName().substring(tempFile.getName().lastIndexOf("."));
|
|
|
|
+ if (suffix.equals(".xls") || suffix.equals(".XLS")) {
|
|
pictureData = getPictures1((HSSFSheet) sheet, row);
|
|
pictureData = getPictures1((HSSFSheet) sheet, row);
|
|
- } else if (filePath.endsWith(".xlsx") || filePath.endsWith(".XLSX")) {
|
|
|
|
|
|
+ } else if (suffix.equals(".xlsx") || suffix.equals(".XLSX")) {
|
|
pictureData = getPictures2((XSSFSheet) sheet, row);
|
|
pictureData = getPictures2((XSSFSheet) sheet, row);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -117,23 +139,27 @@ public class ReadExcelUtils {
|
|
return patentData;
|
|
return patentData;
|
|
}
|
|
}
|
|
|
|
|
|
- public static Sheet readExcel(String filePath) {
|
|
|
|
|
|
+ public static Sheet readExcel(File tempFile) {
|
|
Sheet sheet = null;
|
|
Sheet sheet = null;
|
|
|
|
|
|
- File file = new File(filePath);
|
|
|
|
try {
|
|
try {
|
|
- InputStream inputStream = new FileInputStream(file);
|
|
|
|
|
|
+ InputStream inputStream = new FileInputStream(tempFile);
|
|
//POI可以处理Excel文件
|
|
//POI可以处理Excel文件
|
|
Workbook workbook = null;
|
|
Workbook workbook = null;
|
|
//当文件以.xls结尾时
|
|
//当文件以.xls结尾时
|
|
- if (filePath.endsWith(".xls") || filePath.endsWith(".XLS")) {
|
|
|
|
|
|
+ String suffix = tempFile.getName().substring(tempFile.getName().lastIndexOf("."));
|
|
|
|
+ if (suffix.equals(".xls") || suffix.equals(".XLS")) {
|
|
workbook = new HSSFWorkbook(inputStream);
|
|
workbook = new HSSFWorkbook(inputStream);
|
|
- } else if (filePath.endsWith(".xlsx") || filePath.endsWith(".XLSX")) {
|
|
|
|
|
|
+ } else if (suffix.equals(".xlsx") || suffix.equals(".XLSX")) {
|
|
workbook = new XSSFWorkbook(inputStream);
|
|
workbook = new XSSFWorkbook(inputStream);
|
|
}
|
|
}
|
|
|
|
|
|
//读取第几个sheet
|
|
//读取第几个sheet
|
|
sheet = workbook.getSheetAt(0);
|
|
sheet = workbook.getSheetAt(0);
|
|
|
|
+
|
|
|
|
+ //关闭流
|
|
|
|
+ inputStream.close();
|
|
|
|
+
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|