我正在使用Apache POI
阅读 xlsx 文件。我能够获取结果并且 xlsx 实体成功计数。但是我想在 JSON 中得到我的结果,这样我就可以使用这个JSON数据来制作google graph
或任何graph API
。我是java的新手,我该怎么做?任何帮助将不胜感激。
我的代码详情如下。
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
}
}
}
}
此代码的结果图片在这里。
答案 0 :(得分:0)
一旦将所有javacode输出设置到此对象中,就可以创建一个可扩展对象并将键值对添加到此对象,
只需使用其中一个特定的库将此对象序列化为json。