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="">
<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/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>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
@ -156,7 +158,7 @@
<workItem from="1588161274115" duration="1691000" />
<workItem from="1588163384182" duration="74761000" />
<workItem from="1588936950753" duration="95758000" />
<workItem from="1590038091791" duration="10294000" />
<workItem from="1590038091791" duration="10974000" />
</task>
<servers />
</component>
@ -224,6 +226,21 @@
<line>212</line>
<option name="timeStamp" value="74" />
</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>
</breakpoint-manager>
</component>

View File

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