我想提取并显示 Json 数组数据到 Excel sheet

I want to extract and display Json array data into Excel sheet

我有一个 activity 任务 list.this 任务列表包含 json 数组 我现在有一个按钮 我想在按钮单击

时将任务列表导出到 Excel sheet

创建Excelsheet我用过poi-37.jar

我的代码 Excel sheet:

    public void onClick(View v) {
        // TODO Auto-generated method stub

          switch (v.getId()) 
            {
            case R.id.param11:
                saveExcelFile(this,"myexcel.xls");
                break;

            }
            }


 private static boolean saveExcelFile(Context context, String fileName) { 

            // check if available and not read only 
            if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) { 
                Log.e(TAG, "Storage not available or read only"); 
                return false; 
            } 

            boolean success = false; 

            //New Workbook
            Workbook wb = new HSSFWorkbook();

            Cell c = null;

            //Cell style for header row
            CellStyle cs = wb.createCellStyle();
            cs.setFillForegroundColor(HSSFColor.LIME.index);
            cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

            //New Sheet
            Sheet sheet1 = null;
            sheet1 = wb.createSheet("wrok");

            // Generate column headings
            org.apache.poi.ss.usermodel.Row row = sheet1.createRow(0);

            c = row.createCell(0);
            c.setCellValue("task_id");
            c.setCellStyle(cs);

            c = row.createCell(1);

            c = row.createCell(2);
            c.setCellValue("d_alias");
            c.setCellStyle(cs);


            c = row.createCell(3);
            c.setCellValue("worktype_name");
            c.setCellStyle(cs);


            c = row.createCell(4);
            c.setCellValue("status");
            c.setCellStyle(cs);


            c = row.createCell(5);
            c.setCellValue("departmentstatus");
            c.setCellStyle(cs);

            c = row.createCell(6);
            c.setCellValue("priority");
            c.setCellStyle(cs);
            c = row.createCell(7);
            c.setCellValue("staff_name");
            c.setCellStyle(cs);

            sheet1.setColumnWidth(0, (15 * 500));
            sheet1.setColumnWidth(1, (15 * 500));
            sheet1.setColumnWidth(2, (15 * 500));

            // Create a path where we will place our List of objects on external storage 
            File file = new File(context.getExternalFilesDir(null), fileName); 
            FileOutputStream os = null; 

            try { 
                os = new FileOutputStream(file);
                wb.write(os);
                Log.w("FileUtils", "Writing file" + file); 
                success = true; 
            } catch (IOException e) { 
                Log.w("FileUtils", "Error writing " + file, e); 
            } catch (Exception e) { 
                Log.w("FileUtils", "Failed to save file", e); 
            } finally { 
                try { 
                    if (null != os) 
                        os.close(); 
                } catch (Exception ex) { 
                } 
            } 
            return success; 
        } 


 public static boolean isExternalStorageReadOnly() { 
            String extStorageState = Environment.getExternalStorageState(); 
            if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) { 
                return true; 
            } 
            return false; 
        } 

        public static boolean isExternalStorageAvailable() { 
            String extStorageState = Environment.getExternalStorageState(); 
            if (Environment.MEDIA_MOUNTED.equals(extStorageState)) { 
                return true; 
            } 
            return false; 
        } 


///i have converted json string to csv

用于将 json 转换为 csv 我使用了 org.json.jar

if (array.length() > 0) {
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject obj = (JSONObject) array.get(i);
                        item.add(obj.getString("task_id").toString().trim());

                        WorkItem item = new WorkItem(obj.getString("id"),
                                obj.getString("task_id"),
                                obj.getString("firm_name"),
                                obj.getString("d_alias"),
                                obj.getString("worktype_name"),
                                obj.getString("status"),
                                obj.getString("departmentstatus"),
                                obj.getString("priority"),
                                obj.getString("staff_name"),
                                obj.getString("remark"));


                        String  csv = CDL.toString(array);

                        System.out.println("csv "    +csv); //csv
                        rowItems.add(item);
                        System.out.println("itemss"  +item);

                    }

我的 excelsheet 创建成功,但它只包含 headers

我的json字符串:

object[{"id":"10","task_id":"drm\/Technical \/75","firm_name":"dreamhome","d_alias":"Technical ","worktype_name":"Technical","status":"completed","departmentstatus":"completed","priority":"high","staff_name":"anna","remark":"good","createddate":"2015-02-11 03:42:17","updateddate":"2015-02-11 03:42:17","isdeleted":"0"},{"id":"10","task_id":"sv\/Technical \/89","firm_name":"svye","d_alias":"Technical ","worktype_name":"aa","status":"Submitted","departmentstatus":"incomplete","priority":"5.0","staff_name":"sam","remark":"jdjd","createddate":"2015-02-12 23:59:05","updateddate":"2015-02-12 23:59:05","isdeleted":"0"}] 

//csv 字符串

02-18 14:41:43.531: I/System.out(854): 10,Technical,good,a,completed,high,0,Technical ,drm/Technical /75,2015-02-11 03:42:17,2015-02-11 03:42:17,completed,devrai
02-18 14:41:43.531: I/System.out(854): 10,aa,jdjd,Vishal,Submitted,5.0,0,Technical ,sv/Technical /89,2015-02-12 23:59:05,2015-02-12 23:59:05,incomplete,devrai

我不知道如何转换 csv 并将其保存到 excel sheet。 请帮助我如何实现这一目标。

只需迭代 rowItems 并使用 createRow / createCell 无需 csv。

之后
sheet1.setColumnWidth(2, (15 * 500));

添加如下内容:

int row = 1;
for (final WorkItem item: rowItems) {
    final Row row = sheet1.createRow(row);
    int cell = 0;
    Cell cell = row.createCell(cell++);
    cell.setCellValue(item.getTaskId());
    cell = row.createCell(cell++);
    cell.setCellValue(item.getDAlias());
    // ...
    row += 1;
}

一些阅读:Busy Developers' Guide to HSSF and XSSF Features