Search in sources :

Example 1 with WidthStrategy

use of com.github.liaochong.myexcel.core.strategy.WidthStrategy 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)

Aggregations

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