将JSON数组值导出为ex​​cel

时间:2015-01-23 10:37:38

标签: java json excel

我想将JSONArray值导出到Excel工作表。

这是我的JSONData:

[
   {
     "scandName": "hdjdido",
     "cmpnyName": "Regency",
"address": "2511 South Ocean.",
"phnNum1": "8516 ",
    "phnNum2": "0700 ",
     "emailId": "gm@regeb.com ",
     "designation": "G.M."
   },
   {
    "scandName": "mukesh",
    "cmpnyName": "Economy Inc",
   "address": "3500 west 5th .",
     "phnNum1": " 737541 ",
    "phnNum2": " 739541 ",
     "emailId": "economy@gmail.com ",
    "designation": "Manager"
   },
   {
    "scandName": "Elizabeth",
    "cmpnyName": "The Brown Inc ",
    "address": "12115 Chatsworth ",
    "phnNum1": "341554 ",
    "phnNum2": "341938 ",
    "emailId": "pbrown16@carcom ",
    "designation": "Owner"
   },
   {
    "scandName": "Ginny ",
    "cmpnyName": "Freebornes Inc ",
    "address": "p.o. box 15 ",
    "phnNum1": " 358008 ",
    "phnNum2": " ",
    "emailId": "gabsher@gmail.com ",
     "designation": "bar manager/ow"
  }
 ] 

所以现在我想将这些值传递给excel表。

这是我的Java代码:     尝试             {

            jsonArray = new JSONArray(sdf);


             for (int i = 0; i < jsonArray.length(); i++) 
             {

                    jsonobject = jsonArray.getJSONObject(i);

                    scnName = jsonobject.getString("scandName");
                    cmp    =  jsonobject.getString("cmpnyName");
                    addr  =   jsonobject.getString("address");
                    phn1  =   jsonobject.getString("phnNum1");
                    phn2  =   jsonobject.getString("phnNum2");
                    emil  =   jsonobject.getString("emailId");
                    des  =    jsonobject.getString("designation");
            al.add(scnName);
                al.add(cmp);
                al.add(addr);
                al.add(phn1);
                al.add(phn2);
                al.add(emil);
                al.add(des);


                }

             data.add(al);

             System.out.println("data is" +data);

             exportToExcel("Test", headers, data, file123);

        }catch(Exception e)
        {
            e.printStackTrace();
        }

exportToexcel()方法:

 public static void exportToExcel(String sheetName, ArrayList headers,
                    ArrayList data, File outputFile) throws HPSFException 
     {

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet(sheetName);

        int rowIdx = 0;
        short cellIdx = 0;

        // Header
        HSSFRow hssfHeader = sheet.createRow(rowIdx);
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        for (Iterator cells = headers.iterator(); cells.hasNext();) {
            @SuppressWarnings("deprecation")
            HSSFCell hssfCell = hssfHeader.createCell(cellIdx++);
            hssfCell.setCellStyle(cellStyle);
            hssfCell.setCellValue((String) cells.next());
        }

        // Data
        rowIdx = 1;
        for (Iterator rows = data.iterator(); rows.hasNext();) {
            ArrayList row = (ArrayList) rows.next();
            HSSFRow hssfRow = sheet.createRow(rowIdx++);
            cellIdx = 0;
            for (Iterator cells = row.iterator(); cells.hasNext();) {
                @SuppressWarnings("deprecation")
                HSSFCell hssfCell = hssfRow.createCell(cellIdx++);
                hssfCell.setCellValue((String) cells.next());
            }
        }

        //wb.setSheetName(0, sheetName, HSSFWorkbook.ENCODING_COMPRESSED_UNICODE);
        try {
            FileOutputStream outs = new FileOutputStream(outputFile);
            wb.write(outs);
            outs.close();
        } catch (IOException e) {
            throw new HPSFException(e.getMessage());
        }

所以当我运行这个应用程序时,我会不断地在excel表中获得值。

如何通过设置获取数据?

0 个答案:

没有答案