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();
            }
        }
    }
  • 相关阅读:
    Windows 科研软件推荐
    有关Python 包 (package) 的基本知识
    《Using Python to Access Web Data》Week4 Programs that Surf the Web 课堂笔记
    Coursera助学金申请模板
    《Using Databases with Python》 Week2 Basic Structured Query Language 课堂笔记
    Jupyter 解决单个变量输出问题
    解决 pandas 中打印 DataFrame 行列显示不全的问题
    《Using Python to Access Web Data》 Week3 Networks and Sockets 课堂笔记
    缓存击穿及解决方案
    jvm垃圾收集器
  • 原文地址:https://www.cnblogs.com/kuoAT/p/9777693.html
Copyright © 2011-2022 走看看