2017-08-08 6 views
1

私はxlsxというファイルをApache POIで読み込んでいます。結果を取得できました。xlsxエンティティ数が正常にカウントされました。 JSONに結果を取得したいので、このJSONデータを使用してgoogle graphまたは任意のgraph APIとすることができます。私はJavaに新しいです、どうすればいいのですか?どんな助けもありがとう。javacodeの出力をjsonとして取得するには

マイコードの詳細は以下のとおりです。

package com.amiku.Excel; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.util.*; 

import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.FormulaEvaluator; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

public class ExcelReader 
{ 
    public static void main(String[] args) throws IOException{ 
    { 
     FileInputStream fis = new FileInputStream(new File("C:\\Users\\amiku\\eclipse-workspace\\Amiku\\cloudstreams-connectors-downloads.xlsx")); 

     //create workInstance that refers to .xlsx file 
     XSSFWorkbook wb = new XSSFWorkbook(fis); 

     //create a sheet object to retrive the sheet 
     XSSFSheet sheet = wb.getSheetAt(0); 

     //that is for evalute the cell type 
     FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator(); 
     Map<String, Integer> Details = new HashMap<String,Integer>(); 
     int i=0; 
     List<String> l = new ArrayList<String>(); 
     for (Row row : sheet) 
     { 
      for (Cell cell : row) { 
       l.add(cell.getStringCellValue()); 
       /*HashMap map = new HashMap(); 
       map.put(cell.getStringCellValue());*/ 

      } 
       /*switch(formulaEvaluator.evaluateInCell(cell).getCellType()) 
       {*/ 
       /*//if cell has a numeric format 
       case Cell.CELL_TYPE_NUMERIC: 
        System.out.print(cell.getNumericCellValue() + "\t\t"); 
        break; 

       // if cell has a string format 
       case Cell.CELL_TYPE_STRING: 
        System.out.print(cell.getStringCellValue() + "\t\t"); 
        break; 
       }*/ 


      i++; 
      System.out.println(); 
     } 
     for (int j = 0; j < l.size(); j++) { 
      if (j % 2 == 1) { 

        String var = l.get(j); 

        if (Details.containsKey(var)) { 
         Details.put(var, Details.get(var) + 1); 
        } else { 
         Details.put(var, 1); 
        } 

      } 

     } 
     Iterator it = Details.entrySet().iterator(); 
     while (it.hasNext()) { 
      Map.Entry pair = (Map.Entry) it.next(); 
      System.out.println(pair.getKey() + " = " + pair.getValue()); 
      it.remove(); // avoids a ConcurrentModificationException 
     } 

    } 
    } 
} 

このコードの結果はここにあります。

image

+0

Gsonライブラリを使用してみてくださいhttps://github.com/google/gson –

+0

この問題を解決するライブラリがいくつかあります。 GSON(https://github.com/google/gson)はgoogle(私が好き)、もう1つはJackson(https://github.com/FasterXML/jackson)という名前で呼ばれています。 GSONよりもJSON構造定義の厳密性が優れていますが、私の運用ではGSONの方が使いやすいです。私はこれが助けて欲しい、乾杯、ブラム –

答えて

0

あなたは拡張可能オブジェクトを作成し、このオブジェクトにキーと値のペアを追加し、一度すべてのjavacode出力のは、このオブジェクトに設定されている、

は、単にのいずれかを使用してJSONにこのオブジェクトをシリアル化することができます明確な図書館。

関連する問題