GcExcel数据绑定之List<Map>&List<JSON>
客户是在实际的业务开发场景中经常存在数据结构不固定的场景,此时通常会用json或map这种灵活的键值对结构来承接数据,并期望可以将这种数据结构通过数据绑定的形式设置到表单中去。GcExcel 5.2.0的版本中,JsonDataSource对象支持了该场景,只需用该对象包裹一个json结构的字符串对象,即可完成数据绑定。具体代码如下:1、MAP之数据绑定public static void bindDataToTable(Workbook workbook, IWorksheet worksheet) {
List<Map<String, Object>> mapList = Lists.newArrayList();
Map<String, Object> map = Maps.newHashMap();
map.put("area", "North America");
map.put("city", "Chicago");
map.put("category", "Consumer Electronics");
map.put("name", "Bose 785593-0050");
map.put("revenue", 92800);
mapList.add(map);
JsonDataSource json = new JsonDataSource(JSONUtils.toJSONString(mapList));
worksheet.setDataSource(json);
workbook.save("dd.xlsx");
}2、JSON之数据绑定
public static void bindData(Workbook workbook, IWorksheet worksheet) {
List<JSONObject> jsonObjects = Lists.newArrayList();
JSONObject jsonObject = new JSONObject();
jsonObject.put("area", "North America");
jsonObject.put("city", "Chicago");
jsonObject.put("category", "Consumer Electronics");
jsonObject.put("name", "Bose 785593-0050");
jsonObject.put("revenue", 92800);
jsonObjects.add(jsonObject);
JsonDataSource json = new JsonDataSource(JSONUtils.toJSONString(jsonObjects));
worksheet.setDataSource(json);
workbook.save("hj.xlsx");
}
:i0tw:
页:
[1]