.net下载excel
string FileName = string.Format("批量发货单{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmssfff"), new Random().Next(9999));
string modelPath = httpContext.Server.MapPath("/") + ConfigurationManager.AppSettings["BatchDeliveryPath"].ToString();//批量发货模版
FileStream file = new FileStream(modelPath, FileMode.Open, FileAccess.Read);
HSSFWorkbook book = new HSSFWorkbook(file);
var sheet1 = book.GetSheet("Sheet1");
MemoryStream ms = new MemoryStream();//
for (var i = 0; i < Dt.Rows.Count; i++)
{
var row = sheet1.GetRow(i + 2);
row.CreateCell(0).SetCellValue(Dt.Rows[i]["NO"].ToString());
row.CreateCell(3).SetCellValue(Dt.Rows[i]["logistics_no"].ToString());
row.CreateCell(4).SetCellValue(Dt.Rows[i]["NAME"].ToString());
row.CreateCell(5).SetCellValue(Dt.Rows[i]["spec"].ToString());
row.CreateCell(6).SetCellValue(Dt.Rows[i]["create_time"].ToString());
row.CreateCell(7).SetCellValue(Dt.Rows[i]["email"].ToString() == Dt.Rows[i]["mobile"].ToString();
row.CreateCell(8).SetCellValue(Dt.Rows[i]["recipient"].ToString());
row.CreateCell(9).SetCellValue(Dt.Rows[i]["address"].ToString());
row.CreateCell(10).SetCellValue(Dt.Rows[i]["zip_code"].ToString());
row.CreateCell(11).SetCellValue(Dt.Rows[i]["phone"].ToString());
row.CreateCell(12).SetCellValue(Dt.Rows[i]["ID"].ToString());
row.CreateCell(13).SetCellValue(Dt.Rows[i]["product_id"].ToString());
}
book.Write(ms);
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=" + FileName + ".xls"));
Response.BinaryWrite(ms.ToArray());