當前位置:
首頁 > 知識 > Excel表格導入Mysql資料庫,一行存入多條數據的前後台完整實現思路

Excel表格導入Mysql資料庫,一行存入多條數據的前後台完整實現思路

現在有一張Excel表格:

存入資料庫時需要這樣存放:

現在需要將Excel表格做處理,將每一行拆分成多條數據存入資料庫。

1.首先在前台jsp頁面畫一個按鈕:,加入點擊事件:


定義對應的URL:

//打開上傳excel頁面
function fromExcel{
top.jzts;
var diag = new top.Dialog;
diag.Drag=true;
diag.Title ="EXCEL 導入到資料庫";
//定義對應的URL地址
diag.URL = "<%=basePath%>man/goUploadManExcel.do";
diag.Width = 300;
diag.Height = 150;
diag.CancelEvent = function{ //關閉事件
if(diag.innerFrame.contentWindow.document.getElementById("zhongxin").style.display == "none"){
if("${page.currentPage}" == "0"){
top.jzts;
setTimeout("self.location.reload",100);
}else{
nextPage(${page.currentPage});
}
}
diag.close;
};
diag.show;
}

2.在對應的Controller里返回一個上傳頁面:

/**打開上傳EXCEL頁面
* @return
* @throws Exception
*/
@RequestMapping(value="/goUploadManExcel")
public ModelAndView goUploadExcelthrows Exception{
ModelAndView mv = this.getModelAndView;
mv.setViewName("manpower/man/man_uploadexcel");
return mv;
}

對應的文件目錄:

Excel表格導入Mysql資料庫,一行存入多條數據的前後台完整實現思路

返回一個頁面:

Excel表格導入Mysql資料庫,一行存入多條數據的前後台完整實現思路

這裡只寫關鍵代碼:


...
導入
取消
/man/downManExcel.do"">下載模版

點擊導入時提交此form表單:

$("#Form").submit;

在後台攔截URL,進行處理(這裡是樓主主要想表達的對Excel表格一行數據拆成多行的處理):

/**從EXCEL導入到資料庫
* @param file
* @return
* @throws Exception
*/
@RequestMapping(value="/readManExcel")
public ModelAndView readExcel(
@RequestParam(value="excel",required=false) MultipartFile file
) throws Exception{
FHLOG.save(Jurisdiction.getUsername, "從EXCEL導入到資料庫");
ModelAndView mv = this.getModelAndView;
PageData pd = new PageData;
PageData pdman = 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, "manexcel"); //執行上傳
List listPd = (List)ObjectExcelRead.readExcel(filePath, fileName, 0, 0, 0); //執行讀EXCEL操作,讀出的數據導入List 2:從第1行開始;0:從第A列開始;0:第0個sheet
//存放每行月份及實際投入
List monthList = new ArrayList;
List actualList = new ArrayList;
//遍歷表格每一行
for(int i=1;i

對應的service:

@Override
public void saveMan(PageData pd) throws Exception {
dao.save("ManMapper.saveMan", pd);
}

到對應的ManMapper.xml里寫sql語句:



man_power



MAN_ID,
MAN_NAME,
MAN_NUMBER,
PRJECT_NAME,
PUT_MONTH,
ACTUAL_INPUT


#{MAN_ID},
#{MAN_NAME},
#{MAN_NUMBER},
#{PRJECT_NAME},
#{PUT_MONTH},
#{ACTUAL_INPUT}



insert into

(

) values (

)

重啟Tomcat,數據表就在資料庫里生成了。

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 達人科技 的精彩文章:

admin 後台美化處理 JSONField

TAG:達人科技 |

您可能感興趣

mybatis使用load data local infile實現導入數據到mysql資料庫
SpringBoot整合MyBatis,MySql之從前台頁面到資料庫的小Demo
windows本地mysql資料庫存入中文亂碼
增量同步mysql資料庫信息到ElasticSearch
關於一次Oracle資料庫DMP文件導入的記述
Python Flask,資料庫,SQLAlchemy,模型類的定義,資料庫添加
Linux 環境下 PHP 如何獲取 Access 資料庫數據
SqlServer和Access資料庫開發VB程序的異同
通過 Docker 實現在 Linux 容器中運行 Microsoft SQL Server 資料庫
KooTeam新版本發布,增加 Mysql 資料庫支持
python後台架構Django——連接讀寫mysql資料庫
python與 masql資料庫,從基本開始做起
mysql資料庫:查詢,刪除,插入
這是一個漏洞!錯誤的資料庫腳本使Salesforce陷入困境
sqlite-utils:用於構建SQLite資料庫的Python庫和命令行工具
Ubuntu 更改 MySQL 資料庫數據存儲目錄
springboot項目中使用原生jdbc連接MySQL資料庫
Excel中的資料庫函數
Nokia 7 Plus跑分現身Geekbench資料庫
大數據Hadoop和Spark哪個更重要一些?內含學習資料