This commit is contained in:
fumeiai 2020-05-21 18:30:21 +08:00
parent e50cc47591
commit ac7902edde
6 changed files with 66 additions and 45 deletions

21
.idea/workspace.xml generated
View File

@ -4,7 +4,9 @@
<list default="true" id="e4baaf01-a2c2-445d-98a1-9f4c50c148cf" name="Default Changelist" comment=""> <list default="true" id="e4baaf01-a2c2-445d-98a1-9f4c50c148cf" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/lz/common/utils/ExcelUtil.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/lz/common/utils/ExcelUtil.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/com/lz/common/utils/ExcelUtil.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/lz/common/utils/ExcelUtil.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/lz/modules/app/controller/StaffController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/lz/modules/app/controller/StaffController.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/target/classes/com/lz/common/utils/ExcelUtil$1.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/lz/common/utils/ExcelUtil$1.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/com/lz/common/utils/ExcelUtil.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/lz/common/utils/ExcelUtil.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/com/lz/modules/app/controller/StaffController.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/lz/modules/app/controller/StaffController.class" afterDir="false" />
</list> </list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -156,7 +158,7 @@
<workItem from="1588161274115" duration="1691000" /> <workItem from="1588161274115" duration="1691000" />
<workItem from="1588163384182" duration="74761000" /> <workItem from="1588163384182" duration="74761000" />
<workItem from="1588936950753" duration="95758000" /> <workItem from="1588936950753" duration="95758000" />
<workItem from="1590038091791" duration="10294000" /> <workItem from="1590038091791" duration="10974000" />
</task> </task>
<servers /> <servers />
</component> </component>
@ -224,6 +226,21 @@
<line>212</line> <line>212</line>
<option name="timeStamp" value="74" /> <option name="timeStamp" value="74" />
</line-breakpoint> </line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/com/lz/modules/app/controller/StaffController.java</url>
<line>249</line>
<option name="timeStamp" value="78" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/com/lz/common/utils/ExcelUtil.java</url>
<line>184</line>
<option name="timeStamp" value="80" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/com/lz/common/utils/ExcelUtil.java</url>
<line>205</line>
<option name="timeStamp" value="81" />
</line-breakpoint>
</breakpoints> </breakpoints>
</breakpoint-manager> </breakpoint-manager>
</component> </component>

View File

@ -564,60 +564,64 @@ public class ExcelUtil {
} }
} }
// 遍历集合数据产生数据行 if (dataSet!=null) {
Iterator<T> it = dataSet.iterator(); // 遍历集合数据产生数据行
int index = 0; Iterator<T> it = dataSet.iterator();
while (it.hasNext()) { int index = 0;
index++; while (it.hasNext()) {
row = sheet.createRow(index); index++;
//获取数据源一条数据 row = sheet.createRow(index);
T data = it.next(); //获取数据源一条数据
try { T data = it.next();
// try {
if (data instanceof Map) { //
@SuppressWarnings("unchecked") if (data instanceof Map) {
Map<String, Object> map = (Map<String, Object>) data; @SuppressWarnings("unchecked")
int cellNum = 0; Map<String, Object> map = (Map<String, Object>) data;
//遍历列名 int cellNum = 0;
Iterator<String> it2 = keys.iterator(); //遍历列名
while (it2.hasNext()) { Iterator<String> it2 = keys.iterator();
key = it2.next(); while (it2.hasNext()) {
key = it2.next();
//todo:这样并不会将当前项空出而是所有值向前移动一位 //todo:这样并不会将当前项空出而是所有值向前移动一位
if (!headers.containsKey(key)) { if (!headers.containsKey(key)) {
logger.error("Map 中 不存在 key [" + key + "]"); logger.error("Map 中 不存在 key [" + key + "]");
continue; continue;
}
Object value = map.get(key);
HSSFCell cell = row.createCell(cellNum);
cellNum = setCellValue(cell, value, pattern, cellNum, null, row);
cellNum++;
} }
Object value = map.get(key); } else {
HSSFCell cell = row.createCell(cellNum);
cellNum = setCellValue(cell, value, pattern, cellNum, null, row); //获取对应字段
cellNum++;
}
} else {
//获取对应字段
// List<FieldForSortting> fields = sortFieldByAnno(data.getClass()); // List<FieldForSortting> fields = sortFieldByAnno(data.getClass());
List<FieldForSortting> fields = sortFieldNoAnnoCheck(data.getClass()); List<FieldForSortting> fields = sortFieldNoAnnoCheck(data.getClass());
int cellNum = 0; int cellNum = 0;
for (FieldForSortting field1 : fields) { for (FieldForSortting field1 : fields) {
HSSFCell cell = row.createCell(cellNum); HSSFCell cell = row.createCell(cellNum);
//获取当前字段属性 //获取当前字段属性
Field field = field1.getField(); Field field = field1.getField();
field.setAccessible(true); field.setAccessible(true);
Object value = field.get(data); Object value = field.get(data);
cellNum = setCellValue(cell, value, pattern, cellNum, field, row); cellNum = setCellValue(cell, value, pattern, cellNum, field, row);
cellNum++; cellNum++;
}
} }
} catch (Exception e) {
logger.error(e.toString(), e);
} }
} catch (Exception e) {
logger.error(e.toString(), e);
} }
} }
// 设定自动宽度 // 设定自动宽度
for (int i = 0; i < headers.size(); i++) { for (int i = 0; i < headers.size(); i++) {
sheet.autoSizeColumn(i); sheet.autoSizeColumn(i);