MVC大量匯出資料方法

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   使用   ar   資料   

近段時間做了個資料平台,其中涉及到大量匯出CSV格式資料的業務,主要使用了部分視圖和視圖之間傳值等知識點,今天做了下整理,特此分享下:

主要分為四步:

1:要列印的資料格式陳列View;

2:自訂匯出表頭View呈現,在通過 @{Html.RenderAction(string actionName,object routeValues);}將步驟1的資料整合進來;

3:匯出頁面調用通用的匯出方法,通過傳參數,傳視圖名,擷取到要匯出的資料;

4:匯出成指定格式資料。

下面開始說明詳細匯出流程,為了便於理解,從匯出頁面開始:

匯出頁面,已經查詢到合格所有資料了,然後這是匯出按鈕響應的操作:點擊按鈕【匯出CSV】調用Export_DataInfo方法,如下所示

通用匯出方法 Export_Datainfo :

通用方法中的RenderPartialViewTostring和ExportCSV方法:

 -------------------------------------------------------------------------華麗分割線-------------------------------------------------------------

附件:

DayData_OutPutData:

@{    System.Text.StringBuilder sb = new System.Text.StringBuilder();    sb.Append("日期").Append(",").Append("列名1").Append(",,").Append("列名").Append(",,").Append("列名2").Append(",,")        .Append("列名3").Append(",,,,,,").Append("列名4").Append(",,,,,,,,").Append("列名5").Append(",,,,").Append("\n");    sb.Append(",").Append("列名6").Append(",").Append("列名7")        .Append(",").Append("列名8").Append(",").Append("列名9")        .Append(",").Append("列名10").Append(",").Append("列名11")        .Append(",").Append("列名12").Append(",").Append("列名13").Append(",").Append("列名14").Append(",").Append("列名15").Append(",").Append("列名16").Append(",").Append("列名17")        .Append(",").Append("列名18").Append(",").Append("列名19").Append(",").Append("列名20").Append(",").Append("列名21").Append(",").Append("列名22").Append(",").Append("列名23").Append(",").Append("列名24").Append(",").Append("列名25")        .Append(",").Append("列名26").Append(",").Append("列名27").Append(",").Append("列名28").Append(",").Append("列名29")        .Append("\n");            @sb.ToString()                }@{Html.RenderAction("OutPutData_DayData", new  {      gameId = (int)ViewData["gameId"],      startdate = (string)ViewData["startdate"],      enddate = (string)ViewData["enddate"],      pageNum = 1,      pageSize = 20,      types = (string)ViewData["types"],  });}
View Code

匯出公用方法:

        /// <summary>        /// 通用匯出方法        /// </summary>        /// <returns></returns>        [HttpPost]        public ActionResult Export_DataInfo(int gameId, string partialViewName, string startdate, string enddate, string fileName, string types, int dataType = 1)        {            Game game = gamebll.getGameById(gameId);            string text = "";            /* RenderPartialViewToString 將視圖內容轉化成string */            text = RenderPartialViewToString(this, partialViewName, gameId, startdate, enddate, dataType, types);            /* 將string匯出成指定格式 */            return ExportCSV(text, "application/ms-excel", game.Name + fileName);        }
View Code

匯出實現方法:

        /// <summary>        /// 將視圖內容轉換成string        /// </summary>        /// <returns></returns>        protected string RenderPartialViewToString(Controller controller,string partialViewName,int gameId,string startdate,string enddate,int dataType,string types)        {            IView view = ViewEngines.Engines.FindPartialView(controller.ControllerContext, partialViewName).View;                        ViewDataDictionary dicts =new ViewDataDictionary();            dicts.Add("gameId", gameId);            dicts.Add("startdate",startdate);            dicts.Add("enddate", enddate);            dicts.Add("dataType", dataType);//道具取前20位時,用來區別 日、周、月            dicts.Add("types", types);            using (System.IO.StringWriter writer = new System.IO.StringWriter())            {                ViewContext viewContext = new ViewContext(controller.ControllerContext, view, dicts, controller.TempData, writer);                viewContext.View.Render(viewContext, writer);                return writer.ToString();            }        }        /// <summary>        /// 匯出方法        /// </summary>        /// <returns></returns>        public ActionResult ExportCSV(string contentStr, string contentType, string fileName)        {            byte[] fileContents = System.Text.Encoding.Default.GetBytes(contentStr);            var fileStream = new System.IO.MemoryStream(fileContents);            return File(fileStream, contentType, System.Web.HttpContext.Current.Request.Browser.Browser.ToUpper() == "FIREFOX" ? fileName : Server.UrlEncode(fileName));        }
View Code

MVC大量匯出資料方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.