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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM