diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..756d610 --- /dev/null +++ b/pom.xml @@ -0,0 +1,90 @@ + + + 4.0.0 + + ru.dmitriymx + corrector-1s + 1.0-SNAPSHOT + + + UTF-8 + 1.8 + 1.7.21 + 2.5 + + + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.apache.logging.log4j + log4j-core + ${log4j.version} + + + org.apache.logging.log4j + log4j-slf4j-impl + ${log4j.version} + + + + + org.projectlombok + lombok + 1.16.16 + provided + + + org.apache.poi + poi-ooxml + 3.17 + + + + + + ${project.artifactId}-${project.version} + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + ${java.version} + ${java.version} + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.15 + + -Dfile.encoding=${project.build.sourceEncoding} + + + + + maven-assembly-plugin + 2.2-beta-5 + + + + ru.dmitriymx.Main + + + + jar-with-dependencies + + + + + + + \ No newline at end of file diff --git a/src/main/java/ru/dmitriymx/corrector1s/ExcelReader.java b/src/main/java/ru/dmitriymx/corrector1s/ExcelReader.java new file mode 100644 index 0000000..2ee2426 --- /dev/null +++ b/src/main/java/ru/dmitriymx/corrector1s/ExcelReader.java @@ -0,0 +1,232 @@ +package ru.dmitriymx.corrector1s; + +import lombok.Data; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.ss.util.RegionUtil; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +@Slf4j +public class ExcelReader implements Closeable { + public class ExcelRecord { + private String name; + private Double unknownInt; + private Double data; + } + + private Workbook workbook; + private Sheet sheet; + + public ExcelReader(File file) throws IOException, InvalidFormatException { + workbook = WorkbookFactory.create(file); + sheet = workbook.getSheetAt(0); // берем первый и единственный Лист + } + + public List getData() { + List list = new ArrayList<>(18); + + for (int c = 10; c < 29; c++) { + ExcelRecord record = new ExcelRecord(); + + for (int r = 6; r < 9; r++) { + Cell cell = sheet.getRow(r).getCell(c); + + if (r == 6) { + record.name = cell.getStringCellValue(); + } else if (r == 7) { + record.unknownInt = cell.getNumericCellValue(); + } else if (r == 8) { + record.data= cell.getNumericCellValue(); + } + } + + list.add(record); + } + + return list; + } + + public void correctStyle() { + // Удаляем сведЕния у ячеек + int x = 0; + while (x < 4) { + for(int m = 0; m < sheet.getNumMergedRegions(); ++m) { + CellRangeAddress cellRangeAddress = sheet.getMergedRegion(m); + + if (cellRangeAddress.getFirstRow() == 6 && cellRangeAddress.getFirstColumn() == 23) { + sheet.removeMergedRegion(m); + x++; + break; + } + + if (cellRangeAddress.getFirstRow() == 6 && cellRangeAddress.getFirstColumn() == 25) { + sheet.removeMergedRegion(m); + x++; + break; + } + + if (cellRangeAddress.getFirstRow() == 6 && cellRangeAddress.getFirstColumn() == 26) { + sheet.removeMergedRegion(m); + x++; + break; + } + + if (cellRangeAddress.getFirstRow() == 6 && cellRangeAddress.getFirstColumn() == 27) { + sheet.removeMergedRegion(m); + x++; + break; + } + } + } + + sheet.addMergedRegion(new CellRangeAddress(6, 7, 10, 10)); + sheet.addMergedRegion(new CellRangeAddress(6, 7, 11, 11)); + sheet.addMergedRegion(new CellRangeAddress(6, 7, 14, 14)); + sheet.addMergedRegion(new CellRangeAddress(6, 7, 22, 22)); + } + + private void _set_data(ExcelRecord record, int col) { + sheet.getRow(6).getCell(col).setCellValue(record.name); + sheet.getRow(7).getCell(col).setCellValue(record.unknownInt); + sheet.getRow(8).getCell(col).setCellValue(record.data); + sheet.getRow(9).getCell(col).setCellValue(record.data); + if (record.data == 0.0d) { + sheet.getRow(8).getCell(col).setCellType(CellType.BLANK); + sheet.getRow(9).getCell(col).setCellType(CellType.BLANK); + } + } + + public void correctData(List listOfData) { + _set_data(listOfData.get(0), 12); + _set_data(listOfData.get(1), 13); + _set_data(listOfData.get(2), 15); + _set_data(listOfData.get(3), 16); + _set_data(listOfData.get(4), 17); + _set_data(listOfData.get(5), 18); + _set_data(listOfData.get(6), 19); + _set_data(listOfData.get(7), 20); + _set_data(listOfData.get(8), 21); + _set_data(listOfData.get(9), 23); + _set_data(listOfData.get(10), 25); + _set_data(listOfData.get(11), 26); + _set_data(listOfData.get(12), 27); + _set_data(listOfData.get(13), 28); + _set_data(listOfData.get(14), 14); + _set_data(listOfData.get(15), 22); + _set_data(listOfData.get(16), 24); + _set_data(listOfData.get(17), 11); + _set_data(listOfData.get(18), 10); + } + + public void insertNewCols() { + Font font = workbook.createFont(); + font.setFontName("Arial"); + font.setBold(true); + font.setFontHeightInPoints((short) 8); + + CellStyle cellStyle1 = workbook.createCellStyle(); + cellStyle1.setAlignment(HorizontalAlignment.CENTER); + cellStyle1.setVerticalAlignment(VerticalAlignment.CENTER); + cellStyle1.setFont(font); + cellStyle1.setWrapText(true); + cellStyle1.setBorderTop(BorderStyle.THIN); + cellStyle1.setBorderBottom(BorderStyle.THIN); + cellStyle1.setBorderLeft(BorderStyle.THIN); + cellStyle1.setBorderRight(BorderStyle.THIN); + + CellStyle cellStyle2 = workbook.createCellStyle(); + cellStyle2.setBorderTop(BorderStyle.THIN); + cellStyle2.setBorderBottom(BorderStyle.THIN); + cellStyle2.setBorderLeft(BorderStyle.THIN); + cellStyle2.setBorderRight(BorderStyle.THIN); + + Cell cell = sheet.getRow(6).createCell(29); + cell.setCellType(CellType.STRING); + cell.setCellValue("Страховые взносы + ФСС НС (31%)"); + cell.setCellStyle(cellStyle1); + cell = sheet.getRow(7).createCell(29); + cell.setCellType(CellType.STRING); + cell.setCellValue("Начислено"); + cell.setCellStyle(cellStyle1); + cell = sheet.getRow(8).createCell(29); + cell.setCellStyle(cellStyle2); + cell.setCellType(CellType.FORMULA); + cell.setCellFormula("(AC9-J9)*31%"); + cell = sheet.getRow(9).createCell(29); + cell.setCellStyle(cellStyle2); + cell.setCellType(CellType.FORMULA); + cell.setCellFormula("(AC10-J10)*31%"); + + cell = sheet.getRow(6).createCell(30); + cell.setCellType(CellType.STRING); + cell.setCellValue("БАЗА"); + cell.setCellStyle(cellStyle1); + cell = sheet.getRow(7).createCell(30); + cell.setCellType(CellType.STRING); + cell.setCellValue("для начисления стоимости услуг"); + cell.setCellStyle(cellStyle1); + cell = sheet.getRow(8).createCell(30); + cell.setCellStyle(cellStyle2); + cell.setCellType(CellType.FORMULA); + cell.setCellFormula("AC9+AD9"); + cell = sheet.getRow(9).createCell(30); + cell.setCellStyle(cellStyle2); + cell.setCellType(CellType.FORMULA); + cell.setCellFormula("AC10+AD10"); + + + cell = sheet.getRow(6).createCell(31); + cell.setCellType(CellType.STRING); + cell.setCellValue("Гонорар"); + cell.setCellStyle(cellStyle1); + cell = sheet.getRow(7).createCell(31); + cell.setCellType(CellType.STRING); + cell.setCellValue("9%"); + cell.setCellStyle(cellStyle1); + cell = sheet.getRow(8).createCell(31); + cell.setCellStyle(cellStyle2); + cell.setCellType(CellType.FORMULA); + cell.setCellFormula("AE9*9%"); + cell = sheet.getRow(9).createCell(31); + cell.setCellStyle(cellStyle2); + cell.setCellType(CellType.FORMULA); + cell.setCellFormula("AE10*9%"); + + cell = sheet.getRow(6).createCell(32); + cell.setCellType(CellType.STRING); + cell.setCellValue("Итого, для счета"); + cell.setCellStyle(cellStyle1); + cell = sheet.getRow(7).createCell(32); + cell.setCellType(CellType.STRING); + cell.setCellValue("Результат"); + cell.setCellStyle(cellStyle1); + cell = sheet.getRow(8).createCell(32); + cell.setCellStyle(cellStyle2); + cell.setCellType(CellType.FORMULA); + cell.setCellFormula("AE9+AF9"); + cell = sheet.getRow(9).createCell(32); + cell.setCellStyle(cellStyle2); + cell.setCellType(CellType.FORMULA); + cell.setCellFormula("AE10+AF10"); + + workbook.getCreationHelper().createFormulaEvaluator().evaluateAll(); + } + + public void save(File file) throws IOException { + try (FileOutputStream fos = new FileOutputStream(file)) { + workbook.write(fos); + } + } + + @Override + public void close() throws IOException { + workbook.close(); + } +} diff --git a/src/main/java/ru/dmitriymx/corrector1s/Main.java b/src/main/java/ru/dmitriymx/corrector1s/Main.java new file mode 100644 index 0000000..a564605 --- /dev/null +++ b/src/main/java/ru/dmitriymx/corrector1s/Main.java @@ -0,0 +1,21 @@ +package ru.dmitriymx.corrector1s; + +import lombok.extern.slf4j.Slf4j; + +import java.io.File; +import java.util.List; + +@Slf4j +public class Main { + public static void main(String[] args) throws Exception { + File file = new File(args[0]); + File file1 = new File(args[1]); + + ExcelReader excelReader = new ExcelReader(file); + List data = excelReader.getData(); + excelReader.correctStyle(); + excelReader.correctData(data); + excelReader.insertNewCols(); + excelReader.save(file1); + } +} diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml new file mode 100644 index 0000000..6069001 --- /dev/null +++ b/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file