json转Excel

package com.pl.dcloud.xmlParse;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


import javax.annotation.Resource;


import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


import org.apache.commons.lang.StringUtils;
import org.junit.Test;


import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;
import com.pl.dcloud.model.ResourceType;
import com.pl.dcloud.service.ResourceTypeService;
import com.pl.test.BaseTester;




public class JosnToExcel extends BaseTester {

@Resource
private  ResourceTypeService rService;
/**
* 生成Excel
* Created by dx-ycw on 2017/05/26
* @return
*/


@SuppressWarnings({ "unchecked", "resource" })
public Map partWord(File file){
Map map2 = new HashMap();



Map m = new HashMap();
List resourceTypes = rService.getAllList();
for (ResourceType r : resourceTypes) {
m.put(r.getCode()+"-"+r.getSubCode(), r);
}

InputStream in = null;


try{
in = new FileInputStream(file);//得到文件流,File file = new File("E:\\test\\20170605\\data.json");


BufferedReader br = new BufferedReader(new InputStreamReader(in));


String str;


while((str = br.readLine()) != null){
str = StringUtils.substring(str, str.indexOf('{') + 1,str.length());
Gson gson = new Gson(); 

//json字符串转换Map集合
Map map = gson.fromJson(str, HashMap.class);//解析语句
//System.out.println(map.size());
Set key = map.keySet();
Iterator iter = key.iterator();
while (iter.hasNext()) {
String field = iter.next();
if (field.equalsIgnoreCase("objects")){
//System.out.println(field + ":" + map.get(field));
JSONArray jsonArray = JSONArray.fromObject(map.get(field));
for (int i=0;i Object meta = jsonArray.getJSONObject(i).get("meta");
if (meta != null){
//System.out.println(jsonArray.getJSONObject(i).get("meta"));
JSONObject jObj =JSONObject.fromObject(meta);
String typeStr = String.valueOf(jObj.get("type"));
String typeSubStr = String.valueOf(jObj.get("subType"));
ResourceType r= (ResourceType) m.get(typeSubStr+"-"+typeStr);
map2.put("typeStr", typeStr);

JSONObject sizeObject = JSONObject.fromObject(jObj.get("size"));
if (sizeObject != null){
String width = String.valueOf(sizeObject.get("width"));
String height = String.valueOf(sizeObject.get("height"));
String depth = String.valueOf(sizeObject.get("depth"));
System.out.println("width");
System.out.println(sizeObject.get("height"));
System.out.println(sizeObject.get("depth"));
map2.put("width", width);
map2.put("height", height);
map2.put("depth", depth);
}
}
}
}
}
}
return map2;
}catch(Exception e){
e.printStackTrace();
}
return map2;
}


//然后将解析出来的放到eXCEL表中
public static void writeExcel(String path, Map content,String sheetName, int sheetNum, int startRow) {
try {
// 打开文件
WritableWorkbook book = Workbook.createWorkbook(new File(path));
// 生成名为“第一页”的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet(sheetName, sheetNum);
// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
// 以及单元格内容为test
int start = startRow;
for (String key : content.keySet()) {


Label label1 = new Label(0, start, key);


Label label2 = new Label(1, start, content.get(key));


// 将定义好的单元格添加到工作表中


sheet.addCell(label1);


sheet.addCell(label2);


start++;
}
// 写入数据并关闭文件
book.write();
book.close();
} catch (Exception e) {
System.out.println(e);
}
}


@Test
public void test(){
// JosnToExcel jtx = new JosnToExcel();
// jtx.DCEecel();    //E:\\test\\20170527\\1b.json
writeExcel("E:\\test\\20170605\\data.xls",partWord(new File("E:\\test\\20170605\\data.json")), "BOM", 0, 1);
System.out.println("測試成功!!");
}

}


//json数据格式如图片

你可能感兴趣的:(json转Excel)