importcom.alibaba.fastjson.JSONArray;importcom.alibaba.fastjson.JSONObject;importorg.apache.poi.xssf.usermodel.XSSFCell;importorg.apache.poi.xssf.usermodel.XSSFRow;importorg.apache.poi.xssf.usermodel.XSSFSheet;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;importjava.io.File;importjava.io.OutputStream;importjava.io.FileOutputStream;importjava.util.Iterator;public classOperExcel {public static voidmain(String[] args) {
String stationInfo_title[]={"Pictures","StationLng","SiteGuide","Address","ServiceTel","SupportOrder","OperatorID","StationID","Remark","StationName","StationTel","StationLat","StationStatus","CountryCode","StationType","EquipmentOwnerID","Construction","MatchCars","ParkFee","ParkInfo","ServiceFee","Payment","ElectricityFee","AreaCode","EquipmentInfos","ParkNums","BusineHours"};//创建excel工作薄
XSSFWorkbook workbook = newXSSFWorkbook();//创建一个工作表sheet
XSSFSheet sheet = workbook.createSheet("stationInfo");//创建第一行
XSSFRow row = sheet.createRow(0);//创建一个单元格
XSSFCell cell = null;//插入第一行标题栏
for (int i=0;i
cell=row.createCell(i);
cell.setCellValue(stationInfo_title[i]);
}//接着写入内容
StarStationInfoToExcel starStationInfoToExcel = newStarStationInfoToExcel();try{//通过接口获取StationInfos内容,json格式,用java解析
JSONObject stationInfosJason =starStationInfoToExcel.getStationInfo();int pageNo = (int)stationInfosJason.get("PageNo");int pageCount = (int)stationInfosJason.get("PageCount");int itemSize =(int)stationInfosJason.get("ItemSize");
System.out.println("pageNo:"+pageNo);
System.out.println("pageCount:" +pageCount);
System.out.println("itemSize:" +itemSize);
JSONArray stationInfosJasonArray= stationInfosJason.getJSONArray("StationInfos");for(int i=0;i
row= sheet.createRow(i+1);//先写行数据,从第二行开始写
JSONObject jasonObject =stationInfosJasonArray.getJSONObject(i);
Iterator stationInfoKeys=jasonObject.keySet().iterator();while(stationInfoKeys.hasNext()) {
String key=(String) stationInfoKeys.next();
String value=(String) jasonObject.getString(key);
cell= row.createCell(colunm); //先建单元格,从0开始
cell.setCellValue(value); //给单元格赋值
colunm ++;
}
}
}catch(Exception e) {
e.printStackTrace();
}//创建一个文件
File file = new File("D:\\javaExample\\file\\stationInfo.xlsx");try{if(!file.exists()) {
file.createNewFile();
}//创建输出流
OutputStream outputStream = newFileOutputStream(file);//将拼好的Excel写入到文件流
workbook.write(outputStream);//关闭输出流
outputStream.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}