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 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() {
// мега костыль! а всё потому, что по человечески это, блин, не сделать
loop:
while(true) {
/*
Опытным путём было выявлено, что должно остаться 5 объединённых регионов, если у всех остальных колонок
убрать объединение.
*/
while (this.document.getNumMergedCells() > 5) {
for(int m = 0; m < this.document.getNumMergedCells(); m++) {
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() >= 8) {
if (cellRangeAddress.getFirstColumn() >= ExcelDocument.START_COLUMN) {
this.document.removeMergedCells(m);
break;
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<ColumnData> columnDataList = config.getColumnDataList();
Iterator<ColumnData> 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);
}
}

View File

@@ -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();
}

View File

@@ -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 {
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 {

View File

@@ -4,15 +4,23 @@
*/
package ru.dmitriymx.corrector1s.excel;
import org.apache.poi.ss.usermodel.CellStyle;
import java.util.List;
public interface ExcelRecord<T1, T2> {
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<T2> getDataList();
boolean isEmptyAddon();

View File

@@ -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<Integer, Double> {
@Setter
private String title;
@Setter
private CellStyle titleStyle;
@Setter
private Integer addon;
@Setter
private CellStyle addonStyle;
private List<Double> dataList;
ExcelRecordData(int capacity) {

View File

@@ -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<String, String> {
@Setter
private String title;
@Setter
private CellStyle titleStyle;
@Setter
private String addon;
@Setter
private CellStyle addonStyle;
private List<String> dataList;
@Override

View File

@@ -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();
}
}