Java instanceof 各种数据类型判断

家电修理 2023-07-16 19:17www.caominkang.com电器维修

 Java  instanceof 各种数据类型判断

在导出的时候,要匹配数据,而数据有各种类型,只能用instanceof一一进行判断了。

目标

表头 {"aa","bb","","dd","ee","ff","gg"}

 

 数据

[{"aa":100,"bb":"1,2,3","":{"num":"99","name":"统计数值"},"dd":["10505","10508"],"ee":[["业务运营保障","批量数据"],["系统运行维护","问题故障通知"],["系统运行维护","运维优化"]],"ff":[{"name":"工单开始","state":"F"},{"name":"工单处理","state":"A"}]}]

导出

 

如何进行匹配数据呢? 再进行导出到ord表格内呢?

处理

1,模拟数据:
public static List> initData(){
    List> dataList = ne ArrayList<>();
    Map dataMap = ne HashMap<>();
    dataMap.put("aa", 100);
    dataMap.put("bb", "1,2,3");
    // map
    dataMap.put("", ne HashMap(){{
        put("name","统计数值");
        put("num","99");
    }});
    // 一维字符数组
    dataMap.put("dd", Lists.neArrayList("10505", "10508"));
    // 二维字符数组
    dataMap.put("ee", Lists.neArrayList( Lists.neArrayList("业务运营保障", "批量数据"),
            Lists.neArrayList("系统运行维护", "问题故障通知"),
            Lists.neArrayList("系统运行维护", "运维优化")));
    // list map
    dataMap.put("ff", Lists.neArrayList(ne HashMap(){{
        put("name","工单开始");
        put("state","F");
    }}, ne HashMap(){{
        put("name","工单处理");
        put("state","A");
    }}));

    dataList.add(dataMap);
    return dataList;
}

 

2,匹配数据
private static List> matchHeaderData(List> data, List 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> dyadicList = (List>) 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> mapList = (List>) object;
                        return ListUtils.emptyIfNull(mapList).stream().map(t -> MapUtils.getString(t, "name"))
                                .collect(Collectors.joining(","));
                    } else if (inner instanceof String) { // 一维字符串数组
                        List strList = (List) object;
                        return ListUtils.emptyIfNull(strList).stream().collect(Collectors.joining(","));
                    }
                }
                return "";
            } else if (object instanceof Map) { // map 类型
                Map map = (Map) 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 ne ArrayList<>();
}

 

 

匹配数据的时候,要对各种类型进行判断,map里面属性要相对固定,不然不好处理。最好数据有类型,先根据类型去处理,就不用一个一个 instanceof 判断了。

3,导出数据
public static void main(String[] args) thros Exception {
 OutputStream out = ne FileOutputStream("d://exportFile//table" + System.currentTimeMillis() + ".doc");
 document document = ne document(PageSize.A4);
 RtfWriter2.getInstance(document, out);
 document.open();

 Color lightGray = ne Color(232, 232, 232);
 .loagie.text.Font fontChinese = ne .loagie.text.Font(null, 12, .loagie.text.Font.BOLD,
   Color.black);
 .loagie.text.Font titleChinese = ne .loagie.text.Font(null, 17, .loagie.text.Font.BOLD,
   Color.black);


 String[] headAttr = {"aa","bb","","dd","ee","ff","gg"};

 int size = headAttr.length;

 Table table = ne Table(size);
 int idths = 100 / size;
 int idths1[] = setWordWith(size, idths);// 设置每列宽度比例
 table.setWidths(idths1);
 table.setWidth(100);// 占页面宽度比例
 table.setAlignment(Element.ALIGN_CENTER);//居中
 table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
 table.setAutoFillEmptyCells(true);//自动填满
 table.setBorderWidth(1);//边框宽度
 table.setPadding(8);

 // 表格表题
 Paragraph p = ne Paragraph("导出内容", titleChinese);
 p.setSpacingAfter(1);
 Cell cell = ne Cell(p);
 cell.setHorizontalAlignment(Element.ALIGN_CENTER);
 cell.setBorder(0);
 cell.setColspan(size);
 table.addCell(cell);

 // 表头
 for (String column : headAttr) {
  cell = ne Cell(ne Paragraph(column, fontChinese));
  cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  cell.setBackgroundColor(lightGray);
  table.addCell(cell);
 }

 List> content = matchHeaderData(initData(), Lists.neArrayList(headAttr));
 // 内容
 for (List ro : content) {
  for (String column : ro) {
   cell = ne Cell(ne 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 ith) {
 int[] intWidth = ne int[size];
 for (int i = 0; i < size; i++) {
  intWidth[i] = ith;
 }
 return intWidth;
}

  这边导出用的是Loagie.text ,pdf导出参考《Loagie 导出html的内容到 pdf》

  进行数据类型匹配,因为可能存在各种嵌套,会比较复杂。最好数据本身有一个类型type,根据类型去处理数据的值,这样更为合理。

Copyright © 2016-2025 www.caominkang.com 曹敏电脑维修网 版权所有 Power by