From 9ff6b2fa38167d93de00e6d730d2b038d7731776 Mon Sep 17 00:00:00 2001
From: yaowanxin <3588231647@qq.com>
Date: 星期四, 24 七月 2025 13:21:52 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/ywx' into ywx
---
pom.xml | 5 ++
src/main/java/com/chinaztt/mes/docx/util/TakeWords.java | 65 ++++++++++++++++++++++++++------
2 files changed, 57 insertions(+), 13 deletions(-)
diff --git a/pom.xml b/pom.xml
index 542ce7c..b427f73 100644
--- a/pom.xml
+++ b/pom.xml
@@ -107,6 +107,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>com.opencsv</groupId>
+ <artifactId>opencsv</artifactId>
+ <version>5.7.1</version>
+ </dependency>
</dependencies>
<dependencyManagement>
diff --git a/src/main/java/com/chinaztt/mes/docx/util/TakeWords.java b/src/main/java/com/chinaztt/mes/docx/util/TakeWords.java
index bbaa58d..c3469d4 100644
--- a/src/main/java/com/chinaztt/mes/docx/util/TakeWords.java
+++ b/src/main/java/com/chinaztt/mes/docx/util/TakeWords.java
@@ -2,6 +2,9 @@
import cn.hutool.core.io.FileUtil;
import com.chinaztt.mes.docx.dto.GetFileDto;
+import com.opencsv.CSVReader;
+import com.opencsv.CSVReaderBuilder;
+import com.opencsv.exceptions.CsvValidationException;
import com.chinaztt.mes.docx.dto.ThicknessData;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
@@ -25,15 +28,20 @@
import javax.imageio.ImageIO;
import java.io.*;
-import java.nio.file.Files;
import java.sql.*;
import java.util.*;
+import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TakeWords {
private static final String splitIdentifier = "@-@"; // 鑷畾涔夊敮涓�鏍囪瘑绗�
+
+ // 绉戝璁℃暟娉曟鍒欐ā寮�
+ private static final Pattern SCIENTIFIC_PATTERN = Pattern.compile(
+ "^[+-]?\\d+(\\.\\d+)?[eE][+-]?\\d+$"
+ );
public static Object readWordFile(File file) {
String result = "";
@@ -220,23 +228,54 @@
StringBuilder stringBuilder = new StringBuilder();
// 鍒涘缓 reader
- try (BufferedReader br = Files.newBufferedReader(file.toPath())) {
- // CSV鏂囦欢鐨勫垎闅旂
- String DELIMITER = ",";
- // 鎸夎璇诲彇
- String line;
- while ((line = br.readLine()) != null) {
- // 鍒嗗壊
- String[] columns = line.split(DELIMITER);
- // 鎵撳嵃琛�
- stringBuilder.append(String.join(splitIdentifier, columns)).append("\n");
+// try (BufferedReader br = Files.newBufferedReader(file.toPath())) {
+// // CSV鏂囦欢鐨勫垎闅旂
+// String DELIMITER = ",";
+// // 鎸夎璇诲彇
+// String line;
+// System.out.println(br.readLine());
+// while ((line = br.readLine()) != null) {
+// // 鍒嗗壊
+// String[] columns = line.split(DELIMITER);
+// // 鎵撳嵃琛�
+// stringBuilder.append(String.join(splitIdentifier, columns)).append("\n");
+// }
+// } catch (IOException ex) {
+// ex.printStackTrace();
+// }
+ try (FileReader fileReader = new FileReader(file);
+ CSVReader csvReader = new CSVReaderBuilder(fileReader).build()) {
+
+ String[] nextLine;
+ while ((nextLine = csvReader.readNext()) != null) {
+ // 澶勭悊姣忎竴琛屾暟鎹�
+ for (String cell : nextLine) {
+ if(StringUtils.isNotBlank(cell)){
+ stringBuilder.append(scientificToNumber(cell)).append(splitIdentifier);
+ }
+ }
+ stringBuilder.append("\n");
}
- } catch (IOException ex) {
- ex.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (CsvValidationException e) {
+ throw new RuntimeException(e);
}
return stringBuilder.toString();
}
+ /**
+ * 灏嗙瀛﹁鏁版硶杞崲涓烘暟瀛�
+ * @param cell
+ * @return
+ */
+ public static String scientificToNumber(String cell){
+ if(SCIENTIFIC_PATTERN.matcher(cell).matches()){
+ return String.valueOf(Double.parseDouble(cell));
+ }
+ return cell;
+ }
+
public static Object readMdbFile(File file, GetFileDto getFileDto) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
Map<String, Object> tableMap = new HashMap<>(16);
Properties prop = new Properties();
--
Gitblit v1.9.3