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