Skip to content

Commit

Permalink
fix alais
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Jan 2, 2019
1 parent a4eefa9 commit 19165d7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* 【extra】 增加分词封装,封装了ansj、HanLP、IKAnalyzer、Jcseg、Jieba、MMSeg的实现,统一了接口
* 【core】 去除NumberUtil.parseInt和parseLong的8进制支持(issue#234@Github)
* 【extra】 Template部分修改命名减少歧义(Engine->TemplateEngine,EngineFactory->TemplateFactory)
* 【poi】 ExcelWriter中Map支持alias(issue#IQISU@Gitee)

### Bug修复

Expand Down
17 changes: 15 additions & 2 deletions hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.hutool.poi.excel;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -32,7 +33,13 @@ public class ExcelUtil {
* @since 3.2.0
*/
public static void readBySax(String path, int sheetIndex, RowHandler rowHandler) {
readBySax(FileUtil.getInputStream(path), sheetIndex, rowHandler);
BufferedInputStream in = null;
try {
in = FileUtil.getInputStream(path);
readBySax(in, sheetIndex, rowHandler);
} finally {
IoUtil.close(in);
}
}

/**
Expand All @@ -44,7 +51,13 @@ public static void readBySax(String path, int sheetIndex, RowHandler rowHandler)
* @since 3.2.0
*/
public static void readBySax(File file, int sheetIndex, RowHandler rowHandler) {
readBySax(FileUtil.getInputStream(file), sheetIndex, rowHandler);
BufferedInputStream in = null;
try {
in = FileUtil.getInputStream(file);
readBySax(in, sheetIndex, rowHandler);
} finally {
IoUtil.close(in);
}
}

/**
Expand Down
9 changes: 7 additions & 2 deletions hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -631,13 +631,18 @@ public ExcelWriter writeHeadRow(Iterable<?> rowData) {
* @see #writeRow(Map, boolean)
* @since 4.1.5
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public ExcelWriter writeRow(Object rowBean, boolean isWriteKeyAsHead) {
if (rowBean instanceof Iterable) {
return writeRow((Iterable<?>) rowBean);
}
Map<?, ?> rowMap = null;
Map rowMap = null;
if (rowBean instanceof Map) {
rowMap = (Map<?, ?>) rowBean;
if(MapUtil.isNotEmpty(this.headerAlias)) {
rowMap = MapUtil.newTreeMap((Map) rowBean, getInitedAliasComparator());
} else {
rowMap = (Map) rowBean;
}
} else if(BeanUtil.isBean(rowBean.getClass())){
if (MapUtil.isEmpty(this.headerAlias)) {
rowMap = BeanUtil.beanToMap(rowBean, new LinkedHashMap<String, Object>(), false, false);
Expand Down
37 changes: 36 additions & 1 deletion hutool-poi/src/test/java/cn/hutool/poi/test/ExcelWriteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,42 @@ public void writeMapTest2() {
// 关闭writer,释放内存
writer.close();
}


@Test
@Ignore
public void writeMapAliasTest() {
Map<Object, Object> row1 = new LinkedHashMap<>();
row1.put("name", "张三");
row1.put("age", 22);
row1.put("isPass", true);
row1.put("score", 66.30);
row1.put("examDate", DateUtil.date());
Map<Object, Object> row2 = new LinkedHashMap<>();
row2.put("name", "李四");
row2.put("age", 233);
row2.put("isPass", false);
row2.put("score", 32.30);
row2.put("examDate", DateUtil.date());

List<Map<Object, Object>> rows = CollUtil.newArrayList(row1, row2);
// 通过工具类创建writer
String file = "e:/writeMapAlias.xlsx";
FileUtil.del(file);
ExcelWriter writer = ExcelUtil.getWriter(file);
// 自定义标题
writer.addHeaderAlias("name", "姓名");
writer.addHeaderAlias("age", "年龄");
writer.addHeaderAlias("score", "分数");
writer.addHeaderAlias("isPass", "是否通过");
writer.addHeaderAlias("examDate", "考试时间");
// 合并单元格后的标题行,使用默认标题样式
writer.merge(4, "一班成绩单");
// 一次性写出内容,使用默认样式
writer.write(rows, true);
// 关闭writer,释放内存
writer.close();
}

@Test
@Ignore
public void writeBeanTest() {
Expand Down

0 comments on commit 19165d7

Please sign in to comment.