Skip to content

Commit

Permalink
Merge pull request #339 from liaochong/feature/4.0.0-RC
Browse files Browse the repository at this point in the history
Feature/4.0.0 rc
  • Loading branch information
liaochong authored Dec 11, 2021
2 parents 8fd7441 + e14db7e commit d013eb7
Show file tree
Hide file tree
Showing 29 changed files with 621 additions and 381 deletions.
2 changes: 1 addition & 1 deletion example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>com.github.liaochong</groupId>
<artifactId>myexcel</artifactId>
<version>3.12.1</version>
<version>4.0.0.RC</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
Expand Down
34 changes: 15 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.github.liaochong</groupId>
<artifactId>myexcel</artifactId>
<version>3.12.1</version>
<version>4.0.0.RC</version>
<packaging>jar</packaging>

<name>myexcel</name>
Expand All @@ -22,17 +22,18 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<poi.version>4.1.2</poi.version>
<poi.version>5.1.0</poi.version>
<jsoup.version>1.14.3</jsoup.version>
<lombok.version>1.18.22</lombok.version>
<beetl.version>2.9.9</beetl.version>
<beetl.version>3.9.0.RELEASE</beetl.version>
<velocity.version>1.7</velocity.version>
<freemarker.version>2.3.23</freemarker.version>
<groovy-templates.version>2.4.13</groovy-templates.version>
<thymeleaf.version>2.1.6.RELEASE</thymeleaf.version>
<enjoy.version>4.8</enjoy.version>
<freemarker.version>2.3.31</freemarker.version>
<groovy-templates.version>3.0.9</groovy-templates.version>
<thymeleaf.version>3.0.14.RELEASE</thymeleaf.version>
<enjoy.version>4.9.16</enjoy.version>
<javax.servlet-api.version>4.0.1</javax.servlet-api.version>
<commons-compress.version>1.21</commons-compress.version>
<junit-jupiter-api.version>5.8.2</junit-jupiter-api.version>
<imageio-jpeg.version>3.7.0</imageio-jpeg.version>
</properties>

