zoukankan      html  css  js  c++  java
  • 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());
    }
    }

  • 相关阅读:
    2012 金华 现场赛
    依据错误原理解决Hibernate执行出现No CurrentSessionContext configured!错误
    【python】a[::-1]翻转
    【算法】各种哈希算法
    【wireshark】打开后显示There are no interfaces on which a capture can be done
    【python】在python中调用mysql
    【mysql】执行mysql脚本
    【mysql】用navicat连接虚拟机mysql出现错误代码(10038)
    【python-mysql】在ubuntu下安装python-mysql环境
    【python】lamda表达式,map
  • 原文地址:https://www.cnblogs.com/blogszixin/p/excel.html
Copyright © 2011-2022 走看看