diff --git a/src/main/java/ru/dmitriymx/corrector1s/Corrector1S.java b/src/main/java/ru/dmitriymx/corrector1s/Corrector1S.java index 2f09587..61a6240 100644 --- a/src/main/java/ru/dmitriymx/corrector1s/Corrector1S.java +++ b/src/main/java/ru/dmitriymx/corrector1s/Corrector1S.java @@ -2,9 +2,7 @@ package ru.dmitriymx.corrector1s; import lombok.Setter; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.ss.usermodel.WorkbookFactory; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.xml.sax.SAXException; import ru.dmitriymx.corrector1s.config.ColumnData; @@ -12,6 +10,7 @@ import ru.dmitriymx.corrector1s.config.XmlConfig; import ru.dmitriymx.corrector1s.excel.ExcelDocument; import ru.dmitriymx.corrector1s.excel.ExcelRecord; import ru.dmitriymx.corrector1s.excel.ExcelRecordData; +import ru.dmitriymx.corrector1s.excel.ExcelRecordFormula; import javax.xml.parsers.ParserConfigurationException; import java.io.File; @@ -28,6 +27,8 @@ public class Corrector1S { @Setter private File targetFile; @Setter + private XmlConfig config; + @Setter private double honorarium; @Setter private double fss; @@ -96,18 +97,32 @@ public class Corrector1S { * Убираем объединение ячеек */ public void removeMergedCells() { - /* - Опытным путём было выявлено, что должно остаться 5 объединённых регионов, если у всех остальных колонок - убрать объединение. - */ - while (this.document.getNumMergedCells() > 5) { - for(int m = 0; m < this.document.getNumMergedCells(); m++) { - CellRangeAddress cellRangeAddress = this.document.getMergedCells(m); - if (cellRangeAddress.getFirstColumn() >= 8) { - this.document.removeMergedCells(m); - break; + // мега костыль! а всё потому, что по человечески это, блин, не сделать + loop: + while(true) { + /* + Опытным путём было выявлено, что должно остаться 5 объединённых регионов, если у всех остальных колонок + убрать объединение. + */ + if (this.document.getNumMergedCells() > 5) { + int m = 0; + + while(true) { + if (m >= this.document.getNumMergedCells()) { + continue loop; + } + + CellRangeAddress cellRangeAddress = this.document.getMergedCells(m); + if (cellRangeAddress.getFirstColumn() >= ExcelDocument.START_COLUMN) { + this.document.removeMergedCells(m); + continue loop; + } + + ++m; } } + + return; } } @@ -115,24 +130,65 @@ public class Corrector1S { * Производим сортировку колонок (по сути переписывая текущие значения) */ public void replaceData() { - XmlConfig config = null; - try { - config = new XmlConfig(new File("config.xml")); - } catch (ParserConfigurationException | IOException | SAXException e) { - e.printStackTrace(); //FIXME логирование ошибки - } - List columnDataList = config.getColumnDataList(); Iterator itrColumnDataList = columnDataList.iterator(); - for (int column = ExcelDocument.START_COLUMN; column <= document.getMaxColumn(); column++) { - if (itrColumnDataList.hasNext()) { + int column; + + loopColumn: + for (column = ExcelDocument.START_COLUMN; column <= document.getMaxColumn(); column++) { + loopColumnData: + while (itrColumnDataList.hasNext()) { ColumnData columnData = itrColumnDataList.next(); ExcelRecord record = mapRecords.remove(buildKeyRecord(columnData.getTitle(), columnData.getAddon())); - if (record == null) continue; //FIXME логирование отсутствия нужной колонки + if (record == null) + continue loopColumnData; this.document.setColumnRecord(column, record); + continue loopColumn; //TODO не забываем, нужно еще составить мапу id-column для дальнейшей генерации формул } + + break; } + + for (ExcelRecord record : mapRecords.values()) { + this.document.setColumnRecord(column, record); + column++; + } + + mapRecords.clear(); + } + + private CellStyle buildTitleAddonStyle() { + Font font = document.getWorkbook().createFont(); + font.setFontName("Arial"); + font.setBold(true); + font.setFontHeightInPoints((short) 8); + + CellStyle cellStyle = document.getWorkbook().createCellStyle(); + cellStyle.setAlignment(HorizontalAlignment.CENTER); + cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); + cellStyle.setFont(font); + cellStyle.setWrapText(true); + cellStyle.setBorderTop(BorderStyle.THIN); + cellStyle.setBorderBottom(BorderStyle.THIN); + cellStyle.setBorderLeft(BorderStyle.THIN); + cellStyle.setBorderRight(BorderStyle.THIN); + + return cellStyle; + } + + public void insertNewCols() { + CellStyle titleAddonStyle = buildTitleAddonStyle(); + + ExcelRecordFormula record = new ExcelRecordFormula(); + record.setTitle(String.format("Страховые взносы + ФСС НС (%.2f%%)", this.fss)); + record.setAddon("Начислено"); + record.setTitleStyle(titleAddonStyle); + record.setAddonStyle(titleAddonStyle); + } + + public void saveFile() throws IOException { + document.saveToFile(targetFile); } } diff --git a/src/main/java/ru/dmitriymx/corrector1s/Utils.java b/src/main/java/ru/dmitriymx/corrector1s/Utils.java index bfeeb38..b431956 100644 --- a/src/main/java/ru/dmitriymx/corrector1s/Utils.java +++ b/src/main/java/ru/dmitriymx/corrector1s/Utils.java @@ -6,8 +6,10 @@ package ru.dmitriymx.corrector1s; public class Utils { public static boolean isEmptyObject(Object obj) { - if (obj instanceof Number) { + if (obj instanceof Integer) { return (Integer) obj == 0; + } else if (obj instanceof Double) { + return (Double) obj == 0.0d; } else { return obj == null || obj.toString().trim().isEmpty(); } diff --git a/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelDocument.java b/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelDocument.java index 2dbf291..b970627 100644 --- a/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelDocument.java +++ b/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelDocument.java @@ -19,6 +19,7 @@ public class ExcelDocument implements Closeable { LINE_DATA = 8, START_COLUMN = 7; + @Getter private Workbook workbook; private Sheet sheet; @Getter @@ -63,14 +64,26 @@ public class ExcelDocument implements Closeable { if (line == LINE_TITLE) { record.setTitle(cell.getStringCellValue()); + record.setTitleStyle(cell.getCellStyle()); } else if (line == LINE_ADDON) { if (cell.getCellTypeEnum().equals(CellType.NUMERIC)) { record.setAddon(new Double(cell.getNumericCellValue()).intValue()); } else { record.setAddon(0); } + record.setAddonStyle(cell.getCellStyle()); } else { - record.getDataList().add(cell.getNumericCellValue()); + if (cell.getCellTypeEnum().equals(CellType.NUMERIC)) { + record.getDataList().add(cell.getNumericCellValue()); + } else if (cell.getCellTypeEnum().equals(CellType.BLANK)) { + record.getDataList().add(0.0d); + } else if (cell.getCellTypeEnum().equals(CellType.STRING)){ + try { + record.getDataList().add(Double.parseDouble(cell.getStringCellValue().replace(',', '.'))); + } catch (Exception e) { + e.printStackTrace(); + } + } } } @@ -82,6 +95,7 @@ public class ExcelDocument implements Closeable { Cell cell = this.sheet.getRow(LINE_TITLE).getCell(column, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); cell.setCellType(CellType.STRING); cell.setCellValue(record.getTitle()); + cell.setCellStyle(record.getTitleStyle()); // ADDON cell = this.sheet.getRow(LINE_ADDON).getCell(column, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); @@ -96,6 +110,7 @@ public class ExcelDocument implements Closeable { cell.setCellType(CellType.STRING); cell.setCellValue((String)record.getAddon()); } + cell.setCellStyle(record.getAddonStyle()); // DATA for (int line = LINE_DATA; line <= this.maxLines; line++) { @@ -133,7 +148,7 @@ public class ExcelDocument implements Closeable { } public void removeMergedCells(int id) { - + this.sheet.removeMergedRegion(id); } public void saveToFile(File file) throws IOException { diff --git a/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelRecord.java b/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelRecord.java index b55a85c..ea4e422 100644 --- a/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelRecord.java +++ b/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelRecord.java @@ -4,15 +4,23 @@ */ package ru.dmitriymx.corrector1s.excel; +import org.apache.poi.ss.usermodel.CellStyle; + import java.util.List; public interface ExcelRecord { String getTitle(); void setTitle(String value); + CellStyle getTitleStyle(); + void setTitleStyle(CellStyle style); + T1 getAddon(); void setAddon(T1 value); + CellStyle getAddonStyle(); + void setAddonStyle(CellStyle style); + List getDataList(); boolean isEmptyAddon(); diff --git a/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelRecordData.java b/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelRecordData.java index f28012c..acf4483 100644 --- a/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelRecordData.java +++ b/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelRecordData.java @@ -7,6 +7,7 @@ package ru.dmitriymx.corrector1s.excel; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; +import org.apache.poi.ss.usermodel.CellStyle; import java.util.ArrayList; import java.util.List; @@ -17,7 +18,11 @@ public class ExcelRecordData implements ExcelRecord { @Setter private String title; @Setter + private CellStyle titleStyle; + @Setter private Integer addon; + @Setter + private CellStyle addonStyle; private List dataList; ExcelRecordData(int capacity) { diff --git a/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelRecordFormula.java b/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelRecordFormula.java index a391bd0..cad8415 100644 --- a/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelRecordFormula.java +++ b/src/main/java/ru/dmitriymx/corrector1s/excel/ExcelRecordFormula.java @@ -7,6 +7,7 @@ package ru.dmitriymx.corrector1s.excel; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; +import org.apache.poi.ss.usermodel.CellStyle; import java.util.List; @@ -16,7 +17,11 @@ public class ExcelRecordFormula implements ExcelRecord { @Setter private String title; @Setter + private CellStyle titleStyle; + @Setter private String addon; + @Setter + private CellStyle addonStyle; private List dataList; @Override diff --git a/src/test/java/ru/dmitriymx/corrector1s/Test.java b/src/test/java/ru/dmitriymx/corrector1s/Test.java index 11abd12..0123df7 100644 --- a/src/test/java/ru/dmitriymx/corrector1s/Test.java +++ b/src/test/java/ru/dmitriymx/corrector1s/Test.java @@ -6,12 +6,26 @@ package ru.dmitriymx.corrector1s; import ru.dmitriymx.corrector1s.config.XmlConfig; +import java.io.File; import java.io.InputStream; public class Test { public static void main(String[] args) throws Exception { InputStream stream = Test.class.getResourceAsStream("/config.xml"); XmlConfig config = new XmlConfig(stream); - config.getColumnDataList().forEach(System.out::println); +// config.getColumnDataList().forEach(System.out::println); + + Corrector1S corrector = new Corrector1S(); + corrector.setFss(31d); + corrector.setHonorarium(38.41d); + corrector.setConfig(config); + corrector.setSourceFile(new File("C:\\Users\\admin\\_разное\\1s\\Primer_3_iskh.xlsx")); + corrector.setTargetFile(new File("C:\\Users\\admin\\Downloads\\TARGET.xlsx")); + + corrector.check(); + corrector.createSnapshotData(); + corrector.removeMergedCells(); + corrector.replaceData(); + corrector.saveFile(); } }