mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
优化ExcelWriter中使用比较器writer的方法,只对第一条数据进行排序(pr#3807@Github)
This commit is contained in:
parent
873a8d221e
commit
9997d72ef6
@ -99,7 +99,35 @@ public class JdkClientEngine extends AbstractClientEngine {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
|
||||
return sendRedirectIfPossible(conn, context);
|
||||
// 自定义重定向
|
||||
final int maxRedirects = message.maxRedirects();
|
||||
if (maxRedirects > 0 && context.getRedirectCount() < maxRedirects) {
|
||||
final int code;
|
||||
try {
|
||||
code = conn.getCode();
|
||||
} catch (final IOException e) {
|
||||
// 错误时静默关闭连接
|
||||
conn.closeQuietly();
|
||||
throw new HttpException(e);
|
||||
}
|
||||
|
||||
if (HttpStatus.isRedirected(code)) {
|
||||
// https://www.rfc-editor.org/rfc/rfc7231#section-6.4.7
|
||||
// https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Redirections
|
||||
// 307方法和消息主体都不发生变化。
|
||||
if (HttpStatus.HTTP_TEMP_REDIRECT != code) {
|
||||
// 重定向默认使用GET
|
||||
message.method(Method.GET);
|
||||
}
|
||||
message.locationTo(conn.header(HeaderName.LOCATION));
|
||||
// 自增计数器
|
||||
context.incrementRedirectCount();
|
||||
return doSend(context);
|
||||
}
|
||||
}
|
||||
|
||||
// 最终页面
|
||||
return new JdkHttpResponse(conn, this.cookieManager, context.getRequest());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -167,44 +195,4 @@ public class JdkClientEngine extends AbstractClientEngine {
|
||||
|
||||
return conn;
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用转发,如果需要转发返回转发结果,否则返回{@code null}
|
||||
*
|
||||
* @param conn {@link JdkHttpConnection}}
|
||||
* @param context 请求上下文
|
||||
* @return {@link JdkHttpResponse},无转发返回 {@code null}
|
||||
*/
|
||||
private JdkHttpResponse sendRedirectIfPossible(final JdkHttpConnection conn, final RequestContext context) {
|
||||
final Request message = context.getRequest();
|
||||
final int maxRedirects = message.maxRedirects();
|
||||
// 手动实现重定向
|
||||
if (maxRedirects > 0 && context.getRedirectCount() < maxRedirects) {
|
||||
final int code;
|
||||
try {
|
||||
code = conn.getCode();
|
||||
} catch (final IOException e) {
|
||||
// 错误时静默关闭连接
|
||||
conn.closeQuietly();
|
||||
throw new HttpException(e);
|
||||
}
|
||||
|
||||
if (HttpStatus.isRedirected(code)) {
|
||||
// https://www.rfc-editor.org/rfc/rfc7231#section-6.4.7
|
||||
// https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Redirections
|
||||
// 307方法和消息主体都不发生变化。
|
||||
if (HttpStatus.HTTP_TEMP_REDIRECT != code) {
|
||||
// 重定向默认使用GET
|
||||
message.method(Method.GET);
|
||||
}
|
||||
message.locationTo(conn.header(HeaderName.LOCATION));
|
||||
// 自增计数器
|
||||
context.incrementRedirectCount();
|
||||
return doSend(context);
|
||||
}
|
||||
}
|
||||
|
||||
// 最终页面
|
||||
return new JdkHttpResponse(conn, this.cookieManager, context.getRequest());
|
||||
}
|
||||
}
|
||||
|
@ -38,6 +38,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
@ -731,11 +732,20 @@ public class ExcelWriter extends ExcelBase<ExcelWriter, ExcelWriteConfig> {
|
||||
boolean isFirstRow = true;
|
||||
Map<?, ?> map;
|
||||
for (final Object obj : data) {
|
||||
if (obj instanceof Map) {
|
||||
map = new TreeMap<>(comparator);
|
||||
map.putAll((Map) obj);
|
||||
} else {
|
||||
map = BeanUtil.beanToMap(obj, new TreeMap<>(comparator), false, false);
|
||||
if(isFirstRow){
|
||||
// 只排序首行(标题),后续数据按照首行key的位置填充,无需重新排序
|
||||
if (obj instanceof Map) {
|
||||
map = new TreeMap<>(comparator);
|
||||
map.putAll((Map) obj);
|
||||
} else {
|
||||
map = BeanUtil.beanToMap(obj, new TreeMap<>(comparator), false, false);
|
||||
}
|
||||
}else{
|
||||
if (obj instanceof Map) {
|
||||
map = (Map) obj;
|
||||
} else {
|
||||
map = BeanUtil.beanToMap(obj, new HashMap<>(), false, false);
|
||||
}
|
||||
}
|
||||
writeRow(map, isFirstRow);
|
||||
if (isFirstRow) {
|
||||
|
@ -0,0 +1,84 @@
|
||||
package org.dromara.hutool.poi.excel.writer;
|
||||
|
||||
import org.dromara.hutool.core.comparator.IndexedComparator;
|
||||
import org.dromara.hutool.poi.excel.ExcelUtil;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Pr3807Test {
|
||||
@Test
|
||||
@Disabled
|
||||
public void writeWithComparatorTest() {
|
||||
// 生成测试数据, 10w行50列
|
||||
final List<Map<String, Object>> dataList = new ArrayList<>();
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
final Map<String, Object> map = new HashMap<>();
|
||||
map.put("test11", "test11_" + i);
|
||||
map.put("test12", "test12_" + i);
|
||||
map.put("test13", "test13_" + i);
|
||||
map.put("test14", "test14_" + i);
|
||||
map.put("test15", "test15_" + i);
|
||||
map.put("test16", "test16_" + i);
|
||||
map.put("test17", "test17_" + i);
|
||||
map.put("test18", "test18_" + i);
|
||||
map.put("test19", "test19_" + i);
|
||||
map.put("test1", "test1_" + i);
|
||||
map.put("test2", "test2_" + i);
|
||||
map.put("test3", "test3_" + i);
|
||||
map.put("test4", "test4_" + i);
|
||||
map.put("test5", "test5_" + i);
|
||||
map.put("test6", "test6_" + i);
|
||||
map.put("test7", "test7_" + i);
|
||||
map.put("test8", "test8_" + i);
|
||||
map.put("test9", "test9_" + i);
|
||||
map.put("test10", "test10_" + i);
|
||||
map.put("test20", "test20_" + i);
|
||||
map.put("test21", "test21_" + i);
|
||||
map.put("test22", "test22_" + i);
|
||||
map.put("test23", "test23_" + i);
|
||||
map.put("test24", "test24_" + i);
|
||||
map.put("test25", "test25_" + i);
|
||||
map.put("test26", "test26_" + i);
|
||||
map.put("test27", "test27_" + i);
|
||||
map.put("test28", "test28_" + i);
|
||||
map.put("test29", "test29_" + i);
|
||||
map.put("test30", "test30_" + i);
|
||||
map.put("test41", "test41_" + i);
|
||||
map.put("test42", "test42_" + i);
|
||||
map.put("test43", "test43_" + i);
|
||||
map.put("test44", "test44_" + i);
|
||||
map.put("test45", "test45_" + i);
|
||||
map.put("test46", "test46_" + i);
|
||||
map.put("test47", "test47_" + i);
|
||||
map.put("test48", "test48_" + i);
|
||||
map.put("test49", "test49_" + i);
|
||||
map.put("test50", "test50_" + i);
|
||||
map.put("test31", "test31_" + i);
|
||||
map.put("test32", "test32_" + i);
|
||||
map.put("test33", "test33_" + i);
|
||||
map.put("test34", "test34_" + i);
|
||||
map.put("test35", "test35_" + i);
|
||||
map.put("test36", "test36_" + i);
|
||||
map.put("test37", "test37_" + i);
|
||||
map.put("test38", "test38_" + i);
|
||||
map.put("test39", "test39_" + i);
|
||||
map.put("test40", "test40_" + i);
|
||||
dataList.add(map);
|
||||
}
|
||||
// 使用比较器写出
|
||||
try (final ExcelWriter excelWriter = ExcelUtil.getWriter("d:/test/poi/writeWithComparatorTest.xlsx")) {
|
||||
excelWriter.write(dataList, new IndexedComparator<>(
|
||||
"test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9", "test10",
|
||||
"test11", "test12", "test13", "test14", "test15", "test16", "test17", "test18", "test19", "test20",
|
||||
"test21", "test22", "test23", "test24", "test25", "test26", "test27", "test28", "test29", "test30",
|
||||
"test31", "test32", "test33", "test34", "test35", "test36", "test37", "test38", "test39", "test40",
|
||||
"test41", "test42", "test43", "test44", "test45", "test46", "test47", "test48", "test49", "test50"
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user