Skip to content

Commit

Permalink
Merge pull request #369 from liaochong/feature/4.2.0
Browse files Browse the repository at this point in the history
Feature/4.2.0
  • Loading branch information
liaochong authored May 3, 2022
2 parents 2191bcf + ff845dd commit b993d4a
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 87 deletions.
2 changes: 1 addition & 1 deletion 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>4.1.1</version>
<version>4.2.0</version>
<packaging>jar</packaging>

<name>myexcel</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,19 +763,23 @@ protected Map<Integer, Integer> getColMaxWidthMap(List<Tr> trList) {
protected void setColWidth(Map<Integer, Integer> colMaxWidthMap, Sheet sheet, int maxColIndex) {
if (WidthStrategy.isAutoWidth(widthStrategy)) {
if (sheet instanceof SXSSFSheet) {
throw new UnsupportedOperationException("SXSSF does not support automatic width at this time");
((SXSSFSheet) sheet).trackAllColumnsForAutoSizing();
}
for (int i = 0; i <= maxColIndex; i++) {
sheet.autoSizeColumn(i);
}
}
colMaxWidthMap.forEach((key, value) -> {
int contentLength = value << 1;
if (contentLength > 255) {
contentLength = 255;
if (sheet instanceof SXSSFSheet) {
((SXSSFSheet) sheet).untrackAllColumnsForAutoSizing();
}
sheet.setColumnWidth(key, contentLength << 8);
});
} else {
colMaxWidthMap.forEach((key, value) -> {
int contentLength = value << 1;
if (contentLength > 255) {
contentLength = 255;
}
sheet.setColumnWidth(key, contentLength << 8);
});
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.github.liaochong.myexcel.core.container.Pair;
import com.github.liaochong.myexcel.core.converter.ConvertContext;
import com.github.liaochong.myexcel.core.converter.WriteConverterContext;
import com.github.liaochong.myexcel.core.converter.writer.LocalTimeWriteConverter;
import com.github.liaochong.myexcel.core.parser.ContentTypeEnum;
import com.github.liaochong.myexcel.core.parser.StyleParser;
import com.github.liaochong.myexcel.core.parser.Table;
Expand All @@ -47,6 +48,7 @@
import java.lang.reflect.Modifier;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -332,10 +334,14 @@ protected Tr createTr(List<Pair<? extends Class, ?>> contents) {
}
this.setTdContent(td, pair);
this.setTdContentType(td, pair.getKey());
td.format = formats.get(index);
this.setFormula(index, td);
if (isMapBuild) {
this.setDateFormatForMap(pair.getKey(), td);
} else {
td.format = formats.get(index);
this.setFormula(index, td);
this.setPrompt(td, index);
}
this.setTdWidth(tr.colWidthMap, td);
this.setPrompt(td, index);
return td;
}).collect(Collectors.toList());
customWidthMap.forEach(tr.colWidthMap::put);
Expand Down Expand Up @@ -613,7 +619,21 @@ private void doAddToContents(List<Pair<? extends Class, ?>> contents, Object v)
if (v instanceof Pair && ((Pair) v).getKey() instanceof Class) {
contents.add((Pair) v);
} else {
contents.add(Pair.of(v == null ? NullType.class : v.getClass(), v));
if (v == null) {
contents.add(Pair.of(NullType.class, null));
} else if (v.getClass() == LocalTime.class) {
contents.add(LocalTimeWriteConverter.doConvertDate((LocalTime) v, Constants.DEFAULT_LOCAL_TIME_FORMAT));
} else {
contents.add(Pair.of(v.getClass(), v));
}
}
}

