数据库导出xls

string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
List<Dictionary<string, object>> arr = new List<Dictionary<string, object>>();
arr = Bus.Bus.Select(@"select * from HAP_StorageInfoMiddle where HAP_SelUserCode='"+Session["code"]+"'");
System.IO.StringWriter sw = new System.IO.StringWriter();
sw.WriteLine("包裹单号 \t 编码 \t 代码 \t 仓库\t 仓位\t 重量\t 备注\t 操作人\t 操作时间");
foreach (var row in arr)
{
sw.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t", row["HAP_numbers"], row["HAP_RegClientIdentifying"], row["HAP_RegClientCode"], row["HAP_SelStorage"], row["HAP_SelStorageSite"], row["HAP_Weight"], row["HAP_Remark"], row["HAP_CreateUser"], row["HAP_CreateTime"]));
}
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();

posted @ 2013-03-21 11:03  落叶随心  阅读(116)  评论(0编辑  收藏  举报