Search in sources :

Example 1 with Table

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);
    }
}
Also used : ParseConfig(com.github.liaochong.myexcel.core.parser.ParseConfig) Table(com.github.liaochong.myexcel.core.parser.Table) IOException(java.io.IOException)

Example 2 with Table

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);
    }
}
Also used : Table(com.github.liaochong.myexcel.core.parser.Table) Sheet(org.apache.poi.ss.usermodel.Sheet) NoSuchFileException(java.nio.file.NoSuchFileException) Logger(org.slf4j.Logger) Tr(com.github.liaochong.myexcel.core.parser.Tr) IOException(java.io.IOException) File(java.io.File) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Objects(java.util.Objects) SheetStrategy(com.github.liaochong.myexcel.core.strategy.SheetStrategy) Workbook(org.apache.poi.ss.usermodel.Workbook) List(java.util.List) StringUtil(com.github.liaochong.myexcel.utils.StringUtil) Map(java.util.Map) WidthStrategy(com.github.liaochong.myexcel.core.strategy.WidthStrategy) HtmlTableParser(com.github.liaochong.myexcel.core.parser.HtmlTableParser) ParseConfig(com.github.liaochong.myexcel.core.parser.ParseConfig) Table(com.github.liaochong.myexcel.core.parser.Table) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 3 with Table

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);
}
Also used : Table(com.github.liaochong.myexcel.core.parser.Table) Sheet(org.apache.poi.ss.usermodel.Sheet) NoSuchFileException(java.nio.file.NoSuchFileException) Logger(org.slf4j.Logger) Tr(com.github.liaochong.myexcel.core.parser.Tr) IOException(java.io.IOException) File(java.io.File) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Objects(java.util.Objects) SheetStrategy(com.github.liaochong.myexcel.core.strategy.SheetStrategy) Workbook(org.apache.poi.ss.usermodel.Workbook) List(java.util.List) StringUtil(com.github.liaochong.myexcel.utils.StringUtil) Map(java.util.Map) WidthStrategy(com.github.liaochong.myexcel.core.strategy.WidthStrategy) HtmlTableParser(com.github.liaochong.myexcel.core.parser.HtmlTableParser) ParseConfig(com.github.liaochong.myexcel.core.parser.ParseConfig) Tr(com.github.liaochong.myexcel.core.parser.Tr)

Example 4 with Table

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;
}
Also used : Table(com.github.liaochong.myexcel.core.parser.Table)

Example 5 with 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;
}
Also used : ClassFieldContainer(com.github.liaochong.myexcel.core.reflect.ClassFieldContainer) Table(com.github.liaochong.myexcel.core.parser.Table) Consumer(java.util.function.Consumer) Tr(com.github.liaochong.myexcel.core.parser.Tr)

Aggregations

Table (com.github.liaochong.myexcel.core.parser.Table)6 ParseConfig (com.github.liaochong.myexcel.core.parser.ParseConfig)4 Tr (com.github.liaochong.myexcel.core.parser.Tr)4 IOException (java.io.IOException)4 HtmlTableParser (com.github.liaochong.myexcel.core.parser.HtmlTableParser)3 SheetStrategy (com.github.liaochong.myexcel.core.strategy.SheetStrategy)3 WidthStrategy (com.github.liaochong.myexcel.core.strategy.WidthStrategy)3 StringUtil (com.github.liaochong.myexcel.utils.StringUtil)3 File (java.io.File)3 NoSuchFileException (java.nio.file.NoSuchFileException)3 List (java.util.List)3 Map (java.util.Map)3 Objects (java.util.Objects)3 Sheet (org.apache.poi.ss.usermodel.Sheet)3 Workbook (org.apache.poi.ss.usermodel.Workbook)3 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)3 Logger (org.slf4j.Logger)3 ClassFieldContainer (com.github.liaochong.myexcel.core.reflect.ClassFieldContainer)1 Consumer (java.util.function.Consumer)1