asp.net mvc3 中从list导出EXCEL

public ActionResult ToExcel(string shopname, string buyerNick, string status, string rateStatus, string timeBegin, string endTime)        

{             OpenExcel(shopname,buyerNick,status,rateStatus,timeBegin,endTime);             return null;         }

        public void OpenExcel(string shopname, string buyerNick, string status, string rateStatus, string timeBegin, string endTime)       

  {                       

StringBuilder stringBuilder = new StringBuilder();            

stringBuilder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");          

stringBuilder.Append("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\n");            

stringBuilder.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\n");            

stringBuilder.Append("xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\n");            

stringBuilder.Append("xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n");            

stringBuilder.Append("<Worksheet ss:Name=\"Table1\">\n");            

stringBuilder.Append("<Table>\n");            

stringBuilder.Append("<Column ss:Index=\"1\" ss:AutoFitWidth=\"0\" ss:Width=\"110\"/>\n");            

stringBuilder.Append("<Row>\n");            

stringBuilder.Append("<Cell><Data ss:Type=\"String\">订单号</Data></Cell>\n");            

stringBuilder.Append("<Cell><Data ss:Type=\"String\">买家昵称</Data></Cell>\n");            

stringBuilder.Append("<Cell><Data ss:Type=\"String\">宝贝名称</Data></Cell>\n");            

stringBuilder.Append("<Cell><Data ss:Type=\"String\">宝贝图片</Data></Cell>\n");            

stringBuilder.Append("<Cell><Data ss:Type=\"String\">订单状态</Data></Cell>\n");            

stringBuilder.Append("<Cell><Data ss:Type=\"String\">创建时间</Data></Cell>\n");            

stringBuilder.Append("<Cell><Data ss:Type=\"String\">付款时间</Data></Cell>\n");            

stringBuilder.Append("<Cell><Data ss:Type=\"String\">确认时间</Data></Cell>\n");            

stringBuilder.Append("<Cell><Data ss:Type=\"String\">宝贝价格</Data></Cell>\n");            

stringBuilder.Append("<Cell><Data ss:Type=\"String\">购买数量</Data></Cell>\n");            

stringBuilder.Append("<Cell><Data ss:Type=\"String\">总金额</Data></Cell>\n");            

stringBuilder.Append("<Cell><Data ss:Type=\"String\">优惠</Data></Cell>\n");            

stringBuilder.Append("<Cell><Data ss:Type=\"String\">实际付款</Data></Cell>\n");            

stringBuilder.Append("<Cell><Data ss:Type=\"String\">接收电话</Data></Cell>\n");            

stringBuilder.Append("</Row>\n");            

List<Trade> tradelist = new List<Trade>();            

int toalpage = tolcount / pagecount;           

  for (int j = 1; j <= toalpage+1; j++)            

{                

tradelist =TaobaoTradesSoldGet(shopname, timeBegin, endTime, buyerNick, status, rateStatus, j);                

for (int i = 0; i < tradelist.Count; i++)                

{                    

stringBuilder.Append("<Row>\n");                    

stringBuilder.Append("<Cell><Data ss:Type=\"String\">" + tradelist[i].Tid + "</Data></Cell>\n");                     stringBuilder.Append("<Cell><Data ss:Type=\"String\">" + tradelist[i].BuyerNick + "</Data></Cell>\n");                     stringBuilder.Append("<Cell><Data ss:Type=\"String\">" + tradelist[i].Orders[0].Title + "</Data></Cell>\n");                     stringBuilder.Append("<Cell><Data ss:Type=\"String\">" + tradelist[i].Orders[0].PicPath + "</Data></Cell>\n");                     stringBuilder.Append("<Cell><Data ss:Type=\"String\">" + GetStatus(tradelist[i].Orders[0].Status) + "</Data></Cell>\n");                     stringBuilder.Append("<Cell><Data ss:Type=\"String\">" + tradelist[i].Created + "</Data></Cell>\n");                     stringBuilder.Append("<Cell><Data ss:Type=\"String\">" + tradelist[i].PayTime + "</Data></Cell>\n");                     stringBuilder.Append("<Cell><Data ss:Type=\"String\">" + tradelist[i].EndTime + "</Data></Cell>\n");                     stringBuilder.Append("<Cell><Data ss:Type=\"String\">" + tradelist[i].Orders[0].Price + "</Data></Cell>\n");                     stringBuilder.Append("<Cell><Data ss:Type=\"String\">" + tradelist[i].Orders[0].Num + "</Data></Cell>\n");                     stringBuilder.Append("<Cell><Data ss:Type=\"String\">" + (tradelist[i].Orders[0].Num * Convert.ToDouble(tradelist[i].Orders[0].Price)).ToString("0.00") + "</Data></Cell>\n");                     stringBuilder.Append("<Cell><Data ss:Type=\"String\">" + tradelist[i].Orders[0].DiscountFee + "</Data></Cell>\n");                     stringBuilder.Append("<Cell><Data ss:Type=\"String\">" + tradelist[i].Orders[0].Payment + "</Data></Cell>\n");                     stringBuilder.Append("<Cell><Data ss:Type=\"String\">" + tradelist[i].ReceiverAddress + "</Data></Cell>\n");                     stringBuilder.Append("</Row>\n");                 }             }                        stringBuilder.Append("</Table>\n");             stringBuilder.Append("</Worksheet>\n");             stringBuilder.Append("</Workbook>\n");             Response.Clear();             Response.AppendHeader("Content-Disposition", "attachment;filename=xueda" + System.DateTime.Now.ToString("_yyMMdd_hhmm") + ".xls");             Response.Charset = "gb2312";             Response.ContentType = "application/ms-excel";             Response.Write(stringBuilder.ToString());             Response.End();

        }

转载于:https://www.cnblogs.com/hbgdluck/archive/2013/02/28/2936960.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值