POI 讀取Excel文件 並解析JSON數據

 

 

package skuPrice;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JDSku {

    public static void main(String[] args) {
        String[] strs = args;
        String filename = strs[0];
        String sheetname = strs[1];    
        getSkuPrice(filename,sheetname);
    }
    /**
     * @author wk
     * @date  2019/09/18
     * @param fileName
     * @param bookName
     */
    public static void getSkuPrice(String fileName,String bookName){
        System.out.println("進入解析價格方法..........");
        InputStream input = null;
        FileOutputStream output = null;
        long start = System.currentTimeMillis();
        try {
            System.out.println("**********正在讀取「"+fileName+".xlsx」文件**************");
            input = new FileInputStream(new File("D:\\"+fileName+".xlsx"));
            String skujson = "";
            String skus = "";
            XSSFWorkbook work = new XSSFWorkbook(input);
            XSSFSheet sheet = work.getSheet(bookName);
            int linenum = sheet.getLastRowNum();//獲取excel
            System.out.println(sheet.getLastRowNum());
            int flag = 0;
            try{
                for (int i = 1; i <= linenum; i++) {//讀取每一行數據
                    flag = i;
                    XSSFRow row = sheet.getRow(i);
                    short lastcellnum= row.getLastCellNum();
                    row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
                    row.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
                    skujson= row.getCell(7).getStringCellValue();       //獲取包含sku的價格
                    skus = row.getCell(9).getStringCellValue();       //獲取sku
                    System.out.println("EXPAND_INFO:"+skujson.toString());
                    JSONObject skuob = JSONArray.parseObject(skujson);
                    String pricestr = skuob.getString("price");
                    JSONObject pricejson = JSONArray.parseObject(pricestr);
                    String price= pricejson.getString(skus);
                    System.out.println(skus+"============"+price);
                    try{
                        row.createCell(lastcellnum+1, Cell.CELL_TYPE_STRING);
//                        row.getCell(15).setCellType(Cell.CELL_TYPE_STRING);
                        row.getCell(lastcellnum+1).setCellValue(price);
                    }catch (Exception e) {
                        e.printStackTrace();
                        continue;
                    }
                }
            }catch(Exception e){
                output = new FileOutputStream(new File("D:\\"+fileName+"解析後文件.xlsx"));
                work.write(output);//寫入excel
                System.out.println("解析到第"+(flag+1)+"行,部分解析成功,請重試.........");
                e.printStackTrace();
            }
            output = new FileOutputStream(new File("D:\\"+fileName+"解析後文件.xlsx"));
            work.write(output);//寫入excel
            long end = System.currentTimeMillis();
            long use = (end-start)/60000;
            System.out.println("*********SUCCESS*********");
            System.err.println("共用時:"+use+"分鐘");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
相關文章
相關標籤/搜索