Java instanceof 各种数据类型判断
在导出的时候,要匹配数据,而数据有各种类型,只能用instanceof一一进行判断了。
目标:
表头: {"aa","bb","cc","dd","ee","ff","gg"}
数据
[{"aa":100,"bb":"1,2,3","cc":{"num":"99","name":"统计数值"},"dd":["10505","10508"],"ee":[["业务运营保障","批量数据"],["系统运行维护","问题故障通知"],["系统运行维护","运维优化"]],"ff":[{"name":"工单开始","state":"F"},{"name":"工单处理","state":"A"}]}]
导出:
如何进行匹配数据呢? 再进行导出到word表格内呢?
处理:
1,模拟数据:
public static List<Map<String, Object>> initData(){
List<Map<String, Object>> dataList = new ArrayList<>();
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("aa", 100);
dataMap.put("bb", "1,2,3");
// map
dataMap.put("cc", new HashMap<String, String>(){{
put("name","统计数值");
put("num","99");
}});
// 一维字符数组
dataMap.put("dd", Lists.newArrayList("10505", "10508"));
// 二维字符数组
dataMap.put("ee", Lists.newArrayList( Lists.newArrayList("业务运营保障", "批量数据"),
Lists.newArrayList("系统运行维护", "问题故障通知"),
Lists.newArrayList("系统运行维护", "运维优化")));
// list map
dataMap.put("ff", Lists.newArrayList(new HashMap<String, String>(){{
put("name","工单开始");
put("state","F");
}}, new HashMap<String, String>(){{
put("name","工单处理");
put("state","A");
}}));
dataList.add(dataMap);
return dataList;
}
2,匹配数据:
/**
* 匹配字段对应的值
*/
private static List<List<String>> matchHeaderData(List<Map<String, Object>> data, List<String> matchField) {
try {
return ListUtils.emptyIfNull(data).stream().map(e -> ListUtils.emptyIfNull(matchField).stream().map(f -> {
Object object = e.get(f);
if (object instanceof List) {
List dataList = (List) object;
if (CollectionUtils.isNotEmpty(dataList)) {
Object inner = dataList.get(0);
if (inner instanceof List) { // 二维字符数组 获取最后一个字符
List<List<String>> dyadicList = (List<List<String>>) object;
return ListUtils.emptyIfNull(dyadicList).stream().map(
dy -> ListUtils.emptyIfNull(dy).stream().reduce((first, last) -> last).orElse(""))
.collect(Collectors.joining(","));
} else if (inner instanceof Map) { // list map 格式
List<Map<String, Object>> mapList = (List<Map<String, Object>>) object;
return ListUtils.emptyIfNull(mapList).stream().map(t -> MapUtils.getString(t, "name"))
.collect(Collectors.joining(","));
} else if (inner instanceof String) { // 一维字符串数组
List<String> strList = (List<String>) object;
return ListUtils.emptyIfNull(strList).stream().collect(Collectors.joining(","));
}
}
return "";
} else if (object instanceof Map) { // map 类型
Map<String, Object> map = (Map<String, Object>) object;
return MapUtils.getString(map, "num");
}else { // 数字,字符串
String label = MapUtils.getString(e, f);
return label == null ? "" : label;
}
}).collect(Collectors.toList())).collect(Collectors.toList());
} catch (Exception e) {
e.printStackTrace();
}
return new ArrayList<>();
}
匹配数据的时候,要对各种类型进行判断,map里面属性要相对固定,不然不好处理。最好数据有类型,先根据类型去处理,就不用一个一个 instanceof 判断了。
3,导出数据
public static void main(String[] args) throws Exception {
OutputStream out = new FileOutputStream("d://exportFile//table" + System.currentTimeMillis() + ".doc");
Document document = new Document(PageSize.A4);
RtfWriter2.getInstance(document, out);
document.open();
Color lightGray = new Color(232, 232, 232);
com.lowagie.text.Font fontChinese = new com.lowagie.text.Font(null, 12, com.lowagie.text.Font.BOLD,
Color.black);
com.lowagie.text.Font titleChinese = new com.lowagie.text.Font(null, 17, com.lowagie.text.Font.BOLD,
Color.black);
String[] headAttr = {"aa","bb","cc","dd","ee","ff","gg"};
int size = headAttr.length;
Table table = new Table(size);
int widths = 100 / size;
int widths1[] = setWordWith(size, widths);// 设置每列宽度比例
table.setWidths(widths1);
table.setWidth(100);// 占页面宽度比例
table.setAlignment(Element.ALIGN_CENTER);//居中
table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.setAutoFillEmptyCells(true);//自动填满
table.setBorderWidth(1);//边框宽度
table.setPadding(8);
// 表格表题
Paragraph p = new Paragraph("导出内容", titleChinese);
p.setSpacingAfter(1);
Cell cell = new Cell(p);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBorder(0);
cell.setColspan(size);
table.addCell(cell);
// 表头
for (String column : headAttr) {
cell = new Cell(new Paragraph(column, fontChinese));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setBackgroundColor(lightGray);
table.addCell(cell);
}
List<List<String>> content = matchHeaderData(initData(), Lists.newArrayList(headAttr));
// 内容
for (List<String> row : content) {
for (String column : row) {
cell = new Cell(new Paragraph(column, fontChinese));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
}
}
document.add(table);
document.close();
System.out.println("ok");
}
private static int[] setWordWith(int size, int with) {
int[] intWidth = new int[size];
for (int i = 0; i < size; i++) {
intWidth[i] = with;
}
return intWidth;
}
这边导出用的是Lowagie.text ,pdf导出参考《Lowagie 导出html的内容到 pdf》
总结:
进行数据类型匹配,因为可能存在各种嵌套,会比较复杂。最好数据本身有一个类型type,根据类型去处理数据的值,这样更为合理。