Столбец A: текст (например, коды продуктов) < /li>
Столбец B: текст (например, описания продукта) < /li>
Колонка C: числовые (например, цены на продукт) < /li>
< /ul>
Однако, когда моя программа пытается прочитать данные, я столкнулся с следующей ошибкой: < /p>
Код: Выделить всё
Exception in thread "main" java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell
at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:984)
at org.apache.poi.xssf.usermodel.XSSFCell.getRichStringCellValue(XSSFCell.java:311)
at org.apache.poi.xssf.usermodel.XSSFCell.getStringCellValue(XSSFCell.java:280)
at com.example.ExcelReader.readExcel(ExcelReader.java:37)
at com.example.Main.main(Main.java:26)
Код: Выделить всё
String keyValue = getCellValueAsString(keyCell).trim().replaceAll("\\s+", "");
Код: Выделить всё
Map excelData = ExcelReader.readExcel(excelFilePath);
build.gradle
Код: Выделить всё
plugins {
id 'java'
id 'application'
id 'org.beryx.jlink' version '2.26.0'
}
application {
mainClass = 'com.example.Main'
}
group = 'com.example'
version = '1.0'
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
modularity.inferModulePath = true
}
ext.moduleName = 'com.example'
tasks.compileJava {
inputs.property('moduleName', moduleName)
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath
]
classpath = files()
}
}
tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.pdfbox:pdfbox:2.0.30'
implementation 'org.apache.poi:poi-ooxml:5.3.0'
implementation 'org.apache.commons:commons-math3:3.6'
implementation 'org.apache.logging.log4j:log4j-core:2.20.0'
implementation 'org.apache.logging.log4j:log4j-api:2.20.0'
testImplementation platform('org.junit:junit-bom:5.10.3')
testImplementation 'org.junit.jupiter:junit-jupiter'
}
tasks.jar {
archiveBaseName = 'app'
}
tasks.register('copyDependencies', Copy) {
def outputDir = layout.buildDirectory.dir('libs')
from configurations.runtimeClasspath
into outputDir
}
jlink {
launcher {
name = 'my-app'
}
jpackage {
installerType = 'exe' // lub 'msi', 'dmg', 'pkg', 'deb', 'rpm'
installerOptions = [
'--description', 'Aplikacja do przetwarzania plików PDF i Excel',
'--vendor', 'MojaFirma',
'--win-per-user-install',
'--win-dir-chooser',
'--win-shortcut'
]
imageOptions = ['--icon', 'src/main/resources/icon.ico']
}
}
test {
useJUnitPlatform()
}
< /code>
Excelreader.java
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class ExcelReader {
public static Map readExcel(String excelFilePath) throws IOException {
Map dataMap = new HashMap();
File file = new File(excelFilePath);
if (!file.exists()) {
throw new FileNotFoundException("Plik z danymi nie istnieje: " + excelFilePath);
}
try (FileInputStream fis = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(fis)) {
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
Cell keyCell = row.getCell(0);
Cell equivalentCell = row.getCell(1);
Cell priceCell = row.getCell(2);
// Logowanie typów danych każdej komórki
logCellType("IFS", keyCell);
logCellType("ILME", equivalentCell);
logCellType("Cena", priceCell);
if (keyCell != null && equivalentCell != null && priceCell != null) {
// Pobranie wartości z komórek
String keyValue = getCellValueAsString(keyCell).trim().replaceAll("\\s+", "");
String equivalentValue = getCellValueAsString(equivalentCell).trim();
String priceValue = getCellValueAsString(priceCell)
.replace(" zł", "").replace(",", ".").trim();
dataMap.put(keyValue, new String[]{equivalentValue, priceValue});
System.out.println("Read from Excel: " + keyValue + " -> " + equivalentValue + ", " + priceValue);
}
}
}
return dataMap;
}
private static String getCellValueAsString(Cell cell) {
if (cell == null) {
return "";
}
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue().trim().replaceAll("\\s+", " ");
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue().toString();
} else {
return String.format("%.2f", cell.getNumericCellValue());
}
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case FORMULA:
try {
return String.valueOf(cell.getNumericCellValue());
} catch (IllegalStateException e) {
return cell.getStringCellValue();
}
case BLANK:
default:
return "";
}
}
private static void logCellType(String columnName, Cell cell) {
if (cell == null) {
System.out.println(columnName + " column: EMPTY (null)");
return;
}
switch (cell.getCellType()) {
case STRING:
System.out.println(columnName + " column: STRING value -> " + cell.getStringCellValue());
break;
case NUMERIC:
System.out.println(columnName + " column: NUMERIC value -> " + cell.getNumericCellValue());
break;
case BOOLEAN:
System.out.println(columnName + " column: BOOLEAN value -> " + cell.getBooleanCellValue());
break;
case FORMULA:
System.out.println(columnName + " column: FORMULA value -> " + cell.getCellFormula());
break;
case BLANK:
System.out.println(columnName + " column: BLANK");
break;
default:
System.out.println(columnName + " column: UNKNOWN TYPE");
break;
}
}
}
Код: Выделить всё
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
if (args.length < 3) {
System.out.println(
"Użycie: java -jar my-app.jar
"
);
System.exit(1);
}
String inputFilePath = args[0];
String excelFilePath = args[1];
String outputFilePath = args[2];
try {
System.out.println("Rozpoczynam przetwarzanie pliku PDF...");
List foundPatterns = PDFProcessor.findPatternsInPDF(inputFilePath);
System.out.println("Odczytuję dane z pliku Excel...");
Map excelData = ExcelReader.readExcel(excelFilePath);
System.out.println("Zapisuję wyniki do pliku...");
FileWriterUtil.writeToFile(outputFilePath, foundPatterns, excelData);
System.out.println("Proces zakończony pomyślnie.");
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
} catch (IOException e) {
System.err.println("Błąd podczas przetwarzania plików: " + e.getMessage());
}
}
}
PDF -обработка (класс PDFProcessor):
Эта часть приложения успешно считывает и извлекает специфические шаблоны
из файла PDF с использованием шаблона regex
\ d {4}-\ d {4}-\ d {3}-\ d {3} (где x представляет цифры). < /li>
Извлеченные значения правильно регистрируются и передаются в следующую обработку
Стадия. Эта часть приложения функционирует, как и ожидалось. xxx-xxx, brak,
brak), где «brak» указывает, что в файле
excel не было обнаружено никаких соответствующих данных. < /li>
< /ul>
[столбцы A, B и C содержат образцы данных: < /p>
столбец A (A1): 1234-1234-123-123 (текстовое значение в Специфический формат). < /li>
Столбец B (B1): CDMA 1.0 (значение текста с номером). < /li>
столбец C (C1): 100, 00 Zł (числовое значение, отформатированное как валюта с
«zł» символ). < /Li>
< /ul>
столбцы E, F и G содержат формулы, которые проверяют Тип данных ячеек в столбцах a, b и c: < /p>
e1: istext (a1) → в ячейке F1, результат - Prawda (true) , указывая
, что A1 - это текст. Текст.
< /ul>
столбец g содержит английские эквиваленты результатов, показывая истину для всех проверок, подтверждая, что формулы вернули ожидаемые результаты.] 1 < /p>
Подробнее здесь: https://stackoverflow.com/questions/793 ... -to-handle
Мобильная версия