1. 程式人生 > 其它 >Excel的資料匯入資料庫

Excel的資料匯入資料庫


所需的Maven

<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
</dependencies>



import
org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFDataFormatter; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import
java.io.File; import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Excel { public static void main(String[] args) { //jdbc的連線部分 String driver = "com.mysql.jdbc.Driver";
//資料庫 String url = "jdbc:mysql://127.0.0.1:3306/spms_bs?useUnicode=true&characterEncoding=UTF8&useSSL=false"; Connection conn = null; Statement statement = null; //資料庫欄位資訊 String uid = ""; String uname; String usex; //從表格中匯入的資料存放在這裡 StringBuilder values = new StringBuilder(); try { Class.forName(driver); conn = (Connection) DriverManager.getConnection(url, "root", "123456"); } catch (Exception e) { System.err.println("JDBC沒有啟動成功"); e.printStackTrace(); } //建立要上傳檔案物件 File file = new File("C:\\Users\\Administrator\\Desktop\\測試表格.xlsx"); //讀取檔案 try { FileInputStream excelFile = new FileInputStream(file); //這裡注意Excel的版本 //HSSF對應97-2003版本的Excel(.xls),XSSF則對應2007版本的Excel(.xlsx)。 XSSFWorkbook xssfWorkbook = new XSSFWorkbook(excelFile); // 如果有多個sheety頁面獲取excel中的sheet頁 // for (int sheetXssf = 0; sheetXssf < xssfWorkbook.getNumberOfSheets(); sheetXssf++) { // XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(sheetXssf); // //如果為空表就跳過 // if (xssfSheet.getLastRowNum() == 0) { // continue; // } // } //獲取內容 XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); for (int numRow = 1; numRow <= xssfSheet.getLastRowNum(); numRow++) { XSSFRow xssfRow = xssfSheet.getRow(numRow); //獲取行資訊 XSSFCell num = xssfRow.getCell(0); //因為數字預設是double型別的,所以這裡要進行一下轉換 //判斷資料型別是否為數字型別 if (num.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { HSSFDataFormatter dataFormatter = new HSSFDataFormatter(); //轉換成String型別 uid = dataFormatter.formatCellValue(num); } uname = xssfRow.getCell(1).toString(); usex = xssfRow.getCell(2).toString(); //拼接資料 values.append("(" + uid + ",'" + uname + "','" + usex + "'),"); System.out.println(values); } //去除掉最後一個",",避免語句錯誤 if (',' == values.charAt(values.length() - 1)) { values = values.deleteCharAt(values.length() - 1); } } catch (Exception e) { System.err.println("檔案讀取失敗"); e.printStackTrace(); } //建立sql語句 String sql = "insert into tspti01(uid,uname,usex) values" + values; System.out.println(sql); try { //執行語句插入資料 statement = conn.createStatement(); int result = statement.executeUpdate(sql); if (result > 0) { System.out.println("插入成功"); } else { System.out.println("插入失敗"); } } catch (SQLException e) { System.err.println("資料庫插入異常"); e.printStackTrace(); } finally { //關閉資源 try { if (statement != null) { statement.close(); } if (conn != null) { conn.close(); } } catch (Exception e) { e.printStackTrace(); } } } }