首页 > 代码库 > MVC 导出 简单范例合适初学者

MVC 导出 简单范例合适初学者

界面:

<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();         }