use of com.github.liaochong.myexcel.core.parser.Table in project myexcel by liaochong.
the class HtmlToExcelFactory method build.
/**
* 开始构建
*
* @return Workbook
*/
@Override
public Workbook build() {
try {
ParseConfig parseConfig = new ParseConfig(widthStrategy);
List<Table> tables = htmlTableParser.getAllTable(parseConfig);
htmlTableParser = null;
return this.build(tables);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.github.liaochong.myexcel.core.parser.Table in project myexcel by liaochong.
the class HtmlToExcelFactory method buildTablesWithOneSheet.
/**
* oneSheet 策略
*
* @param tables tables
*/
private void buildTablesWithOneSheet(List<Table> tables) {
String sheetName = this.getRealSheetName(tables.get(0).caption);
Sheet sheet = workbook.getSheet(sheetName);
if (sheet == null) {
sheet = workbook.createSheet(sheetName);
}
for (int i = 0; i < tables.size(); i++) {
Table table = tables.get(i);
boolean hasTd = table.trList.stream().map(tr -> tr.tdList).anyMatch(list -> !list.isEmpty());
if (!hasTd) {
continue;
}
// 设置单元格样式
this.setTdOfTable(table, sheet);
this.freezePane(i, sheet);
// 移除table
tables.set(i, null);
}
}
use of com.github.liaochong.myexcel.core.parser.Table in project myexcel by liaochong.
the class HtmlToExcelFactory method setTdOfTable.
/**
* 设置所有单元格,自适应列宽,单元格最大支持字符长度255
*/
private void setTdOfTable(Table table, Sheet sheet) {
int maxColIndex = 0;
if (WidthStrategy.isAutoWidth(widthStrategy) && !table.trList.isEmpty()) {
maxColIndex = table.trList.parallelStream().mapToInt(tr -> tr.tdList.stream().mapToInt(td -> td.col).max().orElse(0)).max().orElse(0);
}
Map<Integer, Integer> colMaxWidthMap = this.getColMaxWidthMap(table.trList);
// one sheet情况下重置非首个table的tr、td索引下标
int sheetLastRowIndex = sheet.getLastRowNum();
if (SheetStrategy.isOneSheet(sheetStrategy)) {
if (sheetLastRowIndex != 0) {
sheetLastRowIndex += 1;
}
}
for (int i = 0, size = table.trList.size(); i < size; i++) {
Tr tr = table.trList.get(i);
this.updateTrIndex(tr, sheetLastRowIndex);
this.createRow(tr, sheet);
tr.tdList = null;
}
table.trList = null;
this.setColWidth(colMaxWidthMap, sheet, maxColIndex);
}
use of com.github.liaochong.myexcel.core.parser.Table in project myexcel by liaochong.
the class AbstractSimpleExcelBuilder method createTable.
/**
* 创建table
*
* @return table
*/
protected Table createTable() {
Table table = new Table();
table.caption = configuration.sheetName;
table.trList = new LinkedList<>();
return table;
}
use of com.github.liaochong.myexcel.core.parser.Table in project myexcel by liaochong.
the class DefaultStreamExcelBuilder method start.
/**
* 流式构建启动,包含一些初始化操作
*
* @return DefaultExcelBuilder
*/
@Override
public DefaultStreamExcelBuilder<T> start() {
if (isMapBuild) {
this.parseGlobalStyle();
} else {
ClassFieldContainer classFieldContainer = ReflectUtil.getAllFieldsOfClass(dataType);
filteredFields = getFilteredFields(classFieldContainer, groups);
}
context.styleParser = styleParser;
htmlToExcelStreamFactory = new HtmlToExcelStreamFactory(context);
htmlToExcelStreamFactory.widthStrategy(configuration.widthStrategy);
if (workbook == null) {
htmlToExcelStreamFactory.workbookType(configuration.workbookType);
}
Table table = this.createTable();
List<Tr> head = this.createThead();
if (head != null) {
htmlToExcelStreamFactory.appendTitles(head);
}
htmlToExcelStreamFactory.start(table, workbook);
if (excel != null && Files.exists(excel)) {
log.info("start reading existing excel data.");
SaxExcelReader<T> reader = SaxExcelReader.of(dataType).readAllSheet();
if (titleLevel > 0) {
reader.rowFilter(row -> row.getRowNum() > titleLevel - 1);
}
reader.readThen(excel.toFile(), (Consumer<T>) this::append);
}
return this;
}
Aggregations