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(); } } }