Archived
0
This commit is contained in:
2018-07-19 18:08:08 +03:00
parent 61d4897720
commit c7e3aaf39f
7 changed files with 132 additions and 27 deletions

View File

@@ -2,9 +2,7 @@ package ru.dmitriymx.corrector1s;
import lombok.Setter; import lombok.Setter;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import ru.dmitriymx.corrector1s.config.ColumnData; 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.ExcelDocument;
import ru.dmitriymx.corrector1s.excel.ExcelRecord; import ru.dmitriymx.corrector1s.excel.ExcelRecord;
import ru.dmitriymx.corrector1s.excel.ExcelRecordData; import ru.dmitriymx.corrector1s.excel.ExcelRecordData;
import ru.dmitriymx.corrector1s.excel.ExcelRecordFormula;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import java.io.File; import java.io.File;
@@ -28,6 +27,8 @@ public class Corrector1S {
@Setter @Setter
private File targetFile; private File targetFile;
@Setter @Setter
private XmlConfig config;
@Setter
private double honorarium; private double honorarium;
@Setter @Setter
private double fss; private double fss;
@@ -96,18 +97,32 @@ public class Corrector1S {
* Убираем объединение ячеек * Убираем объединение ячеек
*/ */
public void removeMergedCells() { public void removeMergedCells() {
/* // мега костыль! а всё потому, что по человечески это, блин, не сделать
Опытным путём было выявлено, что должно остаться 5 объединённых регионов, если у всех остальных колонок loop:
убрать объединение. while(true) {
*/ /*
while (this.document.getNumMergedCells() > 5) { Опытным путём было выявлено, что должно остаться 5 объединённых регионов, если у всех остальных колонок
for(int m = 0; m < this.document.getNumMergedCells(); m++) { убрать объединение.
CellRangeAddress cellRangeAddress = this.document.getMergedCells(m); */
if (cellRangeAddress.getFirstColumn() >= 8) { if (this.document.getNumMergedCells() > 5) {
this.document.removeMergedCells(m); int m = 0;
break;
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() { public void replaceData() {
XmlConfig config = null;
try {
config = new XmlConfig(new File("config.xml"));
} catch (ParserConfigurationException | IOException | SAXException e) {
e.printStackTrace(); //FIXME логирование ошибки
}
List<ColumnData> columnDataList = config.getColumnDataList(); List<ColumnData> columnDataList = config.getColumnDataList();
Iterator<ColumnData> itrColumnDataList = columnDataList.iterator(); Iterator<ColumnData> itrColumnDataList = columnDataList.iterator();
for (int column = ExcelDocument.START_COLUMN; column <= document.getMaxColumn(); column++) { int column;
if (itrColumnDataList.hasNext()) {
loopColumn:
for (column = ExcelDocument.START_COLUMN; column <= document.getMaxColumn(); column++) {
loopColumnData:
while (itrColumnDataList.hasNext()) {
ColumnData columnData = itrColumnDataList.next(); ColumnData columnData = itrColumnDataList.next();
ExcelRecord record = mapRecords.remove(buildKeyRecord(columnData.getTitle(), columnData.getAddon())); ExcelRecord record = mapRecords.remove(buildKeyRecord(columnData.getTitle(), columnData.getAddon()));
if (record == null) continue; //FIXME логирование отсутствия нужной колонки if (record == null)
continue loopColumnData;
this.document.setColumnRecord(column, record); this.document.setColumnRecord(column, record);
continue loopColumn;
//TODO не забываем, нужно еще составить мапу id-column для дальнейшей генерации формул //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);
} }
} }

View File

@@ -6,8 +6,10 @@ package ru.dmitriymx.corrector1s;
public class Utils { public class Utils {
public static boolean isEmptyObject(Object obj) { public static boolean isEmptyObject(Object obj) {
if (obj instanceof Number) { if (obj instanceof Integer) {
return (Integer) obj == 0; return (Integer) obj == 0;
} else if (obj instanceof Double) {
return (Double) obj == 0.0d;
} else { } else {
return obj == null || obj.toString().trim().isEmpty(); return obj == null || obj.toString().trim().isEmpty();
} }

View File

@@ -19,6 +19,7 @@ public class ExcelDocument implements Closeable {
LINE_DATA = 8, LINE_DATA = 8,
START_COLUMN = 7; START_COLUMN = 7;
@Getter
private Workbook workbook; private Workbook workbook;
private Sheet sheet; private Sheet sheet;
@Getter @Getter
@@ -63,14 +64,26 @@ public class ExcelDocument implements Closeable {
if (line == LINE_TITLE) { if (line == LINE_TITLE) {
record.setTitle(cell.getStringCellValue()); record.setTitle(cell.getStringCellValue());
record.setTitleStyle(cell.getCellStyle());
} else if (line == LINE_ADDON) { } else if (line == LINE_ADDON) {
if (cell.getCellTypeEnum().equals(CellType.NUMERIC)) { if (cell.getCellTypeEnum().equals(CellType.NUMERIC)) {
record.setAddon(new Double(cell.getNumericCellValue()).intValue()); record.setAddon(new Double(cell.getNumericCellValue()).intValue());
} else { } else {
record.setAddon(0); record.setAddon(0);
} }
record.setAddonStyle(cell.getCellStyle());
} else { } 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 cell = this.sheet.getRow(LINE_TITLE).getCell(column, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
cell.setCellType(CellType.STRING); cell.setCellType(CellType.STRING);
cell.setCellValue(record.getTitle()); cell.setCellValue(record.getTitle());
cell.setCellStyle(record.getTitleStyle());
// ADDON // ADDON
cell = this.sheet.getRow(LINE_ADDON).getCell(column, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); 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.setCellType(CellType.STRING);
cell.setCellValue((String)record.getAddon()); cell.setCellValue((String)record.getAddon());
} }
cell.setCellStyle(record.getAddonStyle());
// DATA // DATA
for (int line = LINE_DATA; line <= this.maxLines; line++) { for (int line = LINE_DATA; line <= this.maxLines; line++) {
@@ -133,7 +148,7 @@ public class ExcelDocument implements Closeable {
} }
public void removeMergedCells(int id) { public void removeMergedCells(int id) {
this.sheet.removeMergedRegion(id);
} }
public void saveToFile(File file) throws IOException { public void saveToFile(File file) throws IOException {

View File

@@ -4,15 +4,23 @@
*/ */
package ru.dmitriymx.corrector1s.excel; package ru.dmitriymx.corrector1s.excel;
import org.apache.poi.ss.usermodel.CellStyle;
import java.util.List; import java.util.List;
public interface ExcelRecord<T1, T2> { public interface ExcelRecord<T1, T2> {
String getTitle(); String getTitle();
void setTitle(String value); void setTitle(String value);
CellStyle getTitleStyle();
void setTitleStyle(CellStyle style);
T1 getAddon(); T1 getAddon();
void setAddon(T1 value); void setAddon(T1 value);
CellStyle getAddonStyle();
void setAddonStyle(CellStyle style);
List<T2> getDataList(); List<T2> getDataList();
boolean isEmptyAddon(); boolean isEmptyAddon();

View File

@@ -7,6 +7,7 @@ package ru.dmitriymx.corrector1s.excel;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.apache.poi.ss.usermodel.CellStyle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -17,7 +18,11 @@ public class ExcelRecordData implements ExcelRecord<Integer, Double> {
@Setter @Setter
private String title; private String title;
@Setter @Setter
private CellStyle titleStyle;
@Setter
private Integer addon; private Integer addon;
@Setter
private CellStyle addonStyle;
private List<Double> dataList; private List<Double> dataList;
ExcelRecordData(int capacity) { ExcelRecordData(int capacity) {

View File

@@ -7,6 +7,7 @@ package ru.dmitriymx.corrector1s.excel;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.apache.poi.ss.usermodel.CellStyle;
import java.util.List; import java.util.List;
@@ -16,7 +17,11 @@ public class ExcelRecordFormula implements ExcelRecord<String, String> {
@Setter @Setter
private String title; private String title;
@Setter @Setter
private CellStyle titleStyle;
@Setter
private String addon; private String addon;
@Setter
private CellStyle addonStyle;
private List<String> dataList; private List<String> dataList;
@Override @Override

View File

@@ -6,12 +6,26 @@ package ru.dmitriymx.corrector1s;
import ru.dmitriymx.corrector1s.config.XmlConfig; import ru.dmitriymx.corrector1s.config.XmlConfig;
import java.io.File;
import java.io.InputStream; import java.io.InputStream;
public class Test { public class Test {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
InputStream stream = Test.class.getResourceAsStream("/config.xml"); InputStream stream = Test.class.getResourceAsStream("/config.xml");
XmlConfig config = new XmlConfig(stream); 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();
} }
} }