MVC 匯出 簡單範例合適初學者

來源:互聯網
上載者:User

標籤:style   http   java   os   io   資料   for   ar   

介面:

<div id="tbl">                <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:‘icon-remove‘,plain:true" onclick="Delete()">刪除</a>                <a href="/ProjectInformation/toExcel" id="Excel" data-options="plain:true" class="easyui-linkbutton" iconcls="icon-print">匯出</a> </div>


後台:

/// <summary>        /// 匯出到EXCEL        /// </summary>        public void toExcel()        {            string content = Request.Cookies["getPinfoIdList"].Value.ToString();            List<int> idList = new List<int>();            string[] cont = content.Split(‘,‘);            for (int i = 0; i < cont.Length; i++)            {                if (String.IsNullOrEmpty(cont[i]))                {                    continue;                }                idList.Add(Convert.ToInt32(cont[i]));            }            //接收需要匯出的資料            List<ProjectInformation> list = new List<ProjectInformation>();            foreach (var id in idList)            {                ProjectInformation p = db.ProjectInformations.Find(id);                list.Add(p);            }            //命名匯出表格的StringBuilder變數            StringBuilder sHtml = new StringBuilder(string.Empty);            //列印表頭            sHtml.Append("<table border=\"1\" width=\"100%\">");            sHtml.Append("<tr height=\"40\"><td colspan=\"29    \" align=\"center\" style=‘font-size:24px‘><b>項目匯總分析" + "</b></td></tr>");            //列印列名            sHtml.Append("<tr height=\"20\" align=\"center\" ><td>項目編碼</td><td>行業</td><td>銷售大區</td><td>省份</td><td>項目名稱</td>" +                "<td>銷售人員</td><td>物資類別</td><td>投標型號</td>" +                "<td>投標包數</td><td>投標總金額(萬元)</td><td>中標金額(萬元)</td>" +                "<td>項目類型</td><td>招標單位</td><td>銷售模式</td>" +                "<td>標書接收日期</td><td>標書完成日期</td><td>截標日期</td>" +                "<td>開標日期</td><td>定標日期</td><td>T1公司名</td>" +                "<td>授權代表</td><td>技術:價格:商務</td><td>T1樣品情況描述</td>" +                "<td>備案狀態</td><td>投標協議書</td><td>技術確認表</td>" +                "<td>報價分析表</td><td>標書製作人</td><td>備忘</td>" +                "</tr>");            //迴圈讀取List集合             for (int i = 0; i < list.Count; i++)            {                sHtml.Append("<tr height=\"20\" align=\"left\"><td>" + list[i].itemCode_C + "</td><td>" + list[i].sys_hy_C + "</td><td>" + list[i].regionBelongs_C + "</td><td>" + list[i].province_C + "</td><td>" + list[i].projectName_C                    + "</td><td>" + list[i].salesEngineerName_C + "</td><td>" + list[i].tenderSuppliesCategory_C + "</td><td>" + list[i].sys_tbxh_C                    + "</td><td>" + list[i].ttNumber_N + "</td><td>" + list[i].tbzje_D + "</td><td>" + list[i].zbje_D                    + "</td><td>" + list[i].projectType_C + "</td><td>" + list[i].biddingUnit_C + "</td><td>" + list[i].xsModel_C                    + "</td><td>" + list[i].dateReceiptTenders_T + "</td><td>" + list[i].tenderCompletionDate_T + "</td><td>" + list[i].cutoffTenderDate_T                    + "</td><td>" + list[i].startTenderDate_T + "</td><td>" + list[i].dbrqDate_T + "</td><td>" + list[i].companyName_C                    + "</td><td>" + list[i].authorizedRepresentative_C + "</td><td>" + list[i].evaluationPrinciples_C + "</td><td>" + list[i].ypqkms_C                    + "</td><td>" + list[i].porStatus_C + "</td><td>" + list[i].tbxyBook_C + "</td><td>" + list[i].jsqrTable_C                    + "</td><td>" + list[i].bjfxTable_C + "</td><td>" + list[i].tenderProducer_C + "</td><td>" + list[i].bz_C                    + "</td></tr>");            }            //列印表尾            sHtml.Append("</table>");            //調用輸出Excel表的方法            HelperComm.ExportToExcel("application/ms-excel", "項目匯總分析.xls", sHtml.ToString());        }

 

 
公用類HelperComm

 
/// <summary>        /// 輸出EXCEL表的方法        /// </summary>        /// <param name="FileType"></param>        /// <param name="FileName"></param>        /// <param name="ExcelContent"></param>        public static void ExportToExcel(string FileType, string FileName, string ExcelContent)        {            System.Web.HttpContext.Current.Response.Charset = "UTF-8";            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());            System.Web.HttpContext.Current.Response.ContentType = FileType;            System.IO.StringWriter tw = new System.IO.StringWriter();            System.Web.HttpContext.Current.Response.Output.Write(ExcelContent.ToString());            System.Web.HttpContext.Current.Response.Flush();            System.Web.HttpContext.Current.Response.End();        }

 
/// <summary>        /// 匯出excel模板        /// </summary>        /// <param name="model"></param>        public static void excelModel(string FileUrl, string FileName)         {            String FullFileName = System.Web.HttpContext.Current.Server.MapPath(FileUrl);            FileInfo DownloadFile = new FileInfo(FullFileName);            System.Web.HttpContext.Current.Response.Clear();            System.Web.HttpContext.Current.Response.ClearHeaders();            System.Web.HttpContext.Current.Response.Buffer = false;            System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));            System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());            System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);            System.Web.HttpContext.Current.Response.Flush();            System.Web.HttpContext.Current.Response.End();         }

聯繫我們

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