<dependencies>
Expand All @@ -42,20 +43,15 @@
<version>${poi.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-compress</artifactId>
<groupId>org.apache.commons</groupId>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-lite</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>${commons-compress.version}</version>
<artifactId>poi-ooxml-full</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
Expand Down Expand Up @@ -101,7 +97,7 @@
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.7.0</version>
<version>${imageio-jpeg.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
Expand All @@ -112,7 +108,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
<version>${junit-jupiter-api.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.github.liaochong.myexcel.core.style.TextAlignStyle;
import com.github.liaochong.myexcel.core.style.ThDefaultCellStyle;
import com.github.liaochong.myexcel.core.style.WordBreakStyle;
import com.github.liaochong.myexcel.exception.ExcelBuildException;
import com.github.liaochong.myexcel.utils.ColorUtil;
import com.github.liaochong.myexcel.utils.StringUtil;
import com.github.liaochong.myexcel.utils.TdUtil;
Expand All @@ -50,15 +51,13 @@
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.ShapeTypes;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.util.Units;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
Expand All @@ -68,6 +67,8 @@
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -85,6 +86,10 @@
*/
public abstract class AbstractExcelFactory implements ExcelFactory {

private static final Logger logger = LoggerFactory.getLogger(AbstractExcelFactory.class);

private static final int EMU_PER_MM = 36000;

protected Workbook workbook;
/**
* 是否为hssf
Expand Down Expand Up @@ -134,6 +139,10 @@ public abstract class AbstractExcelFactory implements ExcelFactory {
private CreationHelper createHelper;

private DataFormat format;
/**
* 图片路径缓存
*/
private Map<String, Integer> imageMapping;

@Override
public ExcelFactory useDefaultStyle() {
Expand All @@ -157,9 +166,6 @@ public ExcelFactory workbookType(WorkbookType workbookType) {
workbook = new HSSFWorkbook();
isHssf = true;
break;
case XLSX:
workbook = new XSSFWorkbook();
break;
case SXLSX:
workbook = new SXSSFWorkbook(1);
break;
Expand Down Expand Up @@ -257,10 +263,6 @@ protected void createCell(Td td, Sheet sheet, Row currentRow) {
} else {
String content = td.content;
switch (td.tdContentType) {
case STRING:
cell = currentRow.createCell(td.col, CellType.STRING);
cell.setCellValue(content);
break;
case DOUBLE:
cell = currentRow.createCell(td.col, CellType.NUMERIC);
if (null != content) {
Expand Down Expand Up @@ -351,9 +353,9 @@ private void setComment(Td td, Sheet sheet, Cell cell) {
}
ClientAnchor anchor = createHelper.createClientAnchor();
anchor.setCol1(cell.getColumnIndex());
anchor.setCol2(cell.getColumnIndex() + 1);
anchor.setCol2(cell.getColumnIndex() + 2);
anchor.setRow1(td.row);
anchor.setRow2(td.getRowBound());
anchor.setRow2(td.getRowBound() + 2);
Comment comment = drawing.createCellComment(anchor);
RichTextString str = createHelper.createRichTextString(td.comment.text);
comment.setString(str);
Expand Down Expand Up @@ -411,7 +413,7 @@ private void setPrompt(Td td, Sheet sheet) {
return;
}
DataValidationHelper dvHelper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = dvHelper.createCustomConstraint("*");
DataValidationConstraint constraint = dvHelper.createCustomConstraint("BB1");
CellRangeAddressList addressList = new CellRangeAddressList(
td.row, td.getRowBound(), td.col, td.getColBound());
DataValidation dataValidation = dvHelper.createValidation(constraint, addressList);
Expand All @@ -424,51 +426,62 @@ private void setImage(Td td, Sheet sheet) {
if (td.file == null) {
return;
}
try {
if (createHelper == null) {
createHelper = workbook.getCreationHelper();
}
byte[] bytes = Files.readAllBytes(td.file.toPath());
String fileName = td.file.getName();
int format;
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
switch (suffix) {
case "jpg":
case "jpeg":
format = Workbook.PICTURE_TYPE_JPEG;
break;
case "png":
format = Workbook.PICTURE_TYPE_PNG;
break;
case "dib":
format = Workbook.PICTURE_TYPE_DIB;
break;
case "emf":
format = Workbook.PICTURE_TYPE_EMF;
break;
case "pict":
format = Workbook.PICTURE_TYPE_PICT;
break;
case "wmf":
format = Workbook.PICTURE_TYPE_WMF;
break;
default:
throw new IllegalArgumentException("Invalid image type");
}
int pictureIdx = workbook.addPicture(bytes, format);
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = createHelper.createClientAnchor();
anchor.setDx1(isHssf ? 2 : Units.EMU_PER_PIXEL);
anchor.setDy1(isHssf ? 2 : Units.EMU_PER_PIXEL);
anchor.setCol1(td.col);
anchor.setRow1(td.row);
anchor.setCol2(td.getColBound());
anchor.setRow2(td.getRowBound());
Picture pict = drawing.createPicture(anchor, pictureIdx);
pict.resize(1, 1);
} catch (IOException e) {
throw new RuntimeException(e);
if (createHelper == null) {
createHelper = workbook.getCreationHelper();
}
String fileName = td.file.getName();
int format;
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
switch (suffix) {
case "jpg":
case "jpeg":
format = Workbook.PICTURE_TYPE_JPEG;
break;
case "png":
format = Workbook.PICTURE_TYPE_PNG;
break;
case "dib":
format = Workbook.PICTURE_TYPE_DIB;
break;
case "emf":
format = Workbook.PICTURE_TYPE_EMF;
break;
case "pict":
format = Workbook.PICTURE_TYPE_PICT;
break;
case "wmf":
format = Workbook.PICTURE_TYPE_WMF;
break;
default:
throw new IllegalArgumentException("Invalid image type");
}
ClientAnchor anchor = createHelper.createClientAnchor();
anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
anchor.setDx1(0);
anchor.setDy1(0);
anchor.setDx2(isHssf ? 1023 : 100 * EMU_PER_MM);
anchor.setDy2(isHssf ? 1023 : 99 * EMU_PER_MM);
anchor.setCol1(td.col);
anchor.setRow1(td.row);
anchor.setCol2(td.getColBound());
anchor.setRow2(td.getRowBound());
Drawing<?> drawing = sheet.getDrawingPatriarch();
if (drawing == null) {
drawing = sheet.createDrawingPatriarch();
}
if (imageMapping == null) {
imageMapping = new HashMap<>();
}
Integer pictureIdx = imageMapping.computeIfAbsent(td.file.getAbsolutePath(), s -> {
try {
byte[] bytes = Files.readAllBytes(td.file.toPath());
return workbook.addPicture(bytes, format);
} catch (IOException e) {
logger.error("read image failure", e);
throw new ExcelBuildException("read image failure, path:" + td.file.getAbsolutePath(), e);
}
});
drawing.createPicture(anchor, pictureIdx);
}

private Cell setLink(Td td, Row currentRow, HyperlinkType hyperlinkType) {
Expand Down Expand Up @@ -631,7 +644,7 @@ protected void freezePane(int tableIndex, Sheet sheet) {
if (freezePane == null) {
throw new IllegalStateException("FreezePane is null");
}
sheet.createFreezePane(freezePane.getColSplit(), freezePane.getRowSplit());
sheet.createFreezePane(freezePane.colSplit, freezePane.rowSplit);
}
}

Expand Down Expand Up @@ -699,6 +712,7 @@ protected void clearCache() {
maxTdHeightMap = new HashMap<>();
format = null;
createHelper = null;
imageMapping = null;
}

/**
Expand Down
Loading

0 comments on commit d013eb7

Please sign in to comment.