private void setDateFormatForMap(Class<?> objectClass, Td td) {
if (objectClass == LocalDateTime.class || objectClass == Date.class) {
td.format = Constants.DEFAULT_DATE_TIME_FORMAT;
} else if (objectClass == LocalDate.class) {
td.format = Constants.DEFAULT_DATE_FORMAT;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public class Configuration {
/**
* LocalDate类型数据全局格式化
*/
public String dateFormat = "yyyy-MM-dd";
public String dateFormat = Constants.DEFAULT_DATE_FORMAT;
/**
* Date、LocalDateTime类型数据全局格式化
*/
public String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
public String dateTimeFormat = Constants.DEFAULT_DATE_TIME_FORMAT;
/**
* LocalTime格式化
*/
public String localTimeFormat = "HH:mm:ss";
public String localTimeFormat = Constants.DEFAULT_LOCAL_TIME_FORMAT;
/**
* 数值类全局格式化
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.io.Closeable;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
Expand All @@ -37,7 +36,7 @@ public class DefaultExcelBuilder<T> implements Closeable {

private static final String STYLE_TITLE = "font-weight:bold;font-size:14;text-align:center;vertical-align:middle;";

private DefaultStreamExcelBuilder<T> streamExcelBuilder;
private final DefaultStreamExcelBuilder<T> streamExcelBuilder;

private DefaultExcelBuilder(DefaultStreamExcelBuilder<T> streamExcelBuilder) {
streamExcelBuilder.widthStrategy(WidthStrategy.COMPUTE_AUTO_WIDTH);
Expand All @@ -63,29 +62,6 @@ public static <T> DefaultExcelBuilder<T> of(Class<T> dataType, Workbook workbook
return new DefaultExcelBuilder<>(DefaultStreamExcelBuilder.of(dataType, workbook));
}

/**
* 已过时,获取实例,请使用of方法代替
*
* @return DefaultExcelBuilder
*/
@Deprecated
public static DefaultExcelBuilder<Map> getInstance() {
DefaultExcelBuilder<Map> defaultExcelBuilder = new DefaultExcelBuilder<>(DefaultStreamExcelBuilder.getInstance());
defaultExcelBuilder.streamExcelBuilder.workbookType(WorkbookType.XLSX);
return defaultExcelBuilder;
}

/**
* 已过时,获取实例,请使用of方法代替
*
* @param workbook workbook
* @return DefaultExcelBuilder
*/
@Deprecated
public static DefaultExcelBuilder<Map> getInstance(Workbook workbook) {
return new DefaultExcelBuilder<>(DefaultStreamExcelBuilder.getInstance(workbook));
}

public DefaultExcelBuilder<T> titles(List<String> titles) {
streamExcelBuilder.titles(titles);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,29 +152,6 @@ public static <T> DefaultStreamExcelBuilder<T> of(Class<T> dataType, InputStream
return of(dataType, TempFileOperator.convertToFile(excelInputStream));
}

/**
* 已过时,请使用of方法代替
* 4.0版本移除
*
* @return DefaultStreamExcelBuilder
*/
@Deprecated
public static DefaultStreamExcelBuilder<Map> getInstance() {
return new DefaultStreamExcelBuilder<>(Map.class);
}

/**
* 已过时,请使用of方法代替
* 4.0版本移除
*
* @param workbook 工作簿
* @return DefaultStreamExcelBuilder
*/
@Deprecated
public static DefaultStreamExcelBuilder<Map> getInstance(Workbook workbook) {
return new DefaultStreamExcelBuilder<>(Map.class, workbook);
}

public DefaultStreamExcelBuilder<T> titles(List<String> titles) {
this.titles = titles;
return this;
Expand All @@ -198,11 +175,6 @@ public DefaultStreamExcelBuilder<T> workbookType(WorkbookType workbookType) {
return this;
}

/**
* 设置为无样式
*
* @return DefaultStreamExcelBuilder
*/
public DefaultStreamExcelBuilder<T> noStyle() {
this.styleParser.setNoStyle(true);
return this;
Expand Down Expand Up @@ -281,11 +253,6 @@ public DefaultStreamExcelBuilder<T> waitQueueSize(int waitQueueSize) {
return this;
}

@Deprecated
public DefaultStreamExcelBuilder<T> globalStyle(String... styles) {
return style(styles);
}

public DefaultStreamExcelBuilder<T> style(String... styles) {
this.styleParser.setNoStyle(false);
configuration.style = Arrays.stream(styles).collect(Collectors.toSet());
Expand All @@ -297,7 +264,7 @@ public DefaultStreamExcelBuilder<T> templateHandler(Class<? extends TemplateHand
return this;
}

public DefaultStreamExcelBuilder<T> startSheet(Consumer<Sheet> startSheetConsumer) {
public DefaultStreamExcelBuilder<T> onStartSheet(Consumer<Sheet> startSheetConsumer) {
this.context.startSheetConsumer = startSheetConsumer;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2019 liaochong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.liaochong.myexcel.core;

import com.github.liaochong.myexcel.core.templatehandler.TemplateHandler;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.Closeable;
import java.io.IOException;
import java.util.Map;

/**
* @author liaochong
* @version 4.2.0
*/
public class TemplateStreamExcelBuilder implements Closeable {

private DefaultStreamExcelBuilder<Void> defaultStreamExcelBuilder;

private TemplateStreamExcelBuilder() {
}

public static TemplateStreamExcelBuilder of(Class<? extends TemplateHandler> templateHandlerClass) {
TemplateStreamExcelBuilder templateStreamExcelBuilder = new TemplateStreamExcelBuilder();
templateStreamExcelBuilder.defaultStreamExcelBuilder = DefaultStreamExcelBuilder.of(Void.class)
.templateHandler(templateHandlerClass)
.start();
return templateStreamExcelBuilder;
}

public void append(String templateFilePath, Map<String, Object> renderData) {
defaultStreamExcelBuilder.append(templateFilePath, renderData);
}

public void append(String templateDir, String templateFileName, Map<String, Object> renderData) {
defaultStreamExcelBuilder.append(templateDir, templateFileName, renderData);
}

public Workbook build() {
return defaultStreamExcelBuilder.build();
}

@Override
public void close() throws IOException {
defaultStreamExcelBuilder.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,11 @@ public class Constants {

public static final String EQUAL = "=";

public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

public static final String DEFAULT_LOCAL_TIME_FORMAT = "HH:mm:ss";

public static final Pair<Class, Object> NULL_PAIR = Pair.of(NullType.class, null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.util.Date;

/**
Expand All @@ -50,19 +51,13 @@ public Pair<Class, Object> convert(Field field, Class<?> fieldType, Object field
// 时间格式化
String dateFormatPattern = getDateFormatPattern(convertContext, field, fieldType);
if (fieldType == LocalDateTime.class) {
LocalDateTime localDateTime = (LocalDateTime) fieldVal;
DateTimeFormatter formatter = getDateTimeFormatter(dateFormatPattern);
return Pair.of(String.class, formatter.format(localDateTime));
return doConvertDate((LocalDateTime) fieldVal, dateFormatPattern);
} else if (fieldType == LocalDate.class) {
LocalDate localDate = (LocalDate) fieldVal;
DateTimeFormatter formatter = getDateTimeFormatter(dateFormatPattern);
return Pair.of(String.class, formatter.format(localDate));
return doConvertDate((LocalDate) fieldVal, dateFormatPattern);
} else if (fieldType == LocalTime.class) {
LocalTime localTime = (LocalTime) fieldVal;
DateTimeFormatter formatter = getDateTimeFormatter(dateFormatPattern);
return Pair.of(String.class, formatter.format(localTime));
return doConvertDate((LocalTime) fieldVal, dateFormatPattern);
}
SimpleDateFormat simpleDateFormat = this.getSimpleDateFormat(dateFormatPattern);
SimpleDateFormat simpleDateFormat = getSimpleDateFormat(dateFormatPattern);
return Pair.of(String.class, simpleDateFormat.format((Date) fieldVal));
}

Expand All @@ -84,7 +79,7 @@ protected String getDateFormatPattern(ConvertContext convertContext, Field field
* @param dateFormat 时间格式化
* @return DateTimeFormatter
*/
protected DateTimeFormatter getDateTimeFormatter(String dateFormat) {
protected static DateTimeFormatter getDateTimeFormatter(String dateFormat) {
DateTimeFormatter formatter = DATETIME_FORMATTER_CONTAINER.get(dateFormat);
if (formatter == null) {
formatter = DateTimeFormatter.ofPattern(dateFormat);
Expand All @@ -93,12 +88,17 @@ protected DateTimeFormatter getDateTimeFormatter(String dateFormat) {
return formatter;
}

private SimpleDateFormat getSimpleDateFormat(String dateFormatPattern) {
private static SimpleDateFormat getSimpleDateFormat(String dateFormatPattern) {
ThreadLocal<SimpleDateFormat> tl = SIMPLE_DATE_FORMAT_WEAK_CACHE.get(dateFormatPattern);
if (tl == null) {
tl = ThreadLocal.withInitial(() -> new SimpleDateFormat(dateFormatPattern));
SIMPLE_DATE_FORMAT_WEAK_CACHE.cache(dateFormatPattern, tl);
}
return tl.get();
}

public static Pair<Class, Object> doConvertDate(Temporal v, String format) {
DateTimeFormatter formatter = DateTimeWriteConverter.getDateTimeFormatter(format);
return Pair.of(String.class, formatter.format(v));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.liaochong.myexcel.core;

import com.github.liaochong.myexcel.core.templatehandler.ThymeleafTemplateHandler;
import com.github.liaochong.myexcel.utils.FileExportUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.Collections;

/**
* @author liaochong
* @version 1.0
*/
class TemplateStreamExcelBuilderTest extends BasicTest {

@Test
public void test() throws Exception {
TemplateStreamExcelBuilder templateStreamExcelBuilder = TemplateStreamExcelBuilder.of(ThymeleafTemplateHandler.class);
templateStreamExcelBuilder.append("/templates/thymeleafToExcelExample.html", Collections.emptyMap());
Workbook workbook = templateStreamExcelBuilder.build();
FileExportUtil.export(workbook, new File(TEST_OUTPUT_DIR + "template_stream_thymeleaf_build.xlsx"));
}

}

0 comments on commit b993d4a

Please sign in to comment.