map根据指定好的列进行排序
public void exportExcel( HttpServletResponse response) {
//查询出来的结果集map
List<Map<String, Object>> mapList = deviceWarningMapper.findExcle();
List<Map<String, Object>> resultList = new ArrayList<>();
for (Map<String, Object> row : mapList) {
Map<String, Object> sortedRow = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String key1, String key2) {
// 按照指定的列名顺序进行排序
List<String> columnNameOrder = Arrays.asList("城市名", "设备名称", "监测指标", "最小值", "最大值", "指标单位");
int index1 = columnNameOrder.indexOf(key1);
int index2 = columnNameOrder.indexOf(key2);
// 如果列表中没有找到对应的列名,则按照原有顺序排序
if (index1 == -1 || index2 == -1) {
return 0;
}
return Integer.compare(index1, index2);
}
});
// 将查询结果按照指定规则排序后放入新的Map中
sortedRow.putAll(row);
resultList.add(sortedRow);
}
ExcelUtil.exportToExcel(resultList,"阈值管理","sheet1",response);
}