Archived
0

Сохраняем данные ячеек

This commit is contained in:
2018-06-04 01:33:18 +03:00
parent c550158ce9
commit 3916e58434
3 changed files with 72 additions and 11 deletions

View File

@@ -3,6 +3,7 @@ package ru.dmitriymx.corrector1s;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
@@ -10,6 +11,7 @@ import org.apache.poi.ss.usermodel.WorkbookFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@@ -18,7 +20,7 @@ import java.util.Map;
public class Corrector1S {
private class ExcelRecord {
private String name;
private String addition;
private Integer addition;
private Double data;
}
@@ -29,7 +31,9 @@ public class Corrector1S {
@Setter
private double honorarium;
private Map<String, ExcelRecord> mapRecords = new HashMap<>(19); // ожидается 19 записей для сохранения
private Map<String, ExcelRecord> mapRecords;
private Workbook workbook;
private Sheet sheet;
private String getFileExtension(File file) {
int i = file.getAbsolutePath().lastIndexOf('.');
@@ -67,4 +71,28 @@ public class Corrector1S {
throw new FormatFileException(e);
}
}
public void createSnapshotData() throws IOException, InvalidFormatException {
workbook = WorkbookFactory.create(sourceFile);
sheet = workbook.getSheetAt(0);
mapRecords = new HashMap<>(19); // ожидается 19 записей для сохранения
Cell cell;
loop:
for (int c = 10; true; c++) {
ExcelRecord record = new ExcelRecord();
for (int r = 6; r < 9; r++) {
cell = sheet.getRow(r).getCell(c);
if (cell == null) break loop;
if (r == 6) record.name = cell.getStringCellValue();
else if (r == 7) record.addition = (int) cell.getNumericCellValue();
else record.data = cell.getNumericCellValue();
}
String key = (record.addition == 0 ? record.name : record.name + "_" + record.addition);
mapRecords.put(key, record);
}
}
}

View File

@@ -79,6 +79,7 @@ public class Main {
corrector1S.setTargetFile(targetFile);
corrector1S.setHonorarium((Double) optionSet.valueOf("fee"));
corrector1S.check();
corrector1S.createSnapshotData();
}
}
}

View File

@@ -9,6 +9,7 @@ import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import ru.dmitriymx.corrector1s.Corrector1S;
import java.io.*;
@@ -59,6 +60,19 @@ public class MainController {
return fileChooser;
}
private void enableInterface(boolean value) {
if (value) {
waitPane.setVisible(false);
mainPane.setOpacity(1.0d);
mainPane.setDisable(false);
} else {
mainPane.setDisable(true);
mainPane.setOpacity(0.3d);
waitPane.setVisible(true);
progressBar.setProgress(0d);
}
}
private Alert buildErrorDialog(String title, String text, Exception e) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle(title);
@@ -81,6 +95,14 @@ public class MainController {
return alert;
}
private Alert buildInfoDialog(String title, String text) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(text);
return alert;
}
@FXML
public void initialize() {
gonorar.setValueFactory(gonorarValueFactory);
@@ -104,10 +126,7 @@ public class MainController {
event -> buildFileChooser(saveAsFilePath).showSaveDialog(stage));
btnStartCorrect.addEventHandler(ActionEvent.ACTION, event -> {
mainPane.setDisable(true);
mainPane.setOpacity(0.3d);
waitPane.setVisible(true);
progressBar.setProgress(0d);
enableInterface(false);
Runnable runnable = () -> {
Corrector1S corrector = new Corrector1S();
@@ -125,13 +144,26 @@ public class MainController {
corrector.check();
} catch (Exception e) {
log.error("Analize input data", e);
Alert alert = buildErrorDialog("Ошибка", "Ошибка при анализе входных данных", e);
alert.showAndWait();
buildErrorDialog("Ошибка", "Ошибка при анализе входных данных", e)
.showAndWait();
enableInterface(true);
return;
}
waitPane.setVisible(false);
mainPane.setOpacity(1.0d);
mainPane.setDisable(false);
try {
corrector.createSnapshotData();
} catch (IOException | InvalidFormatException e) {
log.error("createSnapshotData", e);
buildErrorDialog("Ошибка", "Ошибка при обработке данных", e)
.showAndWait();
enableInterface(true);
return;
}
progressBar.setProgress(1.0d);
buildInfoDialog("Информация", "Преобразование завершено успешно")
.showAndWait();
enableInterface(true);
};
Platform.runLater(runnable);