json陣列匯出到Excel
匯出方法
點選(此處)摺疊或開啟
-
public static JSONObject createExcel(String src, JSONArray json) {
-
//用於返回響應的訊息
-
JSONObject result = new JSONObject();
-
-
try {
-
File file = new File(src);
-
file.createNewFile();
-
-
OutputStream outputStream = new FileOutputStream(file);
-
WritableWorkbook writableWorkbook = Workbook.createWorkbook(outputStream);
-
WritableSheet sheet = writableWorkbook.createSheet("First sheet", 0);
-
-
//設定邊框
-
WritableFont font = new WritableFont(WritableFont.TIMES, 10, WritableFont.NO_BOLD, false);
-
WritableCellFormat format = new WritableCellFormat(font);
-
format.setBorder(jxl.format.Border.ALL, BorderLineStyle.THIN);
-
format.setAlignment(Alignment.CENTRE);
-
-
JSONArray jsonArray = json;
-
Label label;//單元格物件
-
int column = 0;//列數計數
-
JSONObject first = jsonArray.getJSONObject(0);
-
Iterator<String> iterator = first.keys();
-
while (iterator.hasNext()) {
-
String key = iterator.next();
-
label = new Label(column++, 0, key, format);
-
sheet.addCell(label);
-
}
-
-
for (int i = 0; i < jsonArray.size(); i++) {
-
JSONObject item = jsonArray.getJSONObject(i);
-
iterator = item.keys();
-
column = 0;
-
while (iterator.hasNext()) {
-
String key = iterator.next();
-
String value = item.getString(key);
-
label = new Label(column++, (i + 1), value, format);
-
sheet.addCell(label);
-
}
-
}
-
writableWorkbook.write();
-
writableWorkbook.close();
-
-
} catch (Exception e) {
-
result.put("result", "failed");
-
result.put("reason", e.getMessage());
-
return result;
-
}
-
result.put("result", "successed");
-
return result;
- }
測試方法
點選(此處)摺疊或開啟
-
public static void main(String[] args) {
-
String src = "C:\\a.xls";
-
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}]";
-
JSONArray jsonArray = JSONArray.fromObject(str);
-
JSONObject jsonObject1 = createExcel(src, jsonArray);
-
if (!JSONUtils.isNull(jsonObject1.get("result"))) {
-
String result = jsonObject1.get("result").toString();
-
if ("failed".equals(result)) {
-
System.out.println(jsonObject1.get("reason"));
-
}
-
}
- }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30046312/viewspace-2150636/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- hive匯出到csv hive匯出到excelHiveExcel
- java 匯出到EXCELJavaExcel
- 將資料匯出到ExcelExcel
- fastadmin的匯出到excel功能ASTExcel
- Dynamics CRM 資料匯出到Excel時列標題不能重複Excel
- Vue+element ui table 匯出到excelVueUIExcel
- 將dataGridView內容匯出到Excel檔案ViewExcel
- Json 陣列JSON陣列
- Excel匯出實列Excel
- 學習JSON陣列JSON陣列
- JS陣列去重 – JSON陣列去重陣列JSON
- 多個報表匯出到一個 excel 的多 sheet 頁Excel
- JavaScript 之 物件/JSON/陣列JavaScript物件JSON陣列
- 第四節:海量資料匯出到Excel、百萬Excel匯入到DB、秒級/分鐘級排行版Excel
- 二進位制陣列實戰 – 純前端匯出Excel檔案陣列前端Excel
- 二進位制陣列實戰 - 純前端匯出Excel檔案陣列前端Excel
- json字串 轉換為陣列JSON字串陣列
- vue element ui excel json2csv csv 匯出VueUIExcelJSON
- js 陣列,字串,json互相轉換陣列字串JSON
- springboot去讀json檔案解析json陣列處理Spring BootJSON陣列
- VUE中使用vue-json-excel超級方便匯出excel表格資料VueJSONExcel
- MSSQL資料匯出到MYSQLMySql
- 前端封裝成json資料和json陣列的方法前端封裝JSON陣列
- 二維陣列JSON.stringify 後,第二層陣列解析為空陣列JSON
- Hive解析Json陣列超全講解HiveJSON陣列
- c#設定匯出Excel的列寬C#Excel
- fastjson: json物件,json物件陣列,javabean物件,json字串之間的相互轉化ASTJSON物件陣列JavaBean字串
- JSON轉ExcelJSONExcel
- JavaScript語法中將json轉成陣列JavaScriptJSON陣列
- csv/json/list/datatable匯出為excel的通用模組設計JSONExcel
- 大文字資料,匯入匯出到資料庫資料庫
- 資料庫文件編寫,如何通過Navicat把表導成表格?資料庫快速匯出為excel表格資訊,excel匯出到word表格資料庫Excel
- 易語言帶陣列json的編寫方法陣列JSON
- excel轉json操作ExcelJSON
- 匯出excelExcel
- PhpSpreadsheet匯出Excel超過26列解決辦法PHPExcel
- vue excel匯入匯出VueExcel
- json 陣列已知父節點,求所有子節點JSON陣列
- 字串json陣列怎麼轉換成jsonobject型別字串JSON陣列Object型別