|
@@ -1,16 +1,24 @@
|
|
package com.github.zuihou.common.util;
|
|
package com.github.zuihou.common.util;
|
|
|
|
|
|
|
|
+import cn.afterturn.easypoi.excel.entity.ImportParams;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
|
+import cn.hutool.poi.excel.ExcelReader;
|
|
|
|
+import cn.hutool.poi.excel.ExcelUtil;
|
|
import com.beust.jcommander.internal.Lists;
|
|
import com.beust.jcommander.internal.Lists;
|
|
import jcifs.smb.*;
|
|
import jcifs.smb.*;
|
|
|
|
+import cn.afterturn.easypoi.excel.ExcelImportUtil;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 提供共享文件夹的操作的通用程序
|
|
* 提供共享文件夹的操作的通用程序
|
|
*/
|
|
*/
|
|
public class SmbShareFileUtil {
|
|
public class SmbShareFileUtil {
|
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(SmbShareFileUtil.class);
|
|
/**
|
|
/**
|
|
* 将文件内容写入远程共享文件目录
|
|
* 将文件内容写入远程共享文件目录
|
|
* @param shareFile 共享文件路径
|
|
* @param shareFile 共享文件路径
|
|
@@ -362,8 +370,84 @@ public class SmbShareFileUtil {
|
|
return returnFileName;
|
|
return returnFileName;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static Map<String, List<String>> processExcelFiles(String folderPath) {
|
|
|
|
+ Map<String, List<String>> resultMap = new HashMap<>();
|
|
|
|
+ File folder = new File(folderPath);
|
|
|
|
+ File[] files = folder.listFiles((dir, name) -> (name.endsWith(".xlsx") || name.endsWith(".xls")) && name.indexOf("processed") == -1);
|
|
|
|
+
|
|
|
|
+ if (files == null || files.length == 0) {
|
|
|
|
+ logger.info("没有找到需要处理的 Excel 文件。");
|
|
|
|
+ return resultMap;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (File file : files) {
|
|
|
|
+ try {
|
|
|
|
+ Map.Entry<String, List<String>> result = parseExcelFile(file);
|
|
|
|
+ if (result != null) {
|
|
|
|
+ resultMap.put(result.getKey(), result.getValue());
|
|
|
|
+ renameFile(file); // 修改文件名
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error("处理文件时出错: " + file.getName(),e);
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return resultMap;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static Map.Entry<String, List<String>> parseExcelFile(File filePath) throws Exception {
|
|
|
|
+ if (filePath.exists() && !filePath.canWrite()) {
|
|
|
|
+ boolean success = filePath.setWritable(true);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 使用 Hutool 的 ExcelReader 解析 Excel 文件
|
|
|
|
+ ExcelReader reader = ExcelUtil.getReader(filePath);
|
|
|
|
+
|
|
|
|
+ // 获取所有行数据,假设每行的第二列是我们需要的数据
|
|
|
|
+ List<List<Object>> list = reader.read();
|
|
|
|
+
|
|
|
|
+ if (list == null || list.isEmpty()) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 第一行作为唯一标识
|
|
|
|
+ String uniqueId = list.get(0).get(1).toString();
|
|
|
|
+
|
|
|
|
+ // 解析第 8-13 行的数据
|
|
|
|
+ List<String> dataList = new ArrayList<>();
|
|
|
|
+ for (int i = 7; i <= 12; i++) { // 行号从 0 开始
|
|
|
|
+ if (i < list.size()) {
|
|
|
|
+ Object cellData = list.get(i).get(1);
|
|
|
|
+ dataList.add(cellData != null ? cellData.toString() : "");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return new AbstractMap.SimpleEntry<>(uniqueId, dataList);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void renameFile(File file) {
|
|
|
|
+ String newName = "processed_"+file.getName();
|
|
|
|
+ File renamedFile = new File(file.getParent(), newName);
|
|
|
|
+ if (!file.renameTo(renamedFile)) {
|
|
|
|
+ logger.info("文件重命名失败: " + file.getName());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
|
|
+ String folderPath = "D:\\CeShi"; // 替换为您的文件夹路径
|
|
|
|
+ Map<String, List<String>> resultMap = processExcelFiles(folderPath);
|
|
|
|
+
|
|
|
|
+ // 打印结果
|
|
|
|
+ resultMap.forEach((key, value) -> {
|
|
|
|
+ System.out.println("唯一标识: " + key);
|
|
|
|
+ System.out.println("数据: " + value);
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* public static void main(String[] args) {
|
|
String ip="192.168.11.245";
|
|
String ip="192.168.11.245";
|
|
String url="smb://192.168.11.245/measuring/";
|
|
String url="smb://192.168.11.245/measuring/";
|
|
//String url="smb://192.168.170.23/measuring/abcd-1234.csv";
|
|
//String url="smb://192.168.170.23/measuring/abcd-1234.csv";
|
|
@@ -394,5 +478,5 @@ public class SmbShareFileUtil {
|
|
// writeShareFileContent(url,content,name,password,ip);
|
|
// writeShareFileContent(url,content,name,password,ip);
|
|
|
|
|
|
findMeasuringFilesAndDownload(ip,name,password,url,"111.xls","C:/data/projects/uploadfile/file/");
|
|
findMeasuringFilesAndDownload(ip,name,password,url,"111.xls","C:/data/projects/uploadfile/file/");
|
|
- }
|
|
|
|
|
|
+ }*/
|
|
}
|
|
}
|