Skip to content

Commit

Permalink
refactored :excel module
Browse files Browse the repository at this point in the history
  • Loading branch information
jlangch committed Aug 29, 2024
1 parent 3ef3a12 commit 1abd6bc
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public DocSection section() {
cells.addItem(diBuilder.getDocItem("excel/cell-hidden?", false));
cells.addItem(diBuilder.getDocItem("excel/cell-type", false));
cells.addItem(diBuilder.getDocItem("excel/cell-data-format-string", false));
cells.addItem(diBuilder.getDocItem("excel/copy-cell-style", false));
cells.addItem(diBuilder.getDocItem("excel/addr->string", false));

final DocSection rows = new DocSection("Rows", id());
Expand All @@ -110,6 +111,7 @@ public DocSection section() {
wr_data.addItem(diBuilder.getDocItem("excel/write-item", false));
wr_data.addItem(diBuilder.getDocItem("excel/write-value", false));
wr_data.addItem(diBuilder.getDocItem("excel/write-values", false));
wr_data.addItem(diBuilder.getDocItem("excel/write-values-keep-style", false));

final DocSection rd_data = new DocSection("Read Cells", id());
all.addSection(rd_data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

Expand Down Expand Up @@ -161,22 +159,23 @@ public void deleteRow(final int row) {
}
}

public void clearRow(final int row) {
public void clearRow(final int row, final boolean clearValues, final boolean clearStyles) {
final Row sourceRow = sheet.getRow(row);
if (sourceRow == null) {
return;
}

final List<Cell> cells = new ArrayList<>();
for (int ii = 0; ii < sourceRow.getLastCellNum(); ii++) {
final Cell cell = sourceRow.getCell(ii);
if (cell != null) {
cells.add(cell);
if (clearValues) {
cell.setBlank();
}
else if (clearStyles) {
cell.setCellStyle(null);
}
}
}

Collections.reverse(cells);
cells.forEach(c -> sourceRow.removeCell(c));
}

public void copyRowToEndOfSheet(final int row, final boolean copyValues, final boolean copyStyles) {
Expand Down Expand Up @@ -223,7 +222,7 @@ public void copyRow(final int rowFrom, final int rowTo, final boolean copyValues
return;
}

clearRow(rowTo);
clearRow(rowTo, true, copyStyles);

// Copy each cell from the source row to the dest row
for (int ii = 0; ii < sourceRow.getLastCellNum(); ii++) {
Expand Down Expand Up @@ -267,6 +266,22 @@ public void insertEmptyRows(final int row, final int count) {
}
}

public void copyCellStyle(
final int cellRowFrom,
final int cellColFrom,
final int cellRowTo,
final int cellColTo
) {
final Cell cellFrom = getCell(cellRowFrom, cellColFrom);

if (cellFrom != null) {
final Cell cellTo = getCellOrCreate(cellRowTo, cellColTo);

final CellStyle style = cellFrom.getCellStyle();
cellTo.setCellStyle(style);
}
}

public Object getValue(final int row, final int col) {
final Cell cell = getCell(row, col);
return getValue(cell);
Expand Down Expand Up @@ -329,87 +344,87 @@ public boolean isColumnHidden(final int col) {
public void setString(
final int row, final int col, final String value, final String styleName
) {
setCellValue(getCellCreate(row, col), value, styleName);
setCellValue(getCellOrCreate(row, col), value, styleName);
}

public void setString(final int row, final int col, final String value) {
setCellValue(getCellCreate(row, col), value, "string");
setCellValue(getCellOrCreate(row, col), value, "string");
}

public void setBoolean(final int row, final int col, final Boolean value, final String styleName) {
setCellValue(getCellCreate(row, col), value, styleName);
setCellValue(getCellOrCreate(row, col), value, styleName);
}

public void setBoolean(final int row, final int col, final Boolean value) {
setCellValue(getCellCreate(row, col), value, "boolean");
setCellValue(getCellOrCreate(row, col), value, "boolean");
}

public void setInteger(final int row, final int col, final Integer value, final String styleName) {
setCellValue(getCellCreate(row, col), value, styleName);
setCellValue(getCellOrCreate(row, col), value, styleName);
}

public void setInteger(final int row, final int col, final Integer value ) {
setCellValue(getCellCreate(row, col), value, "integer");
setCellValue(getCellOrCreate(row, col), value, "integer");
}

public void setInteger(final int row, final int col, final Long value, final String styleName) {
setCellValue(getCellCreate(row, col), value, styleName);
setCellValue(getCellOrCreate(row, col), value, styleName);
}

public void setInteger(final int row, final int col, final Long value ) {
setCellValue(getCellCreate(row, col), value, "integer");
setCellValue(getCellOrCreate(row, col), value, "integer");
}

public void setFloat(final int row, final int col, final Float value, final String styleName) {
setCellValue(getCellCreate(row, col), value, styleName);
setCellValue(getCellOrCreate(row, col), value, styleName);
}

public void setFloat(final int row, final int col, final Float value) {
setCellValue(getCellCreate(row, col), value, "float");
setCellValue(getCellOrCreate(row, col), value, "float");
}

public void setFloat(final int row, final int col, final Double value, final String styleName) {
setCellValue(getCellCreate(row, col), value, styleName);
setCellValue(getCellOrCreate(row, col), value, styleName);
}

public void setFloat(final int row, final int col, final Double value) {
setCellValue(getCellCreate(row, col), value, "float");
setCellValue(getCellOrCreate(row, col), value, "float");
}

public void setDate(final int row, final int col, final Date value, final String styleName) {
setCellValue(getCellCreate(row, col), value, styleName);
setCellValue(getCellOrCreate(row, col), value, styleName);
}

public void setDate(final int row, final int col, final LocalDate value, final String styleName) {
setCellValue(getCellCreate(row, col), value, styleName);
setCellValue(getCellOrCreate(row, col), value, styleName);
}

public void setDate(final int row, final int col, final LocalDateTime value, final String styleName) {
setCellValue(getCellCreate(row, col), value, styleName);
setCellValue(getCellOrCreate(row, col), value, styleName);
}

public void setDate(final int row, final int col, final ZonedDateTime value, final String styleName) {
setCellValue(getCellCreate(row, col), value, styleName);
setCellValue(getCellOrCreate(row, col), value, styleName);
}

public void setDate(final int row, final int col, final Date value) {
setCellValue(getCellCreate(row, col), value, "date");
setCellValue(getCellOrCreate(row, col), value, "date");
}

public void setDate(final int row, final int col, final LocalDate value) {
setCellValue(getCellCreate(row, col), value, "date");
setCellValue(getCellOrCreate(row, col), value, "date");
}

public void setDate(final int row, final int col, final LocalDateTime value) {
setCellValue(getCellCreate(row, col), value, "date");
setCellValue(getCellOrCreate(row, col), value, "date");
}

public void setDate(final int row, final int col, final ZonedDateTime value) {
setCellValue(getCellCreate(row, col), value, "date");
setCellValue(getCellOrCreate(row, col), value, "date");
}

public void setBlank(final int row, final int col) {
getCellCreate(row, col).setBlank();
getCellOrCreate(row, col).setBlank();
}

public void addImage(
Expand Down Expand Up @@ -571,6 +586,27 @@ public void setValue(final int row, final int col, final Object value, final Str
}
}

public void setValueKeepCellStyle(final int row, final int col, final Object value) {
if (value == null) {
setCellValue(getCellCreate(row, col), null, null);
}
else {
if (value instanceof String) setString(row, col, (String)value, null);
else if (value instanceof Boolean) setBoolean(row, col, (Boolean)value, null);
else if (value instanceof Integer) setInteger(row, col, (Integer)value, null);
else if (value instanceof Long) setInteger(row, col, (Long)value, null);
else if (value instanceof Float) setFloat(row, col, (Float)value, null);
else if (value instanceof Double) setFloat(row, col, (Double)value, null);
else if (value instanceof BigDecimal) setFloat(row, col, ((BigDecimal)value).doubleValue(), null);
else if (value instanceof BigInteger) setInteger(row, col, ((BigInteger)value).longValue(), null);
else if (value instanceof LocalDate) setDate(row, col, (LocalDate)value, null);
else if (value instanceof LocalDateTime) setDate(row, col, (LocalDateTime)value, null);
else if (value instanceof ZonedDateTime) setDate(row, col, (ZonedDateTime)value, null);
else if (value instanceof Date) setDate(row, col, (Date)value, null);
else throw new IllegalArgumentException("Invalid value type " + value.getClass().getSimpleName());
}
}

public void setBgColor(final int row, final int col, final Color bgColor) {
final Cell cell = getCellOrCreate(row, col);
if (cell != null) {
Expand Down Expand Up @@ -668,9 +704,11 @@ private void setImage(
}

private void setCellValue(final Cell cell, final Object value, final String styleName) {
final CellStyle style = cellStyles.getCellStyle(styleName);
if (style != null) {
cell.setCellStyle(style);
if (styleName != null) {
final CellStyle style = cellStyles.getCellStyle(styleName);
if (style != null) {
cell.setCellStyle(style);
}
}

if (value == null) {
Expand Down Expand Up @@ -718,7 +756,7 @@ else if (value instanceof ZonedDateTime) {

private Cell getCell(final int row, final int col) {
final Row r = sheet.getRow(row);
return r == null ? null : r.getCell(col, MissingCellPolicy.RETURN_BLANK_AS_NULL);
return r == null ? null : r.getCell(col, MissingCellPolicy.RETURN_NULL_AND_BLANK);
}

private Cell getCellCreate(final int row, final int col) {
Expand All @@ -727,7 +765,7 @@ private Cell getCellCreate(final int row, final int col) {

private Cell getCellOrCreate(final int row, final int col) {
final Row r = getRowCreate(row);
final Cell cell = r.getCell(col);
final Cell cell = r.getCell(col, MissingCellPolicy.RETURN_NULL_AND_BLANK);
return cell == null ? r.createCell(col) : cell;
}

Expand Down Expand Up @@ -977,7 +1015,7 @@ private String getDataFormatString(final Cell cell) {

final CellStyle style = cell.getCellStyle();
if (style == null) {
return null;
return null;
}

return style.getDataFormatString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ public void deleteRow(final int row1) {
sheet.deleteRow(row1-1);
}

public void copyRowToEndOfSheet(final int row1, final boolean copyValues, final boolean copyStyles) {
sheet.copyRowToEndOfSheet(row1-1, copyValues, copyStyles);
public void copyRow(final int row1From, final int row1To, final boolean copyValues, final boolean copyStyles) {
sheet.copyRow(row1From-1, row1To-1, copyValues, copyStyles);
}

public void clearRow(final int row1) {
sheet.clearRow(row1-1);
public void copyRowToEndOfSheet(final int row1, final boolean copyValues, final boolean copyStyles) {
sheet.copyRowToEndOfSheet(row1-1, copyValues, copyStyles);
}

public void copyRow(final int row1From, final int row1To, final boolean copyValues, final boolean copyStyles) {
sheet.copyRow(row1From-1, row1To-1, copyValues, copyStyles);
public void clearRow(final int row1, final boolean clearValues, final boolean clearStyles) {
sheet.clearRow(row1-1, clearValues, clearStyles);
}

public void insertEmptyRow(final int row1) {
Expand All @@ -150,6 +150,15 @@ public void insertEmptyRows(final int row1, final int count) {
sheet.insertEmptyRows(row1-1, count);
}

public void copyCellStyle(
final int cellRowFrom1,
final int cellColFrom1,
final int cellRowTo1,
final int cellColTo1
) {
sheet.copyCellStyle(cellRowFrom1-1, cellColFrom1-1, cellRowTo1-1, cellColTo1-1);
}

public ExcelSheetFacade<T> noHeader() {
this.noHeader = true;
return this;
Expand Down Expand Up @@ -223,6 +232,11 @@ public ExcelSheetFacade<T> value(final int row1, final int col1, final Object va
return this;
}

public ExcelSheetFacade<T> valueKeepCellStyle(final int row1, final int col1, final Object value) {
sheet.setValueKeepCellStyle(row1-1, col1-1, value);
return this;
}

public ExcelSheetFacade<T> image(
final int row1,
final int col1,
Expand Down
Loading

0 comments on commit 1abd6bc

Please sign in to comment.