zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    HDU5000 (DP + 规律)
    HDU5127 神坑题---vector 、 list 、 deque 的用法区别
    HDU5128 细心、细心、细心
    dij单源最短路纯模板
    POJ 1236 SCC+缩点
    SCC(强连通分量)
    用树状数组求数组内的逆序对数
    HDU 1811 并查集
    大数模板,只要不是手敲,非常好用
    市赛
  • 原文地址:https://www.cnblogs.com/kuoAT/p/9777693.html
Copyright © 2011-2022 走看看