MVC导出

来源:互联网 发布:发货打印软件 编辑:程序博客网 时间:2024/06/06 05:44

<a href="#" class="easyui-linkbutton" iconCls="icon-Export" plain="true" onclick="ExportExcel()">导出</a>


     //导出Excel
       function ExportExcel() {
           //if ($('#CarType').datagrid("getRows").length == 0) { return; } //是否有信息
           //var CarType = $("#CarTypeInfoFrom").JsonData();
           //var surl = "/BaseInfo/CarType/ExportCarType?CarType=" + CarType;
           //window.location.href = surl;


           $("#ExportExcel").show();
           $("#ExportExcel").dialog({
               title: "导出车型-(如导出的文件名为乱码,请修改为:车型信息.xls 即可)",
               width: 700,
               height: 300,
               draggable: true,
               resizable: false,
               modal: true,
               buttons:
                   [
                       {
                           text: '确定',
                           iconCls: 'icon-ok',
                           handler: function () {
                               //导出条件
                               var Condition = "sBrandName=" +escape($("#ExportExcel_sBrandName").val())
                                   + "&sFirmName=" + escape($("#ExportExcel_sFirmName").val())
                                   + "&sModelName=" + escape($("#ExportExcel_sModelName").val())
                                   + "&sDisplacementName=" + escape($("#ExportExcel_sDisplacementName").val())
                                   + "&TransmissionMmodelName=" + escape($("#ExportExcels_TransmissionMmodelName").val())
                                   + "&BiGuidedPrice=" + $("#ExportExcel_BiGuidedPrice").val()
                                   + "&EiGuidedPrice=" + $("#ExportExcel_EiGuidedPrice").val();
                               $("#aLinkExportExcel").attr("href", "/BaseInfo/CarType/ExportExcel?"+Condition);
                               document.getElementById("aLinkExportExcel").click();
                           }
                       },
                       {
                           text: '取消',
                           handler: function () {
                               $("#ExportExcel").dialog('close');
                           }
                       }
                   ]
           });


    }


这里必须要  $("#aLinkExportExcel").attr("href", "/BaseInfo/CarType/ExportExcel?"+Condition); 发送

        #region 导出Excel


       /// <summary>
       /// 批量导出
       /// </summary>
       /// <param name="str1">条件1</param>
       /// <param name="str2">条件2</param>
       /// <returns></returns>
       public FileResult ExportExcel(string sBrandName, string sFirmName, string sModelName, 
           string sDisplacementName, string TransmissionMmodelName, string BiGuidedPrice, string EiGuidedPrice)
       {
           sBrandName = Server.UrlDecode(sBrandName);
           sFirmName = Server.UrlDecode(sFirmName);
           sModelName = Server.UrlDecode(sModelName);
           sDisplacementName = Server.UrlDecode(sDisplacementName);
           TransmissionMmodelName = Server.UrlDecode(TransmissionMmodelName);
           BiGuidedPrice = Server.UrlDecode(BiGuidedPrice);
           EiGuidedPrice = Server.UrlDecode(EiGuidedPrice);
           //创建Excel文件的对象
           NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
           //添加一个sheet
           NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("车型信息");


           //获取list数据
           CarTypeBll bll = new CarTypeBll();
           dynamic listRainInfo = bll.GetCarTypeExport(sBrandName, sFirmName, sModelName, sDisplacementName, TransmissionMmodelName, BiGuidedPrice, EiGuidedPrice);
           //给sheet1添加第一行的头部标题
           NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
           row1.CreateCell(0).SetCellValue("首字母");
           row1.CreateCell(1).SetCellValue("品牌");
           row1.CreateCell(2).SetCellValue("厂商");
           row1.CreateCell(3).SetCellValue("型号");
           row1.CreateCell(4).SetCellValue("年代款型");
           row1.CreateCell(5).SetCellValue("发动机排量");
           row1.CreateCell(6).SetCellValue("变速箱");
           row1.CreateCell(7).SetCellValue("发动机类型");
           row1.CreateCell(8).SetCellValue("指导价(万)");
           //将数据逐步写入sheet1各个行
           //ID,sFirstLetter,sBrand,sFirm,sModel,iYearsStyle,sDisplacement,sTransmissionMmodel,sEngine,iGuidedPrice
           int i = 0;
           foreach (var item in listRainInfo)
           {
               NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
               rowtemp.CreateCell(0).SetCellValue(item.SFIRSTLETTER);
               rowtemp.CreateCell(1).SetCellValue(item.SBRAND);
               rowtemp.CreateCell(2).SetCellValue(item.SFIRM);
               rowtemp.CreateCell(3).SetCellValue(item.SMODEL);
               rowtemp.CreateCell(4).SetCellValue(item.IYEARSSTYLE);
               rowtemp.CreateCell(5).SetCellValue(item.SDISPLACEMENT);
               rowtemp.CreateCell(6).SetCellValue(item.STRANSMISSIONMMODEL);
               rowtemp.CreateCell(7).SetCellValue(item.SENGINE);
               rowtemp.CreateCell(8).SetCellValue(item.IGUIDEDPRICE.ToString());
               i++;
           }
           // 写入到客户端 
           System.IO.MemoryStream ms = new System.IO.MemoryStream();
           book.Write(ms);
           ms.Seek(0, SeekOrigin.Begin);
           return File(ms, "application/vnd.ms-excel", "车型信息.xls");
       }


        #endregion


0 0
原创粉丝点击