用java解析從接口獲取json內容並用XSSFWorkbook寫到excel(.xlsx)


1.接口獲取jason數據

2.用java解析該json數據

3.用Apache POI提供的相關API(XSSFWorkbook等)將獲取的數據寫入到excel中(.xlsx)

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.util.Iterator;

public class OperExcel {
    public static void main(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 = new XSSFWorkbook();
        //創建一個工作表sheet
        XSSFSheet sheet = workbook.createSheet("stationInfo");
        //創建第一行
        XSSFRow row = sheet.createRow(0);
        //創建一個單元格
        XSSFCell cell = null;
        //插入第一行標題欄
        for (int i=0;i<stationInfo_title.length;i++) {
            cell = row.createCell(i);
            cell.setCellValue(stationInfo_title[i]);
        }
        
        //接着寫入內容
        StarStationInfoToExcel starStationInfoToExcel = new StarStationInfoToExcel();
        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<stationInfosJasonArray.size();i++) {
                int colunm = 0;
                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 = new FileOutputStream(file);
            //將拼好的Excel寫入到文件流
            workbook.write(outputStream);
            //關閉輸出流
            outputStream.close();
        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM