Java 读取Excel内容并保存进数据库

读取Excel中内容,并保存进数据库
步骤

  1. 建立数据库连接
  2. 读取文件内容 (fileInputStream 放进POI的对应Excel读取接口,实现Excel文件读取)
  3. 获取文件各种内容(总列数,总行数,各个单元格的内容)
  4. 执行SQL语句,存进数据库

public static void main(String[] args) {
		Connection con = null;
		//驱动程序名
		String driver = "";
		//URL指向要访问的数据库名mydata 
		String url = "";
		//MySQL配置时的用户名
		String user = "";
		//MySQL配置时的密码
		String password = "";
		 try {
	            //加载驱动程序
	            Class.forName(driver);
	            //1.getConnection()方法,连接MySQL数据库!!
	            con = DriverManager.getConnection(url,user,password);
	            if(!con.isClosed())
	                System.out.println("Succeeded connecting to the Database!");
	            //2.创建statement类对象,用来执行SQL语句!!
	            Statement statement = con.createStatement();
		 
        Workbook wb =null;
        Sheet sheet = null;
        Row row = null;
        List<Map<String,String>> list = null;
        //文件路径
        String filePath = "";
        wb = readExcel(filePath);
        if(wb != null){
            //用来存放表中数据
            list = new ArrayList<Map<String,String>>();
            //获取第一个sheet
            sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            //获取最大列数
            int colnum = row.getPhysicalNumberOfCells();
            //格式化日期 dateFormat.format
            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd"); 
            //循环读取所有内容
            for (int i = 1; i<rownum; i++) {
            	Row row1=sheet.getRow(i);

            	//sql语句生成并执行
            	String sql="";
            	statement.execute(sql);
            }
        }
        //遍历解析出来的list
        for (Map<String,String> map : list) {
            for (Entry<String,String> entry : map.entrySet()) {
                System.out.print(entry.getKey()+":"+entry.getValue()+",");
            }
            System.out.println();
        }
		 }catch(Exception e) {
			 System.out.println(e.getMessage());
		 }finally {
	         try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

    }
    //读取excel
    public static Workbook readExcel(String filePath){
        Workbook wb = null;
        if(filePath==null){
            return null;
        }
        String extString = filePath.substring(filePath.lastIndexOf("."));
        InputStream is = null;
        try {
            is = new FileInputStream(filePath);
            if(".xls".equals(extString)){
                return wb = new HSSFWorkbook(is);
            }else if(".xlsx".equals(extString)){
                return wb = new XSSFWorkbook(is);
            }else{
                return wb = null;
            }
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }
posted @ 2019-05-26 15:19  花落丶流年  阅读(2862)  评论(0编辑  收藏  举报