導入EXCEL
阿新 • • 發佈:2018-11-02
static str 上傳 import dig 文件上傳 usermode valueof tip
導入EXCEL
1.傳入對應的表格,轉為list工具類
import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; import java.util.List; 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;/** * 從EXCEL導入到數據庫 * 創建人:FH 創建時間:2014年12月23日 * @version */ public class ObjectExcelRead { /** * @param filepath //文件路徑 * @param filename //文件名 * @param startrow //開始行號 * @param startcol //開始列號 * @param sheetnum //sheet * @return list */ public static List<Object> readExcel(String filepath, String filename, intstartrow, int startcol, int sheetnum) { List<Object> varList = new ArrayList<Object>(); try { File target = new File(filepath, filename); FileInputStream fi = new FileInputStream(target); HSSFWorkbook wb = new HSSFWorkbook(fi); HSSFSheet sheet= wb.getSheetAt(sheetnum); //sheet 從0開始 int rowNum = sheet.getLastRowNum() + 1; //取得最後一行的行號 for (int i = startrow; i < rowNum; i++) { //行循環開始 PageData varpd = new PageData(); HSSFRow row = sheet.getRow(i); //行 int cellNum = row.getLastCellNum(); //每行的最後一個單元格位置 for (int j = startcol; j < cellNum; j++) { //列循環開始 HSSFCell cell = row.getCell(Short.parseShort(j + "")); String cellValue = null; if (null != cell) { switch (cell.getCellType()) { // 判斷excel單元格內容的格式,並對其進行轉換,以便插入數據庫 case 0: cellValue = String.valueOf((int) cell.getNumericCellValue()); break; case 1: cellValue = cell.getStringCellValue(); break; case 2: cellValue = cell.getNumericCellValue() + ""; // cellValue = String.valueOf(cell.getDateCellValue()); break; case 3: cellValue = ""; break; case 4: cellValue = String.valueOf(cell.getBooleanCellValue()); break; case 5: cellValue = String.valueOf(cell.getErrorCellValue()); break; } } else { cellValue = ""; } varpd.put("var"+j, cellValue); } varList.add(varpd); } } catch (Exception e) { System.out.println(e); } return varList; } }
導入案例
/** * 從EXCEL導入到數據庫 */ @RequestMapping(value="/readExcel") @ResponseBody public String readExcel( @RequestParam(value="excel",required=false) MultipartFile file ) throws Exception{ PageData pd = new PageData(); if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} if (null != file && !file.isEmpty()) { String filePath = PathUtil.getClasspath() + Const.FILEpathFILE; //文件上傳路徑 String fileName = FileUpload.fileUp(file, filePath, "userexcel"); //執行上傳 List<PageData> listPd = (List)ObjectExcelRead.readExcel(filePath, fileName, 1, 0, 0); //執行讀EXCEL操作,讀出的數據導入List 1:從第2行開始;0:從第A列開始;0:第0個sheet /*存入數據庫操作======================================*/ pd.put("rights", ""); //權限 pd.put("last_login", ""); //最後登錄時間 pd.put("ip", ""); //ip pd.put("status", "0"); //狀態 pd.put("skin", "default"); //默認皮膚 //得到數據;保存到數據庫 for(int i=0;i<listPd.size();i++){ //查詢身份證信息,如果已經存在,則跳過 if(driverService.getByIdcode(listPd.get(i).getString("var10"))>0){ continue; } PageData pd2 = new PageData(); /* [{var9=是, var8=一車隊, var7=在職, var6=2018-04-03 16:29:36.0, var5=qewe, var4=加盟, var10=21, var3=21, var2=1, var1=男, var0=wqw}]*/ pd2.put("name", listPd.get(i).getString("var0")); if(listPd.get(i).get("var1")!=null&&listPd.get(i).get("var1").toString().equals("男")){ pd2.put("sex",1 ); }else{ pd2.put("sex",2 ); } pd2.put("phone", listPd.get(i).getString("var2")); pd2.put("drive_number", listPd.get(i).getString("var3")); pd2.put("signing_company", listPd.get(i).getString("var4")); pd2.put("entry_time", listPd.get(i).getString("var6")); if(listPd.get(i).get("var7")!=null&&listPd.get(i).get("var7").toString().equals("在職")){ pd2.put("state",1); }else if(listPd.get(i).get("var7")!=null&&listPd.get(i).get("var7").toString().equals("離職")){ pd2.put("state",2); }else{ pd2.put("state",3); } pd2.put("idcode", listPd.get(i).getString("var10")); pd2.put("address", listPd.get(i).getString("var11")); //批量導入時的默認密碼為123456 String md5DigestAsHex = DigestUtils.md5DigestAsHex("123456".getBytes()); pd2.put("password", md5DigestAsHex); pd2.put("id", this.get32UUID()); driverService.save(pd2); } } //刪除所有司機緩存 driverCacheUtil.deleteAllDriver(); return "0"; }
導入EXCEL