问题
I am trying to dump my JSON
response into excel sheet with .csv
file after getting response from server. I have tried with poi-3.7.jar
and poi-ooxml-3.11.jar
libraries. But I am getting an empty excel sheet.
Please check my below code for more reference.
My JSON URL http://vehiclerescue.in/ideadarpan_beta/api/reportApp/get_tsm_data
Code how I created rows:
try {
fis = new FileInputStream(new File("C:\\File\\excel.xlsx"));
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(0);
//Create rows
XSSFRow row0 = sheet.createRow(0);
XSSFCell r1c0 = row0.createCell(0);
r1c0.setCellValue("id\t");
XSSFRow row1 = sheet.createRow(1);
XSSFCell r1c1 = row1.createCell(0);
r1c1.setCellValue("zone");
XSSFRow row2 = sheet.createRow(2);
XSSFCell r1c2 = row2.createCell(0);
r1c2.setCellValue("scd_code");
XSSFRow row3 = sheet.createRow(3);
XSSFCell r1c3 = row3.createCell(0);
r1c3.setCellValue("asm_name");
XSSFRow row4 = sheet.createRow(4);
XSSFCell r1c4 = row4.createCell(0);
r1c4.setCellValue("tsm_tse_name");
XSSFRow row5 = sheet.createRow(5);
XSSFCell r1c5 = row5.createCell(0);
r1c5.setCellValue("tsm_tse_type");
XSSFRow row6 = sheet.createRow(6);
XSSFCell r1c6 = row6.createCell(0);
r1c6.setCellValue("distributorname");
XSSFRow row7 = sheet.createRow(7);
XSSFCell r1c7 = row7.createCell(0);
r1c7.setCellValue("dist_msisdn");
XSSFRow row8 = sheet.createRow(8);
XSSFCell r1c8 = row8.createCell(0);
r1c8.setCellValue("se_msisdn");
XSSFRow row9 = sheet.createRow(9);
XSSFCell r1c9 = row9.createCell(0);
r1c9.setCellValue("ret_msisdn");
XSSFRow row10 = sheet.createRow(10);
XSSFCell r1c10 = row10.createCell(0);
r1c10.setCellValue("july_1st");
XSSFRow row11 = sheet.createRow(11);
XSSFCell r1c11 = row11.createCell(0);
r1c11.setCellValue("july_2nd");
XSSFRow row12 = sheet.createRow(12);
XSSFCell r1c12 = row12.createCell(0);
r1c12.setCellValue("july_3rd");
XSSFRow row13 = sheet.createRow(13);
XSSFCell r1c13 = row13.createCell(0);
r1c13.setCellValue("july_4th");
XSSFRow row14 = sheet.createRow(14);
XSSFCell r1c14 = row14.createCell(0);
r1c14.setCellValue("july_5th");
XSSFRow row15 = sheet.createRow(15);
XSSFCell r1c15 = row15.createCell(0);
r1c15.setCellValue("july_6th");
XSSFRow row16 = sheet.createRow(16);
XSSFCell r1c16 = row16.createCell(0);
r1c16.setCellValue("july_7th");
XSSFRow row17 = sheet.createRow(17);
XSSFCell r1c17 = row17.createCell(0);
r1c17.setCellValue("july_8th");
XSSFRow row18 = sheet.createRow(18);
XSSFCell r1c18 = row18.createCell(0);
r1c18.setCellValue("july_9th");
XSSFRow row19 = sheet.createRow(19);
XSSFCell r1c19 = row19.createCell(0);
r1c19.setCellValue("july_10th");
XSSFRow row20 = sheet.createRow(20);
XSSFCell r1c20 = row20.createCell(0);
r1c20.setCellValue("july_11th");
XSSFRow row21 = sheet.createRow(21);
XSSFCell r1c21 = row21.createCell(0);
r1c21.setCellValue("july_12th");
XSSFRow row22 = sheet.createRow(22);
XSSFCell r1c22 = row22.createCell(0);
r1c22.setCellValue("july_13th");
XSSFRow row23 = sheet.createRow(23);
XSSFCell r1c23 = row23.createCell(0);
r1c23.setCellValue("july_14th");
XSSFRow row24 = sheet.createRow(24);
XSSFCell r1c24 = row24.createCell(0);
r1c24.setCellValue("july_15th");
XSSFRow row25 = sheet.createRow(25);
XSSFCell r1c25 = row25.createCell(0);
r1c25.setCellValue("july_16th");
XSSFRow row26 = sheet.createRow(26);
XSSFCell r1c26 = row26.createCell(0);
r1c26.setCellValue("july_17th");
XSSFRow row27 = sheet.createRow(27);
XSSFCell r1c27 = row27.createCell(0);
r1c27.setCellValue("july_18th");
XSSFRow row28 = sheet.createRow(28);
XSSFCell r1c28 = row28.createCell(0);
r1c28.setCellValue("july_19th");
XSSFRow row29 = sheet.createRow(29);
XSSFCell r1c29 = row29.createCell(0);
r1c29.setCellValue("july_20th");
XSSFRow row30 = sheet.createRow(30);
XSSFCell r1c30 = row30.createCell(0);
r1c30.setCellValue("july_21st");
XSSFRow row31 = sheet.createRow(31);
XSSFCell r1c31 = row31.createCell(0);
r1c31.setCellValue("july_22nd");
XSSFRow row32 = sheet.createRow(32);
XSSFCell r1c32 = row32.createCell(0);
r1c32.setCellValue("july_23rd");
XSSFRow row33 = sheet.createRow(33);
XSSFCell r1c33 = row33.createCell(0);
r1c33.setCellValue("july_24th");
XSSFRow row34 = sheet.createRow(34);
XSSFCell r1c34 = row34.createCell(0);
r1c34.setCellValue("july_25th");
XSSFRow row35 = sheet.createRow(35);
XSSFCell r1c35 = row35.createCell(0);
r1c35.setCellValue("july_26th");
XSSFRow row36 = sheet.createRow(36);
XSSFCell r1c36 = row36.createCell(0);
r1c36.setCellValue("july_27th");
XSSFRow row37 = sheet.createRow(37);
XSSFCell r1c37 = row37.createCell(0);
r1c37.setCellValue("july_23rd");
XSSFRow row38 = sheet.createRow(38);
XSSFCell r1c38 = row38.createCell(0);
r1c38.setCellValue("july_28th");
XSSFRow row39 = sheet.createRow(39);
XSSFCell r1c39 = row39.createCell(0);
r1c39.setCellValue("july_29th");
XSSFRow row40 = sheet.createRow(40);
XSSFCell r1c40 = row40.createCell(0);
r1c40.setCellValue("july_30th");
XSSFRow row41 = sheet.createRow(41);
XSSFCell r1c41 = row41.createCell(0);
r1c41.setCellValue("july_31st");
XSSFRow row42 = sheet.createRow(42);
XSSFCell r1c42 = row42.createCell(0);
r1c42.setCellValue("grand_total");
XSSFRow row43 = sheet.createRow(44);
XSSFCell r1c43 = row43.createCell(0);
r1c43.setCellValue("status");
XSSFRow row44 = sheet.createRow(45);
XSSFCell r1c44 = row44.createCell(0);
r1c44.setCellValue("created_date");
XSSFRow row45 = sheet.createRow(46);
XSSFCell r1c45 = row45.createCell(0);
r1c45.setCellValue("added_by");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Below is the code how I fetched data from that URL and created rows for that response.
private class converttocsv extends AsyncTask<String,Void,String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String cname = "'Goa'";
HashMap<String, String> data = new HashMap<>();
data.put("auth_key", "auth123");
data.put("role_type", "'4'");
data.put("file_id", "4");
String url = "http://vehiclerescue.in/ideadarpan_beta/api/reportApp/get_tsm_data";
String result = Json.performPostCall(url, data);
Log.d("getornotss","getornot"+result);
String projectname = null;
try{
JSONArray jsonArray = new JSONArray(result);
Log.e("TAG_jsonobj", ""+jsonArray);
for(int i=0;i<jsonArray.length();i++){
projectname = jsonArray.get(i).toString();
Log.e("projectname", projectname);
JSONObject jsonObject = new JSONObject(projectname);
Log.e("TAG_jsonobj", ""+jsonObject);
XSSFRow row00 = sheet.getRow(0);
XSSFCell r1c00 = row00.createCell(i+1);
r1c00.setCellValue(jsonObject.getString("id\t"));
Log.e("row creating" +"****", "row created");
XSSFRow row01 = sheet.getRow(1);
XSSFCell r1c01 = row01.createCell(i+1);
r1c01.setCellValue(jsonObject.getString("zone"));
XSSFRow row02 = sheet.getRow(2);
XSSFCell r1c02 = row02.createCell(i+1);
r1c02.setCellValue(jsonObject.getString("scd_code"));
XSSFRow row03 = sheet.getRow(3);
XSSFCell r1c03 = row03.createCell(i+1);
r1c03.setCellValue(jsonObject.getString("asm_name"));
XSSFRow row04 = sheet.getRow(4);
XSSFCell r1c04 = row04.createCell(i+1);
r1c04.setCellValue(jsonObject.getString("tsm_tse_name"));
XSSFRow row05 = sheet.getRow(5);
XSSFCell r1c05 = row05.createCell(i+1);
r1c05.setCellValue(jsonObject.getString("tsm_tse_type"));
XSSFRow row06 = sheet.getRow(6);
XSSFCell r1c06 = row06.createCell(i+1);
r1c06.setCellValue(jsonObject.getString("distributorname"));
XSSFRow row07 = sheet.getRow(7);
XSSFCell r1c07 = row07.createCell(i+1);
r1c07.setCellValue(jsonObject.getString("dist_msisdn"));
XSSFRow row08 = sheet.getRow(8);
XSSFCell r1c08 = row08.createCell(i+1);
r1c08.setCellValue(jsonObject.getString("se_msisdn"));
XSSFRow row09 = sheet.getRow(9);
XSSFCell r1c09 = row09.createCell(i+1);
r1c09.setCellValue(jsonObject.getString("ret_msisdn"));
XSSFRow row10 = sheet.getRow(10);
XSSFCell r1c10 = row10.createCell(i+1);
r1c10.setCellValue(jsonObject.getString("july_1st"));
XSSFRow row11 = sheet.getRow(11);
XSSFCell r1c11 = row11.createCell(i+1);
r1c11.setCellValue(jsonObject.getString("july_2nd"));
XSSFRow row12 = sheet.getRow(12);
XSSFCell r1c12 = row12.createCell(i+1);
r1c12.setCellValue(jsonObject.getString("july_3rd"));
XSSFRow row13 = sheet.getRow(13);
XSSFCell r1c13 = row13.createCell(i+1);
r1c13.setCellValue(jsonObject.getString("july_4th"));
XSSFRow row14 = sheet.getRow(14);
XSSFCell r1c14 = row14.createCell(i+1);
r1c14.setCellValue(jsonObject.getString("july_5th"));
XSSFRow row15 = sheet.getRow(15);
XSSFCell r1c15 = row15.createCell(i+1);
r1c15.setCellValue(jsonObject.getString("july_6th"));
XSSFRow row16 = sheet.getRow(16);
XSSFCell r1c16 = row16.createCell(i+1);
r1c16.setCellValue(jsonObject.getString("july_7th"));
XSSFRow row17 = sheet.getRow(17);
XSSFCell r1c17 = row17.createCell(i+1);
r1c17.setCellValue(jsonObject.getString("july_8th"));
XSSFRow row18 = sheet.getRow(18);
XSSFCell r1c18 = row18.createCell(i+1);
r1c18.setCellValue(jsonObject.getString("july_9th"));
XSSFRow row19 = sheet.getRow(19);
XSSFCell r1c19 = row19.createCell(i+1);
r1c19.setCellValue(jsonObject.getString("july_10th"));
XSSFRow row20 = sheet.getRow(20);
XSSFCell r1c20 = row20.createCell(i+1);
r1c20.setCellValue(jsonObject.getString("july_11th"));
XSSFRow row21 = sheet.getRow(21);
XSSFCell r1c21 = row21.createCell(i+1);
r1c21.setCellValue(jsonObject.getString("july_12th"));
XSSFRow row22 = sheet.getRow(22);
XSSFCell r1c22 = row22.createCell(i+1);
r1c22.setCellValue(jsonObject.getString("july_13th"));
XSSFRow row23 = sheet.getRow(23);
XSSFCell r1c23 = row23.createCell(i+1);
r1c23.setCellValue(jsonObject.getString("july_14th"));
XSSFRow row24 = sheet.getRow(24);
XSSFCell r1c24 = row24.createCell(i+1);
r1c24.setCellValue(jsonObject.getString("july_15th"));
XSSFRow row25 = sheet.getRow(25);
XSSFCell r1c25 = row25.createCell(i+1);
r1c25.setCellValue(jsonObject.getString("july_16th"));
XSSFRow row26 = sheet.getRow(26);
XSSFCell r1c26 = row26.createCell(i+1);
r1c26.setCellValue(jsonObject.getString("july_17th"));
XSSFRow row27 = sheet.getRow(27);
XSSFCell r1c27 = row27.createCell(i+1);
r1c27.setCellValue(jsonObject.getString("july_18th"));
XSSFRow row28 = sheet.getRow(28);
XSSFCell r1c28 = row28.createCell(i+1);
r1c28.setCellValue(jsonObject.getString("july_19th"));
XSSFRow row29 = sheet.getRow(29);
XSSFCell r1c29 = row29.createCell(i+1);
r1c29.setCellValue(jsonObject.getString("july_20th"));
XSSFRow row30 = sheet.getRow(30);
XSSFCell r1c30 = row30.createCell(i+1);
r1c30.setCellValue(jsonObject.getString("july_21st"));
XSSFRow row31 = sheet.getRow(31);
XSSFCell r1c31 = row31.createCell(i+1);
r1c31.setCellValue(jsonObject.getString("july_22nd"));
XSSFRow row32 = sheet.getRow(32);
XSSFCell r1c32 = row32.createCell(i+1);
r1c32.setCellValue(jsonObject.getString("july_23rd"));
XSSFRow row33 = sheet.getRow(33);
XSSFCell r1c33 = row33.createCell(i+1);
r1c33.setCellValue(jsonObject.getString("july_24th"));
XSSFRow row34 = sheet.getRow(34);
XSSFCell r1c34 = row34.createCell(i+1);
r1c34.setCellValue(jsonObject.getString("july_25th"));
XSSFRow row35 = sheet.getRow(35);
XSSFCell r1c35 = row35.createCell(i+1);
r1c35.setCellValue(jsonObject.getString("july_26th"));
XSSFRow row36 = sheet.getRow(36);
XSSFCell r1c36 = row36.createCell(i+1);
r1c36.setCellValue(jsonObject.getString("july_27th"));
XSSFRow row37 = sheet.getRow(37);
XSSFCell r1c37 = row37.createCell(i+1);
r1c37.setCellValue(jsonObject.getString("july_28th"));
XSSFRow row38 = sheet.getRow(38);
XSSFCell r1c38 = row38.createCell(i+1);
r1c38.setCellValue(jsonObject.getString("july_29th"));
XSSFRow row39 = sheet.getRow(39);
XSSFCell r1c39 = row39.createCell(i+1);
r1c39.setCellValue(jsonObject.getString("july_30th"));
XSSFRow row40 = sheet.getRow(40);
XSSFCell r1c40 = row40.createCell(i+1);
r1c40.setCellValue(jsonObject.getString("july_31st"));
XSSFRow row41 = sheet.getRow(41);
XSSFCell r1c41 = row41.createCell(i+1);
r1c41.setCellValue(jsonObject.getString("grand_total"));
XSSFRow row42 = sheet.getRow(42);
XSSFCell r1c42 = row42.createCell(i+1);
r1c42.setCellValue(jsonObject.getString("status"));
XSSFRow row43 = sheet.getRow(43);
XSSFCell r1c43 = row43.createCell(i+1);
r1c43.setCellValue(jsonObject.getString("created_date"));
XSSFRow row44 = sheet.getRow(44);
XSSFCell r1c44 = row44.createCell(i+1);
r1c44.setCellValue(jsonObject.getString("added_by"));
}
fis.close();
fos =new FileOutputStream(new File("C:\\File\\excel.xlsx"));
workbook.write(fos);
fos.flush();
fos.close();
}catch (Exception e){
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.e("TAG", s);
}
}
回答1:
Please Download this two jar files json-20140107.jar and commons-io-2.4.jar put into your libs folder.
1.Download here:commons-io-2.4.jar
2.Download here:json-20140107.jar
try {
if (response.isSuccessful()) {
mProgressDialog.dismiss();
try {
String result = response.body().string();
Log.d("files1st_reports", "*** " + result);
JSONArray docs = new JSONArray(result);
File file=new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/gowthamguru"+"/tmp2/fromJSONss.csv");
String csv = CDL.toString(docs);
FileUtils.writeStringToFile(file, csv);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/vnd.ms-excel");
context.startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I hope this will Work Perfectly..
来源:https://stackoverflow.com/questions/45547767/how-to-write-json-data-into-excel-or-csv-format-android