json陣列匯出到Excel

634828354發表於2018-01-30

匯出方法

點選(此處)摺疊或開啟

  1. public static JSONObject createExcel(String src, JSONArray json) {
  2.         //用於返回響應的訊息
  3.         JSONObject result = new JSONObject();

  4.         try {
  5.             File file = new File(src);
  6.             file.createNewFile();

  7.             OutputStream outputStream = new FileOutputStream(file);
  8.             WritableWorkbook writableWorkbook = Workbook.createWorkbook(outputStream);
  9.             WritableSheet sheet = writableWorkbook.createSheet("First sheet", 0);

  10.             //設定邊框
  11.             WritableFont font = new WritableFont(WritableFont.TIMES, 10, WritableFont.NO_BOLD, false);
  12.             WritableCellFormat format = new WritableCellFormat(font);
  13.             format.setBorder(jxl.format.Border.ALL, BorderLineStyle.THIN);
  14.             format.setAlignment(Alignment.CENTRE);

  15.             JSONArray jsonArray = json;
  16.             Label label;//單元格物件
  17.             int column = 0;//列數計數
  18.             JSONObject first = jsonArray.getJSONObject(0);
  19.             Iterator<String> iterator = first.keys();
  20.             while (iterator.hasNext()) {
  21.                 String key = iterator.next();
  22.                 label = new Label(column++, 0, key, format);
  23.                 sheet.addCell(label);
  24.             }

  25.             for (int i = 0; i < jsonArray.size(); i++) {
  26.                 JSONObject item = jsonArray.getJSONObject(i);
  27.                 iterator = item.keys();
  28.                 column = 0;
  29.                 while (iterator.hasNext()) {
  30.                     String key = iterator.next();
  31.                     String value = item.getString(key);
  32.                     label = new Label(column++, (i + 1), value, format);
  33.                     sheet.addCell(label);
  34.                 }
  35.             }
  36.             writableWorkbook.write();
  37.             writableWorkbook.close();

  38.         } catch (Exception e) {
  39.             result.put("result", "failed");
  40.             result.put("reason", e.getMessage());
  41.             return result;
  42.         }
  43.         result.put("result", "successed");
  44.         return result;
  45.     }

測試方法


點選(此處)摺疊或開啟

  1. public static void main(String[] args) {
  2.         String src = "C:\\a.xls";
  3.         String str = "[{\"dupCount\":4,\"qs_sumValue\":30,\"qs_span\":16,\"qs_dupCount\":0,\"sumValue\":64,\"dxb\":\"2:6\",\"job_qs\":\"2:1\",\"winNumber\":\"19 03 08 06 12 01 05 10\",\"zhb\":\"4:4\",\"matchNo\":\"17122387\",\"zhb_qs\":\"2:1\",\"job\":\"4:4\",\"dxb_qs\":\"1:2\",\"span\":18},{\"dupCount\":4,\"qs_sumValue\":36,\"qs_span\":14,\"qs_dupCount\":1,\"sumValue\":70,\"dxb\":\"3:5\",\"job_qs\":\"0:3\",\"winNumber\":\"20 10 06 11 15 01 02 05\",\"zhb\":\"4:4\",\"matchNo\":\"17122386\",\"zhb_qs\":\"0:3\",\"job\":\"4:4\",\"dxb_qs\":\"1:2\",\"span\":19},{\"dupCount\":5,\"qs_sumValue\":32,\"qs_span\":13,\"qs_dupCount\":0,\"sumValue\":57,\"dxb\":\"1:7\",\"job_qs\":\"2:1\",\"winNumber\":\"19 07 06 10 01 02 03 09\",\"zhb\":\"5:3\",\"matchNo\":\"17122385\",\"zhb_qs\":\"2:1\",\"job\":\"5:3\",\"dxb_qs\":\"1:2\",\"span\":18},{\"dupCount\":6,\"qs_sumValue\":15,\"qs_span\":10,\"qs_dupCount\":1,\"sumValue\":46,\"dxb\":\"1:7\",\"job_qs\":\"3:0\",\"winNumber\":\"11 01 03 08 10 02 05 06\",\"zhb\":\"5:3\",\"matchNo\":\"17122384\",\"zhb_qs\":\"3:0\",\"job\":\"4:4\",\"dxb_qs\":\"1:2\",\"span\":10},{\"dupCount\":0,\"qs_sumValue\":27,\"qs_span\":5,\"qs_dupCount\":0,\"sumValue\":46,\"dxb\":\"1:7\",\"job_qs\":\"1:2\",\"winNumber\":\"10 11 06 04 05 01 02 07\",\"zhb\":\"5:3\",\"matchNo\":\"17122383\",\"zhb_qs\":\"1:2\",\"job\":\"4:4\",\"dxb_qs\":\"1:2\",\"span\":10}]";
  4.         JSONArray jsonArray = JSONArray.fromObject(str);
  5.         JSONObject jsonObject1 = createExcel(src, jsonArray);
  6.         if (!JSONUtils.isNull(jsonObject1.get("result"))) {
  7.             String result = jsonObject1.get("result").toString();
  8.             if ("failed".equals(result)) {
  9.                 System.out.println(jsonObject1.get("reason"));
  10.             }
  11.         }
  12.     }


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30046312/viewspace-2150636/,如需轉載,請註明出處,否則將追究法律責任。

相關文章