.net下载excel

wxl19900320 2015-06-05 02:52:07
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());
...全文
139 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
takpod 2015-06-12
  • 打赏
  • 举报
回复
刚好也要~~~
huiyaosoft 2015-06-05
  • 打赏
  • 举报
回复
引用 2 楼 qq283426694 的回复:
什么情况都不说,就甩段代码,再看看结贴率,搞得好像必须帮一样,真是胃疼的存在
i疾风 2015-06-05
  • 打赏
  • 举报
回复
什么情况都不说,就甩段代码,再看看结贴率,搞得好像必须帮一样,真是胃疼的存在
於黾 2015-06-05
  • 打赏
  • 举报
回复
看看这个,完整例子 不要根据网上的只言片语拼凑代码

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