Excel資料匯入___你hold住麼(一),資料匯入hold
最近小編跟著團隊一起開發ITOO3.0高校雲平台項目,其中的收穫是不言而喻滴,在項目中有個匯入功能:匯入學生資訊;匯入班級資訊:匯入教學樓資訊等,在不知多少次的嘗試之下,成功實現功能。
架構分析
詳細解釋一下
- 加粗
-第一步:在MVC架構的Client端建立檔案夾
- 第二步:通過NPOI檔案流(具體的專業名稱不知道是叫啥,姑且稱檔案流)將儲存在Client的Excel檔案流傳到WCF架構的Server端
- 第三步:在Server端中,建立檔案夾儲存Excel模板的xml檔案
Server端中的XML檔案起到解析的作用,從而將Excel 中的資料插入Insert到資料表中
代碼設計
上傳Excel到Server
* Controller
private readonly IUploadFile upLoadService1 = ServiceFactory.GetUploadFileService();
#region ImportFlowBatch()+大量匯入流程-徐露-2015年7月8日10:39:02
///
/// 大量匯入流程
///
///
public ActionResult ImportFlowBatch()
{
#region 檔案驗證以及上傳到指定檔案夾 Client端
HttpPostedFileBase file = Request.Files[“files”];
string strFileName;
string strSavePath;
string ClientPath = AppDomain.CurrentDomain.BaseDirectory + “File\UpFile\”;
string strPaperId = “1”;
//這個是Client端的檔案儲存路徑
if (file == null || file.ContentLength <= 0) { ViewBag.error = "檔案不可為空"; return View(); } else { string strFilename = Path.GetFileName(file.FileName); int intFilesize = file.ContentLength;//擷取上傳檔案的大小單位為位元組byte string fileEx = System.IO.Path.GetExtension(strFilename);//擷取上傳檔案的副檔名 string strNoFileName = System.IO.Path.GetFileNameWithoutExtension(strFilename);//擷取無副檔名的檔案名稱 int Maxsize = 4000 * 1024;//定義上傳檔案的最大空間大小為4M string FileType = ".xls,.xlsx";//定義上傳檔案的類型字串 strFileName = strNoFileName + DateTime.Now.ToString("yyyyMMddhhmmss") + fileEx; if (!FileType.Contains(fileEx)) { ViewBag.error = "檔案類型不對,只能匯入xls和xlsx格式的檔案"; //return View(); } if (intFilesize >= Maxsize) { ViewBag.error = "上傳檔案超過4M,不能上傳"; //return View(); } strSavePath = Path.Combine(ClientPath, strFileName); file.SaveAs(strSavePath); } #endregion #region 將Client端上傳的檔案 上傳到Server端 FileUploadMessage myFileMessage = new FileUploadMessage(); string strDataFileName = file.FileName; myFileMessage.FileName = strDataFileName;//檔案名稱 string CientPathName = ClientPath + strFileName; using (FileStream fs = System.IO.File.OpenRead(CientPathName)) { myFileMessage.FileData = fs; try { upLoadService1.UploadFileMethod(myFileMessage); } catch { } //關閉流 fs.Close(); } #endregion string[] HeadName = { "流程ID", "流程名稱", "優先順序", "是否啟用(啟用1,未啟用0)", "流程Url", "時間戳記", "是否刪除" }; //調用執行 寫資料庫 if(upLoadService1.ServiceReadFile (strDataFileName ,strPaperId )==null) { return RedirectToAction("Index","ImportStudent"); } else { DataTable table = upLoadService1 .ServiceReadFile (strDataFileName ,strPaperId )[0]; return File (Export .ExportManager .ExportExcel (table,HeadName ),"application/vnd.ms-excel", "流程匯入錯誤清單" + ".xls"); } } #endregion
*在Service中讀取Excel檔案,寫入資料庫
需要加的三個:IUploadFile(介面)、UploadFile(實作類別)、以及ServiceFactory(工廠)
篇幅限制,具體代碼不再粘貼,小編會具體上傳Demo,另行下載
*XML配置
針對每一個匯入的Excel都需要一個相應的XML設定檔,以便底層方法能夠解析擷取
XML應統一放置在某一路徑下,具體路徑配置如下:
WebConfig.xml添加配置節點:
在下添加節點
即將設定檔存放路徑放在了Service層的Models/ImportConfigXML檔案夾下。
命名:各系統可根據具體情況具體命名,調用方法時需要傳入XML檔案名稱
具體XML配置可參考:
<?xml version="1.0" encoding="utf-8" ?><Excel name="匯入流程模板"> <Sheet name="流程" table="FreshFlowEntity" primaryKey="FlowID" pkType="guid"> <Column name="流程名稱" field="Name"> <DataType>string</DataType> <ForeignKey isExist="false"></ForeignKey> </Column> <Column name="優先順序" field="Sort" isNecessary="true"> <DataType>int</DataType> <ForeignKey isExist="false"></ForeignKey> </Column> <Column name="是否啟用(啟用1,未啟用0)" field="IsUse" isVerifyRepeat="false" > <DataType>int</DataType> <ForeignKey isExist="false"></ForeignKey> </Column> <Column name="流程Url" field="Url" isVerifyRepeat="false"> <DataType>string</DataType> <ForeignKey isExist="false"></ForeignKey> </Column> <Column name="時間戳記" field="TimeSpan" isNecessary="false" isVerifyRepeat="false" > <DataType>DateTime</DataType> <ForeignKey isExist="false"></ForeignKey> </Column> <Column name="是否刪除" field="IsDelete" isVerifyRepeat="false"> <DataType>int</DataType> <ForeignKey isExist="false"></ForeignKey> </Column> </Sheet></Excel>
以上就是做匯入的基本流程,那是不是完好敲出代碼就能正常運行呢,請見小編下文分析
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。