首页 > 其他 > 详细

文件的下载

时间:2015-07-21 12:30:46      阅读:156      评论:0      收藏:0      [点我收藏+]

1.将数据库的数据保存到文本文件中:

context.Response.ContentType = "text/plain";
//增加另存为功能
//增加Content-Disposition是告诉浏览器,这个返回的内容是"附件形式",要给用户保存,filename是建议的文件名
context.Response.AddHeader("Content-Disposition", "attachment;filename=" + context.Server.UrlEncode("动态文件.txt"));
DataTable table = SQLHelper.ExecuteReader("select * from userinfo");
foreach (DataRow row in table.Rows)
{
    context.Response.Write(row["name"].ToString() + "\t" + row["age"].ToString() + "\r\n");
}

2.将数据库的数据保存到EXCEL中

context.Response.ContentType = "application/ms-excel";
//using(IWorkbook workbook = new XSSFWorkbook(); //new HSSFWorkbook())
context.Response.AddHeader("Content-Disposition", "attachment;filename=" +
    context.Server.UrlEncode("人员列表.xls"));
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet("人员列表");
DataTable dt=SQLHelper.ExecuteReader("select * from userinfo");
for (int i = 0; i < dt.Rows.Count; i++)
{
    IRow excelRow = sheet.CreateRow(i);
    DataRow dataRow=dt.Rows[i];
    ICell cell0 = excelRow.CreateCell(0);
    cell0.SetCellValue((string)dataRow["name"]);

    ICell cell1 = excelRow.CreateCell(1);
    cell1.SetCellValue((int)dataRow["age"]);
}
workbook.Write(context.Response.OutputStream);

 

文件的下载

原文:http://www.cnblogs.com/genesis/p/4663958.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!