1、添加POI jar包到項目的lib目錄下-
2、Excel文件目錄:d://excel.xls-
3、數據庫字段為:num1 num2 num3 num4 num5 num6-
4、數據庫名:blog-
5、表名:test-
6、編寫類:連接mysql的字符串方法、插入的方法、實體類--
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;-
?
? public ? class ?TestExcel?{-
??????? // 記錄類的輸出信息-
??????? static ?Log?log?=?LogFactory.getLog(TestExcel. class );?-
??????? // 獲取Excel文檔的路徑-
??????? public ? static ?String?filePath?=?"D://excel.xls";-
??????? public ? static ? void ?main(String[]?args)?{-
????????????? try ?{-
??????????????????? // ?創建對Excel工作簿文件的引用-
???????????????????HSSFWorkbook?wookbook?=? new ?HSSFWorkbook( new ?FileInputStream(filePath));-?
??????????????????? // ?在Excel文檔中,第一張工作表的缺省索引是0
??????????????????? // ?其語句為:HSSFSheet?sheet?=?workbook.getSheetAt(0);-
???????????????????HSSFSheet?sheet?=?wookbook.getSheet("Sheet1");-
??????????????????? // 獲取到Excel文件中的所有行數-
??????????????????? int ?rows?=?sheet.getPhysicalNumberOfRows();
??????????????????? // 遍歷行-
??????????????????? for ?( int ?i?=?0;?i?<?rows;?i++)?{-
????????????????????????? // ?讀取左上端單元格-
?????????????????????????HSSFRow?row?=?sheet.getRow(i);-
????????????????????????? // ?行不為空-
????????????????????????? if ?(row?!=? null )?{-
??????????????????????????????? // 獲取到Excel文件中的所有的列-
??????????????????????????????? int ?cells?=?row.getPhysicalNumberOfCells();-
???????????????????????????????String?value?=?"";?????-
??????????????????????????????? // 遍歷列-
??????????????????????????????? for ?( int ?j?=?0;?j?<?cells;?j++)?{-
????????????????????????????????????? // 獲取到列的值-
?????????????????????????????????????HSSFCell?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 ;-
?????????????????????????????????????}-
???????????????????????????????}??????
?????????????????????????}-
????????????????????????? // ?將數據插入到mysql數據庫中-
?????????????????????????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();-
?????????????????????????method.Add(entity);-
???????????????????}-
??????????????}-
???????}? catch ?(FileNotFoundException?e)?{-
?????????????e.printStackTrace();-
???????}? catch ?(IOException?e)?{-
?????????????e.printStackTrace();-
???????}-
????}-
?}-
? 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;-
?
? public ? class ?TestExcel?{-
??????? // 記錄類的輸出信息-
??????? static ?Log?log?=?LogFactory.getLog(TestExcel. class );?-
??????? // 獲取Excel文檔的路徑-
??????? public ? static ?String?filePath?=?"D://excel.xls";-
??????? public ? static ? void ?main(String[]?args)?{-
????????????? try ?{-
??????????????????? // ?創建對Excel工作簿文件的引用-
???????????????????HSSFWorkbook?wookbook?=? new ?HSSFWorkbook( new ?FileInputStream(filePath));-?
??????????????????? // ?在Excel文檔中,第一張工作表的缺省索引是0
??????????????????? // ?其語句為:HSSFSheet?sheet?=?workbook.getSheetAt(0);-
???????????????????HSSFSheet?sheet?=?wookbook.getSheet("Sheet1");-
??????????????????? // 獲取到Excel文件中的所有行數-
??????????????????? int ?rows?=?sheet.getPhysicalNumberOfRows();
??????????????????? // 遍歷行-
??????????????????? for ?( int ?i?=?0;?i?<?rows;?i++)?{-
????????????????????????? // ?讀取左上端單元格-
?????????????????????????HSSFRow?row?=?sheet.getRow(i);-
????????????????????????? // ?行不為空-
????????????????????????? if ?(row?!=? null )?{-
??????????????????????????????? // 獲取到Excel文件中的所有的列-
??????????????????????????????? int ?cells?=?row.getPhysicalNumberOfCells();-
???????????????????????????????String?value?=?"";?????-
??????????????????????????????? // 遍歷列-
??????????????????????????????? for ?( int ?j?=?0;?j?<?cells;?j++)?{-
????????????????????????????????????? // 獲取到列的值-
?????????????????????????????????????HSSFCell?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 ;-
?????????????????????????????????????}-
???????????????????????????????}??????
?????????????????????????}-
????????????????????????? // ?將數據插入到mysql數據庫中-
?????????????????????????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();-
?????????????????????????method.Add(entity);-
???????????????????}-
??????????????}-
???????}? catch ?(FileNotFoundException?e)?{-
?????????????e.printStackTrace();-
???????}? catch ?(IOException?e)?{-
?????????????e.printStackTrace();-
???????}-
????}-
?}-
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
