java把excel內(nèi)容上傳到mysql實(shí)例代碼
mysql 表列名 num1,num2,num3,num4,num5,num6 表名Excle
上傳的方法
package com.web.connection; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; 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; public class TestExcel { //記錄類的輸出信息 static Log log = LogFactory.getLog(TestExcel.class); //獲取Excel文檔的路徑 //.xlsx文件用XSSFWorkbook .xlx 用HSSFWorkbook public static String filePath = "D://demoExcel.xlsx"; public static void main(String[] args) { try { // 創(chuàng)建對(duì)Excel工作簿文件的引用 XSSFWorkbook wookbook = new XSSFWorkbook(new FileInputStream(filePath)); // 在Excel文檔中,第一張工作表的缺省索引是0 // 其語句為:HSSFSheet sheet = workbook.getSheetAt(0); XSSFSheet sheet = wookbook.getSheet("Sheet1"); //獲取到Excel文件中的所有行數(shù) int rows = sheet.getPhysicalNumberOfRows(); //遍歷行 for (int i = 0; i < rows; i++) { // 讀取左上端單元格 XSSFRow row = sheet.getRow(i); // 行不為空 if (row != null) { //獲取到Excel文件中的所有的列 int cells = row.getPhysicalNumberOfCells(); String value = ""; //遍歷列 for (int j = 0; j < cells; j++) { //獲取到列的值 XSSFCell cell = row.getCell(j); if (cell != null) { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_FORMULA: break; case HSSFCell.CELL_TYPE_NUMERIC: value += cell.getNumericCellValue() + ","; break; case HSSFCell.CELL_TYPE_STRING: value += cell.getStringCellValue() + ","; break; default: value += "0"; break; } } } // 將數(shù)據(jù)插入到mysql數(shù)據(jù)庫中 String[] val = value.split(","); TestEntity entity = new TestEntity(); entity.setNum1(val[0]); entity.setNum2(val[1]); entity.setNum3(val[2]); entity.setNum4(val[3]); entity.setNum5(val[4]); entity.setNum6(val[5]); TestMethod method = new TestMethod(); int a=method.add(entity); if(a>0){ System.out.println("插入成功"); } else{ System.out.println("插入失敗"); } } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
其中 TestEntity 為用存放從excel表中查詢到的數(shù)據(jù)的實(shí)體類
package com.web.connection; public class TestEntity { private String num1; private String num2; private String num3; private String num4; private String num5; private String num6; public TestEntity(){ } public String getNum1() { return num1; } public void setNum1(String num1) { this.num1 = num1; } public String getNum2() { return num2; } public void setNum2(String num2) { this.num2 = num2; } public String getNum3() { return num3; } public void setNum3(String num3) { this.num3 = num3; } public String getNum4() { return num4; } public void setNum4(String num4) { this.num4 = num4; } public String getNum5() { return num5; } public void setNum5(String num5) { this.num5 = num5; } public String getNum6() { return num6; } public void setNum6(String num6) { this.num6 = num6; } }
TestMethod 為往mysql表中插入數(shù)據(jù) 的sql語句
package com.web.connection; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class TestMethod { public int add(TestEntity te){ Connection con = DBconnection.getConnection(); PreparedStatement pstmt = null; int count = 0; String sql = " insert into Excle(num1,num2,num3,num4,num5,num6) values(?,?,?,?,?,?)"; try { pstmt = con.prepareStatement(sql); pstmt.setString(1, te.getNum1()); pstmt.setString(2, te.getNum2()); pstmt.setString(3, te.getNum3()); pstmt.setString(4, te.getNum4()); pstmt.setString(5, te.getNum5()); pstmt.setString(6, te.getNum6()); count = pstmt.executeUpdate(); /* * if(count==0){ throw new DataAlreadyExistException(); } */ } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DBconnection.closeConnection(); } return count; } }
總結(jié)
以上就是本文關(guān)于java把excel內(nèi)容上傳到mysql實(shí)例代碼的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
windows上nacos自啟動(dòng)的三種方法小結(jié)
本文主要給大家介紹了windows上nacos自啟動(dòng)的三種方法,借助WinSW.exe添加到服務(wù)列表,修改nacos啟動(dòng)配置以及以開機(jī)"啟動(dòng)"方式——啟動(dòng)Nacos的startup.cmd這三種方法,文中通過圖文講解的非常詳細(xì),需要的朋友可以參考下2023-12-12Spring依賴注入方式(Dependency Injection)
在實(shí)際開發(fā)中,推薦使用構(gòu)造器注入而非字段注入,原因包括官方推薦、代碼簡潔、安全性高和易于測試,構(gòu)造器注入通過在對(duì)象創(chuàng)建時(shí)注入所有必需的依賴,避免運(yùn)行時(shí)忘記注入的問題,且利用Lombok的@RequiredArgsConstructor可自動(dòng)生成構(gòu)造函數(shù)2024-10-10Java實(shí)現(xiàn)Word/Pdf/TXT轉(zhuǎn)html的實(shí)例代碼
本文主要介紹了Java實(shí)現(xiàn)Word/Pdf/TXT轉(zhuǎn)html的實(shí)例代碼,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02springboot 中異步任務(wù),定時(shí)任務(wù),郵件任務(wù)詳解
這篇文章主要介紹了springboot 與異步任務(wù),定時(shí)任務(wù),郵件任務(wù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09SpringBoot Entity中枚舉類型詳細(xì)使用介紹
本文介紹SpringBoot如何在Entity(DAO)中使用枚舉類型。(本文使用MyBatis-Plus)。在實(shí)際開發(fā)中,經(jīng)常會(huì)遇到表示類型或者狀態(tài)的情況,比如:有三種支付方式:微信、支付寶、銀聯(lián)。本文介紹如何這種場景的方案對(duì)比,并用實(shí)例來介紹如何用枚舉這種最優(yōu)雅的來表示2022-10-10