• Excel导数据到数据库


    public static void main(String[] args) throws IOException {
    List<ProProduct> list = readXls();
    for (ProProduct product : list){
    ProProductClient.updateByProduct(product);
    }

    }

    private static List<ProProduct> readXls() throws IOException {
    InputStream is = new FileInputStream("/Users/wuzixin/work/meiliwan/trunk/product.xls");
    HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
    ProProduct xlsDto = null;
    List<ProProduct> list = new ArrayList<ProProduct>();
    // 循环工作表Sheet
    for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
    HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
    if (hssfSheet == null) {
    continue;
    }
    // 循环行Row
    for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
    HSSFRow hssfRow = hssfSheet.getRow(rowNum);
    if (hssfRow == null) {
    continue;
    }
    xlsDto = new ProProduct();
    // 循环列Cell
    // 0学号 1姓名 2学院 3课程名 4 成绩
    HSSFCell proId = hssfRow.getCell(0);
    if (proId == null) {
    continue;
    }
    xlsDto.setProId(Integer.valueOf(String.format("%.0f",Double.parseDouble(getValue(proId)))));

    HSSFCell status = hssfRow.getCell(3);
    if (status == null) {
    continue;
    }else {
    if (getValue(status).equals("下架")){
    xlsDto.setState((short)1);
    }
    }
    HSSFCell mlwPrice = hssfRow.getCell(8);
    if (mlwPrice == null) {
    continue;
    }
    xlsDto.setMlwPrice(new BigDecimal(getValue(mlwPrice)));
    list.add(xlsDto);
    }
    }
    return list;
    }

    @SuppressWarnings("static-access")
    private static String getValue(HSSFCell hssfCell) {
    if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
    // 返回布尔类型的值
    return String.valueOf(hssfCell.getBooleanCellValue());
    } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
    // 返回数值类型的值
    return String.valueOf(String.format("%.2f",hssfCell.getNumericCellValue()));
    } else {
    // 返回字符串类型的值
    return String.valueOf(hssfCell.getStringCellValue());
    }
    }

  • 相关阅读:
    cmd 新建空文件
    查看Linux版本
    centos7时间调整
    正确卸载vs2015及以前版本方式
    vs2017,vs2019 无法连接到Web服务器“IIS Express”
    .netcore开发环境和服务器注意事项
    .netcore 网站启动后 502.5
    CentOS7开机报错piix4_smbus ****host smbus controller not enabled
    centos7 升级系统后,启动界面出现多个选项
    .gitkeep文件
  • 原文地址:https://www.cnblogs.com/blogszixin/p/excel.html
Copyright © 2020-2023  润新知