zoukankan      html  css  js  c++  java
  • 文件的下载

    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() + "	" + row["age"].ToString() + "
    ");
    }

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

    context.Response.ContentType = "application/ms-excel";
    context.Response.AddHeader("Content-Disposition", "attachment;filename=" +
        context.Server.UrlEncode("人员列表.xls"));
    IWorkbook workbook = new HSSFWorkbook();//new XSSFWorkbook();//xlsx
    ISheet sheet = workbook.CreateSheet("人员列表");
    DataTable dt = SQLHelper.ExecuteReader("select * from Users");
    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["username"]);
    
        ICell cell1 = excelRow.CreateCell(1);
        cell1.SetCellValue((int)dataRow["age"]);
    }
    workbook.Write(context.Response.OutputStream);
  • 相关阅读:
    HihoCoder#1052:基因工程
    HihoCoder第十周:后序遍历
    HihoCoder第九周 状态压缩 二 与POJ2411总结
    [百度之星]资格赛:IP聚合
    HihoCoder第八周:状态压缩 一
    HihoCoder#1051:补提交卡
    HihoCoder#1039:字符消除
    HihoCoder第七周:完全背包问题
    HihoCoder第六周:01背包问题
    杭电2502--月之数
  • 原文地址:https://www.cnblogs.com/genesis/p/4663958.html
Copyright © 2011-2022 走看看