mysql 表列名 num1,num2,num3,num4,num5,num6 表名Excle
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、小程序定制開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了岳普湖免費(fèi)建站歡迎大家使用!上傳的方法
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 // 其語(yǔ)句為: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ù)庫(kù)中 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(); } } }