
br.com.tecsinapse.dataio.Table Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tecsinapse-data-io Show documentation
Show all versions of tecsinapse-data-io Show documentation
A simple way to work with CSV, XLSX? and TXT files
The newest version!
/*
* Tecsinapse Data Input and Output
*
* License: GNU Lesser General Public License (LGPL), version 3 or later
* See the LICENSE file in the root directory or .
*/
package br.com.tecsinapse.dataio;
import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import lombok.Getter;
import lombok.Setter;
import br.com.tecsinapse.dataio.style.Style;
import br.com.tecsinapse.dataio.style.TableCellStyle;
import br.com.tecsinapse.dataio.type.CellType;
import br.com.tecsinapse.dataio.util.WorkbookUtil;
public class Table {
private ExporterFormatter exporterFormatter;
private TableCellStyle tableCellStyleDefaultBody = Style.TABLE_CELL_STYLE_BODY_CENTER;
private String title;
private boolean autoSizeColumnSheet = true;
@Getter @Setter
private Integer minColumnWidth = 8 * 256;
@Getter @Setter
private Integer maxColumnWidth = 32 * 256;
private List> cells = new ArrayList<>();
@Getter
private Map colorsReplaceMap = new HashMap<>();
public Table() {
this(ExporterFormatter.ENGLISH);
}
public Table(ExporterFormatter exporterFormatter) {
this.exporterFormatter = exporterFormatter;
}
public void replace(TableCell cell, Integer row, Integer column) {
cells.get(row).set(column, cell);
}
public void replaceLastCell(TableCell cell) {
replaceLastColumn(getLastRow(), cell);
}
public TableCell getLastCell() {
return getLastRow().get(getLastColumnIndex(getLastRow()));
}
public String getLastCellContent() {
return getLastCell().getContentText();
}
public void replaceLastCellContent(String content) {
getLastCell().setContent(content);
}
public void replaceLastColumn(List row, TableCell cell) {
row.set(getLastColumnIndex(row), cell);
}
public Integer getLastColumnIndex(List row) {
return row.size() - 1;
}
public Integer getLastRowIndex() {
return cells.size() - 1;
}
public List getLastRow() {
return cells.get(getLastRowIndex());
}
public void addOnRow(TableCell cell, Integer row) {
cells.get(row).add(cell);
}
public void add(TableCell cell) {
getLastRow().add(cell);
}
public void add(Object content) {
add(new TableCell(content, getTableCellStyleDefaultBody()));
}
public void add(Object content, TableCellStyle tableCellStyle) {
add(new TableCell(content, tableCellStyle));
}
public void add(Object content, TableCellStyle tableCellStyle, int colspan) {
add(new TableCell(content, tableCellStyle, colspan));
}
public void add(Object content, TableCellStyle tableCellStyle, int colspan, int rowspan) {
add(new TableCell(content, tableCellStyle, colspan, rowspan));
}
public void add(Object content, TableCellStyle tableCellStyle, String cssStyle, int colspan) {
add(new TableCell(content, tableCellStyle, colspan).withStyle(cssStyle));
}
public void add(Object content, TableCellStyle tableCellStyle, String cssStyle, int colspan, int rowspan) {
add(new TableCell(content, tableCellStyle, colspan, rowspan).withStyle(cssStyle));
}
public void add(Object content, String cssStyle) {
add(new TableCell(content, getTableCellStyleDefaultBody()).withStyle(cssStyle));
}
public void add(Object content, int colspan) {
add(new TableCell(content, getTableCellStyleDefaultBody(), colspan));
}
public void add(Object content, int colspan, int rowspan) {
add(new TableCell(content, getTableCellStyleDefaultBody(), colspan, rowspan));
}
public void add(Object content, String cssStyle, int colspan) {
add(new TableCell(content, getTableCellStyleDefaultBody(), colspan).withStyle(cssStyle));
}
public void add(Object content, String cssStyle, int colspan, int rowspan) {
add(new TableCell(content, getTableCellStyleDefaultBody(), colspan, rowspan).withStyle(cssStyle));
}
public void add(Object content, CellType cellType) {
add(new TableCell(content, getTableCellStyleDefaultBody()).withCellType(cellType));
}
public void addOnNewRow(TableCell cell) {
addNewRow();
add(cell);
}
public void addNewRow() {
cells.add(new ArrayList());
}
public void addNewRow(List row) {
cells.add(row);
}
public List> getCells() {
return cells;
}
public void setCells(List> cells) {
this.cells = cells;
}
public void removeFirstRow() {
if (!cells.isEmpty()) {
cells.remove(0);
}
}
public void removeInitialRows(int numberRows) {
if (cells.size() > numberRows) {
for (int i = 0; i < numberRows; i++) {
cells.remove(0);
}
}
}
public List> toStringMatrix() {
int rows = cells.size();
int columns = getBiggerRowSize();
List> matrix = new ArrayList<>();
boolean[][] spanMark = new boolean[rows][];
for (int r = 0; r < rows; ++r) {
spanMark[r] = new boolean[columns];
matrix.add(new ArrayList());
for (int c = 0; c < columns; ++c) {
matrix.get(r).add("");
spanMark[r][c] = false;
}
}
int r = 0;
int c = 0;
for (List row : cells) {
for (TableCell cell : row) {
// � spanMark e ainda tem colunas
while (spanMark[r][c] && c < spanMark[r].length - 1) {
c++;
}
if (!spanMark[r][c]) {
matrix.get(r).set(c, cell.getFormattedContentInternalFirst(getExporterFormatter()));
int rowspan = cell.getRowspan();
int colspan = cell.getColspan();
if (rowspan == 1 && colspan > 1) {
for (int i = 1; i < colspan; ++i) {
spanMark[r][c + i] = true;
}
}
if (colspan == 1 && rowspan > 1) {
for (int i = 1; i < rowspan; ++i) {
spanMark[r + i][c] = true;
}
}
if (colspan > 1 && rowspan > 1) {
for (int i = 1; i < colspan; ++i) {
for (int j = 1; j < rowspan; ++j) {
spanMark[r + j][c + i] = true;
}
}
}
}
c++;
}
c = 0;
r++;
}
return matrix;
}
public List> toTableCellMatrix() {
int rows = cells.size();
int columns = getBiggerRowSize();
List> matrix = new ArrayList<>();
boolean[][] spanMark = new boolean[rows][];
for (int r = 0; r < rows; ++r) {
spanMark[r] = new boolean[columns];
matrix.add(new ArrayList());
for (int c = 0; c < columns; ++c) {
matrix.get(r).add(EmptyTableCell.EMPTY_CELL);
spanMark[r][c] = false;
}
}
int r = 0;
int c = 0;
for (List row : cells) {
for (TableCell cell : row) {
// � spanMark e ainda tem colunas
while (spanMark[r][c] && c < spanMark[r].length - 1) {
c++;
}
if (!spanMark[r][c]) {
matrix.get(r).set(c, cell);
int rowspan = cell.getRowspan();
int colspan = cell.getColspan();
if (rowspan == 1 && colspan > 1) {
for (int i = 1; i < colspan; ++i) {
spanMark[r][c + i] = true;
}
}
if (colspan == 1 && rowspan > 1) {
for (int i = 1; i < rowspan; ++i) {
spanMark[r + i][c] = true;
}
}
if (colspan > 1 && rowspan > 1) {
for (int jr = r; jr < (r + rowspan); jr++) {
for (int ic = c; ic < (c + colspan); ic++) {
if (ic != c || jr != r) {
spanMark[jr][ic] = true;
}
}
}
}
}
c++;
}
c = 0;
r++;
}
return matrix;
}
private int getBiggerRowSize() {
int biggerRowSize = 0;
for (List row : cells) {
int qtdColumns = 0;
for (TableCell column : row) {
qtdColumns += column.getColspan();
if (qtdColumns > biggerRowSize) {
biggerRowSize = qtdColumns;
}
}
}
return biggerRowSize;
}
public String getStringMatrixAsString(List> matrix) {
StringBuilder sb = new StringBuilder();
for (List row : matrix) {
for (String cell : row) {
sb.append("|");
sb.append(cell);
}
sb.append("\n");
}
return sb.toString();
}
public Workbook toXSSFWorkBook() {
return toWorkBook(new XSSFWorkbook());
}
public Workbook toSXSSFWorkBook() {
return toWorkBook(new SXSSFWorkbook());
}
public Workbook toHSSFWorkBook() {
return toWorkBook(new HSSFWorkbook());
}
public Workbook toWorkBook(Workbook wb) {
return WorkbookUtil.newWorkbookUtil().toWorkBook(wb, this);
}
public void addAll(List values) {
if (values != null) {
for (String value : values) {
add(value);
}
}
}
public void addAllCells(List values) {
if (values != null) {
for (TableCell cell : values) {
add(cell);
}
}
}
public void addAll(List row, List values) {
row.addAll(values);
}
public void concatenateTableOnRight(Table table) {
// completa se necessario
int rows = this.getLastRowIndex();
int rowsOut = table.getLastRowIndex();
if (rows < rowsOut) {
int biggerRow = this.getBiggerRowSize();
List emptyCells = new ArrayList<>();
for (int i = 0; i < biggerRow; ++i) {
emptyCells.add(EmptyTableCell.EMPTY_CELL);
}
for (int i = 0; i < rowsOut - rows; ++i) {
this.addNewRow();
this.addAllCells(emptyCells);
}
}
for (List row : this.cells) {
row.add(EmptyTableCell.EMPTY_CELL);
}
// concatena linhas
for (int i = 0; i < table.cells.size(); ++i) {
List row = table.cells.get(i);
addAll(this.cells.get(i), row);
}
}
public void concatenateTableBelow(Table table) {
// completa se necessario
int biggerRow = this.getBiggerRowSize();
List emptyCells = new ArrayList<>();
for (int i = 0; i < biggerRow; ++i) {
emptyCells.add(new TableCell(" ", getTableCellStyleDefaultBody()));
}
this.addNewRow();
this.addAllCells(emptyCells);
// concatena linhas
for (int i = 0; i < table.cells.size(); ++i) {
List row = table.cells.get(i);
this.addNewRow();
this.addAllCells(row);
}
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getNextColumnIndexOfLastRow() {
return getLastColumnIndex(getLastRow()) + 1;
}
/**
* Este método identifica se ao escrever as colunas da planilha deve ser
* utilizado o método autoSizeColumn
do objeto
* org.apache.poi.ss.usermodel.Sheet
.
* Se retornar false as colunas serão redimencionadas realizando um
* cálculo considerando o maior número de caracteres existente em uma coluna
*
* @return {@code true} caso esteja configurado {@code auto size}. {@code false} caso contrário
*/
//#workaround para resolver o problema do tamanho das colunas com valor zero
public boolean isAutoSizeColumnSheet() {
return autoSizeColumnSheet;
}
public void setAutoSizeColumnSheet(boolean autoSizeColumnSheet) {
this.autoSizeColumnSheet = autoSizeColumnSheet;
}
public ExporterFormatter getExporterFormatter() {
return exporterFormatter;
}
public void setTableCellStyleDefaultBody(TableCellStyle tableCellStyleDefaultBody) {
this.tableCellStyleDefaultBody = tableCellStyleDefaultBody;
}
public TableCellStyle getTableCellStyleDefaultBody() {
return tableCellStyleDefaultBody != null ? tableCellStyleDefaultBody : Style.TABLE_CELL_STYLE_BODY_CENTER;
}
public void setExporterFormatter(ExporterFormatter exporterFormatter) {
this.exporterFormatter = exporterFormatter;
}
public Table withNewRow() {
addNewRow();
return this;
}
public Table withCell(TableCell tableCell) {
add(tableCell);
return this;
}
public Table withCell(Object content) {
add(content);
return this;
}
public Table withCell(Object content, TableCellStyle tableCellStyle) {
add(content, tableCellStyle);
return this;
}
public Table withCell(Object content, TableCellStyle tableCellStyle, int colspan) {
add(content, tableCellStyle, colspan);
return this;
}
public List getTableCellStyles() {
List tableCellStyles = new ArrayList<>();
for (List cellsTable : getCells()) {
for (TableCell cell : cellsTable) {
if (!tableCellStyles.contains(cell.getTableCellStyle())) {
tableCellStyles.add(cell.getTableCellStyle());
}
}
}
return tableCellStyles;
}
public Set getAllColors() {
return getTableCellStyles().stream()
.flatMap(tcs -> Stream.of(tcs.getBackgroundColor(), tcs.getFontColor()))
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}
private HSSFColor newCustomColor(short index, Color newColor) {
return new DataIOCustomColor(index, newColor);
}
/**
* @deprecated in future release, replaced by {@link #newCustomColor(Color newColor)}
*/
@Deprecated
public HSSFColor newCustomColor(HSSFColor replaceColor, Color newColor) {
return newCustomColor(replaceColor.getIndex(), newColor);
}
public HSSFColor newCustomColor(Color newColor) {
return newCustomColor((short)-1, newColor);
}
public int getMinOrMaxOrActualCellWidth(int value) {
if (minColumnWidth == null && maxColumnWidth == null) {
return value;
}
if (minColumnWidth != null && value < minColumnWidth) {
return minColumnWidth;
}
if (maxColumnWidth != null && value > maxColumnWidth) {
return maxColumnWidth;
}
return value;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy