Excel資料匯入___你hold住麼(一),資料匯入hold

來源:互聯網
上載者:User

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>

以上就是做匯入的基本流程,那是不是完好敲出代碼就能正常運行呢,請見小編下文分析

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.