1. 程式人生 > >asp.net MVC 檔案流匯出Excel

asp.net MVC 檔案流匯出Excel

1 <form id="FormEsExcel" action="/**/ExportEsExcelData" method="post">
2   
3 </form>
  <div style="float: right; margin-top: 25px; margin-bottom: 5px;">
            <a class="btn120" href="#" onclick="ShowExportData()">匯出</a>
        </div>

 

 

  1
public FileResult ExportEsExcelData() 2 { 3 4 var sbHtml = new StringBuilder(); 5 sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0'>"); 6 sbHtml.Append("<tr>"); 7 var lstTitle = new
List<string> { "序列號1", "序列號1", "序列號1", "序列號1", "序列號1", "序列號1", "序列號1", "序列號1", "序列號1", "序列號1", "序列號1", "序列號1", "序列號1", "序列號1", "序列號1", "序列號1", "序列號1", "序列號1", "序列號1", "序列號1","序列號1" }; 8 foreach (var item in lstTitle) 9 { 10 sbHtml.AppendFormat("<td style='font-size: 14px;text-align:center;background-color: #DCE0E2; font-weight:bold;' height='25'>{0}</td>
", item); 11 } 12 sbHtml.Append("</tr>"); 13 try 14 { 15 ExportParamDTO param = new ExportParamDTO(); 16 string orderIs = SessionHelper.Get("orderids"); 17 List<Guid> orderlist = new List<Guid>(); 18 if (orderIs!=null) 19 { 20 string[] arr = orderIs.Split(',').ToArray(); 21 foreach (var item in arr) 22 { 23 orderlist.Add(Guid.Parse(item)); 24 } 25 } 26 param._orderIds = orderlist; 27 CommodityOrderFacade orderBP = new CommodityOrderFacade(); 28 List<ExportResultDTO> result = orderBP.ExportResult(param); 29 int i = 1, orderItemCount = 0, j = 0; 30 foreach (ExportResultDTO model in result) 31 { 32 orderItemCount = model.Products == null ? 0 : model.Products.Count; 33 j = 0; 34 foreach (ProductList product in model.Products) 35 { 36 if (model.State == 7 || model.StateAfterSales == 7) 37 { 38 model.ChannelShareMoney = 0; 39 } 40 sbHtml.Append("<tr>"); 41 if (orderItemCount > 1 && j == 0) 42 { 43 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;'>{1}</td>", orderItemCount, i); 44 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{1}</td>", orderItemCount, model.Code); 45 } 46 else if (orderItemCount == 1) 47 { 48 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", i); 49 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{0}</td>", model.Code); 50 } 51 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{0}</td>", model.AppName); 52 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{0}</td>", model.AppType); 53 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{0}</td>", product.ProductName); 54 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", product.ProductPric); 55 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", product.BuyNumber); 56 if (orderItemCount > 1 && j == 0) 57 { 58 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;'>{1}</td>", orderItemCount, model.OrdersTime); 59 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;'>{1}</td>", orderItemCount, model.PaymentTime); 60 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;'>{1}</td>", orderItemCount, model.PaymentType); 61 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;'>{1}</td>", orderItemCount, model.PracticalPayment); 62 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{1}</td>", orderItemCount, GetOrderStateDescription(model.State, model.SelfTakeFlag, model.StateAfterSales)); 63 if (model.State == 7 && model.RefundMoney + model.RefundScoreMoney >= 0) 64 { 65 if (model.RefundScoreMoney > 0) 66 { 67 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;'>{1}(含{2}元積分)</td>", orderItemCount, model.RefundMoney.Value + model.RefundScoreMoney, model.RefundScoreMoney); 68 } 69 else 70 { 71 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;'>{1}</td>", orderItemCount, model.RefundMoney.Value); 72 } 73 } 74 else 75 { 76 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;'>{1}</td>", orderItemCount, ""); 77 } 78 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{1}</td>", orderItemCount, model.Payer); 79 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{1}</td>", orderItemCount, model.Phone); 80 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{1}</td>", orderItemCount, model.ShippingAddress); 81 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;'>{1}</td>", orderItemCount, model.Freight); 82 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;'>{1}</td>", orderItemCount, model.GoldCoupon); 83 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;'>{1}</td>", orderItemCount, model.CouponValue); 84 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;'>{1}</td>", orderItemCount, model.JczfAmonut); 85 sbHtml.AppendFormat("<td rowspan='{0}' style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{1}</td>", orderItemCount, model.SpendYJBMoney); 86 87 } 88 else if (orderItemCount == 1) 89 { 90 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", model.OrdersTime); 91 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", model.PaymentTime); 92 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{0}</td>", model.PaymentType); 93 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", model.PracticalPayment); 94 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{0}</td>", GetOrderStateDescription(model.State, model.SelfTakeFlag, model.StateAfterSales)); 95 if (model.State == 7 && model.RefundMoney >= 0 || model.State == 3 && model.StateAfterSales == 7 && model.RefundMoney >= 0) 96 { 97 if (model.RefundScoreMoney > 0) 98 { 99 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}(含{1}元積分)</td>", model.RefundMoney.Value + model.RefundScoreMoney, model.RefundScoreMoney); 100 } 101 else 102 { 103 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", model.RefundMoney.Value); 104 } 105 } 106 else 107 { 108 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", ""); 109 } 110 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{0}</td>", model.Payer); 111 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{0}</td>", model.Phone); 112 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{0}</td>", model.ShippingAddress); 113 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", model.Freight); 114 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", model.GoldCoupon); 115 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", model.CouponValue); 116 // sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", model.SpendYJBMoney); 117 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", model.JczfAmonut); 118 119 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;vnd.ms-excel.numberformat:@'>{0}</td>", model.SpendYJBMoney); 120 121 } 122 sbHtml.Append("</tr>"); 123 124 j++; 125 } 126 i++; 127 } 128 } 129 catch (Exception ex) 130 { 131 132 } 133 sbHtml.Append("</table>"); 134 return File(System.Text.Encoding.UTF8.GetBytes(sbHtml.ToString()), "application/ms-excel", string.Format("dingdan{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"))); 135 }