如何将JSON数据写入excel或csv格式的Android?

时间:2017-08-07 13:11:07

标签: android excel csv android-asynctask apache-poi

我尝试在收到服务器响应后,将TUNING_PARAMETER响应转储到带有JSON文件的excel表中。我试过了.csvpoi-3.7.jar个库。但我得到一张空的excel表。

请查看我的以下代码以获取更多参考。

我的JSON网址poi-ooxml-3.11.jar

编码我创建行的方式:

http://vehiclerescue.in/ideadarpan_beta/api/reportApp/get_tsm_data

下面是我从该URL获取数据并为该响应创建行的代码。

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();
}

1 个答案:

答案 0 :(得分:1)

请将这两个jar文件 json-20140107.jar commons-io-2.4.jar 下载到您的libs文件夹中。

1.在此处下载:commons-io-2.4.jar

2.在此下载: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();
        }
    }
}

我希望这会完美地运作..