この記事では、主にSpringboot-Mybatisフレームワークの下でExcelをアップロードし、MySQLデータベースにインポートするプロセスについて説明します。ユーザーIDが既に存在する場合は、データベースの情報を更新および変更します。フロントエンド分離技術が使用されるため、バックエンドJavaパーツの主なレコードはバックエンドインターフェイスです。フロントエンドインターフェイスに接続して関数を実装し、LayUIなどのフロントエンドフレームワークを使用して接続できます。フロントエンドコードを自分で書くこともできます。この記事はコントローラーから始まり、辞書テーブルの変換を含む導入プロセスから始まります。
1.主にPOIを使用して、pom.xmlファイルに注釈をインポートします
<Dependency> groupId> org.apache.poi </groupid> <artifactid> poi-ooxml </artifactid> <version> 3.9 </version> </dependency> <deprofency> commons-fileupload </groupId> <artifactid> commons-fileupload </depentid> </</</</> </</</</</</</</</</</biversion <gripid> commons-io </groupid> <artifactid> commons-io </artifactid> <version> 2.4 </version> </dependency>
2.コントローラーインターフェイス
@postmapping( "/save")public string adduser(@requestparam( "file")multipartfile file){string filename = file.getoriginalfilename(); try {return sysservice.batchimport(filename、file); } catch(myexception e){e.printstacktrace(); return e.getMessage(); } catch(Exception e){e.printstacktrace(); 「ファイルの例外、インポートが失敗した」を返します。 }}3。サービスレイヤーインターフェイス
Boolean Import(String Filename、MultiPartFileファイル)は例外をスローします。
4。ビジネス層の実装クラス
@transactional(readonly = false、lollbackfor = exception.class)@override public boolean import(string filename、multipartfile file)slows exception {map <string、integer> departmentmap = finddepartment(); map <string、integer> rolemap = findrole(); boolean notnull = false; List <user> userlist = new ArrayList <user>(); if(!filename.matches( "^。+//。(?i)(xls)$")&&!filename.matches( "^。+//。(?i)(xlsx)$")){throw new myexception( "アップロードファイル形式は不正"); } boolean isexcel2003 = true; if(filename.matches( "^。+//。(?i)(xlsx)$")){isexcel2003 = false; } inputstream is = file.getInputStream();ワークブックwb = null; if(isexcel2003){wb = new hssfworkbook(is); } else {wb = new xssfworkbook(is); }シートシート= wb.getsheetat(0); if(sheet!= null){notnull = true; }ユーザー; for(int r = 1; r <= sheet.getlastrownum(); r ++){row row = sheet.getrow(r); if(row == null){続行; } user = new user(); if(row.getCell(0).getCellType()!= 1){throw new MyException( "Import failed(row"+1)+"およびIDセル形式がテキスト形式に設定されています"); } string id = row.getCell(0).getStringCellValue(); if(id == null || id.isempty()){throw new MyException( "Import failed(row"+(r+1)+"およびIDが記入されていない)"); } string name = row.getCell(1).getStringCellValue(); if(name == null || name.isempty()){throw new myException( "Import failed(line"+(r+1)+"および名前が記入されていない)"); } string department = row.getCell(2).getStringCellValue(); if(departmentmap.get(department)== null){新しいmyException( "Import failed(line"+(r+1)+")を投げます。このユニットは存在しないか、ユニットが入力されません)"); } string role = row.getCell(3).getStringCellValue(); if(rolemap.get(role)== null){new myexception( "Import failed(line"+(r+1)+")、この役割は存在しないか、役割が埋められない)"); }日付; if(row.getCell(4).getCellType()!= 0){throw new MyException( "Import failed(line"+1)+"、エントリ日付形式が間違っているか、塗りつぶされていない); } else {date = row.getCell(4).getDateCellValue(); } user.setId(id); user.setname(name); user.setDepartmentId((int)departmentmap.get(department)); user.setroleid((int)rolemap.get(role)); user.setdate(date); userlist.add(user); } for(user user:userlist){string id = user.getId(); int cnt = usermapper.seerectbyid(id); if(cnt == 0){usermapper.adduser(user); } else {usermapper.updateuserbyid(user); }} return notnull; }要約します
上記は、編集者があなたに紹介するものです。 Springbootは、MySQLデータベースにテーブルデータをExcelにアップロードおよびインポートします。私はそれが誰にでも役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は、すべての人に時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!