下载的一些方法

1

execl的方法

  protected void btnSavr_Click(object sender, EventArgs e)
        {
            HSSFWorkbook workbook = new HSSFWorkbook();//创建一个工作本
                       //// HSSFSheet sheet = new HSSFSheet(workbook);  //通过工作本 创建一个工作页 1
          HSSFSheet sheet = workbook.CreateSheet("第一页");  //这种也可以创建一个工作页 1
         // HSSFRow row= sheet.CreateRow(0);    //通过页创建一个行对象  注意索引从0 开始
                       // HSSFCell cell=   row.CreateCell(0);  //通过行对象创建一个指定 的列对象   0 开始
                       // cell.SetCellValue("小明"); //为单元格对象设置值
                       //         // cell.SetCellValue(dgv.rows[0].cells[0].value.tostring());

                       // HSSFCell cell2 = row.CreateCell(1);     //创建第2列
          //             // cell.SetCellValue(20);//为第2列赋值
          //HSSFCell cell1 = row.CreateCell(0);
          #region 01 第一种创建行的方法
          //cell1.SetCellValue("张三");    // cell.SetCellValue(dgv.row[0].cells[0].value.tostring());

          //HSSFCell cell2 = row.CreateCell(1);
          //cell2.SetCellValue("20");


          //HSSFCell cll3 = row.CreateCell(3);
          //cell2.SetCellValue("男");   //创建三列发现规律 
          #endregion

          #region 第二种创建行的方法
          //row.CreateCell(1).SetCellValue("张三");
          //row.CreateCell(2).SetCellValue("20");
          //row.CreateCell(3).SetCellValue("男");  /////创建一个行
          #endregion

          //cell.SetCellValue(dgv.rows[0].cells[1].value.tostring());
          //cell.SetCellValue(dgv.rows[1].cells[2].value.tostring());
          //cell.SetCellValue(dgv.rows[2].cells[3].value.tostring());
          //cell.SetCellValue(dgv.rows[3].cells[4].value.tostring());
            //列头
            HSSFRow rowhead=  sheet.CreateRow(0);
            rowhead.CreateCell(0).SetCellValue("Id");
            rowhead.CreateCell(1).SetCellValue("编号");
            rowhead.CreateCell(2).SetCellValue("名称");
            rowhead.CreateCell(3).SetCellValue("房子");
            rowhead.CreateCell(4).SetCellValue("年纪");


           

            //遍历面板 遍历行
          for (int rowindex = 1; rowindex < 10; rowindex++)  //dgv.Rows.count    行加1  应为她是从0 开始
          {
              HSSFRow row = sheet.CreateRow(rowindex);
              //遍历列
              for (int cellindex = 0; cellindex < 5; cellindex++)    //列不用加 应为列是-1;  所以不用加
              {
                  // row.CreateCell(rowindex).SetCellValue(dgv.Rows[rowindex].cells[cellindex].value.tostring());     
              }

          }

              //创建一个文档流对象
              using (FileStream fs = new FileStream(@"d:1.xls", FileMode.Create))
              {
                  workbook.Write(fs); //将内存的文档对象写入到文档流中
              }


        }

table转成 execll  列子

//查处数据库中所有的表

string username = (Session["user"] as userLoginInfo).UserName;
string strwhere = "[user].username2=wallet.w_username and [user].username2=user2.username3 and [user].username2='" + username + "'";
string selectdate = "w_datetime,zname,username2,level_Id,recomPeo,Placement_id,w_ldSumMoney,w_zhituiSumMoney,w_pzSumMoney";//要查询的字段
int Counts = 0;//行数
if (txtStartDate.Text.Trim().Length > 0 && txtEndDate.Text.Trim().Length > 0)
{
string dt1 = txtStartDate.Text.Trim();
string dt2 = txtEndDate.Text.Trim();
strwhere = strwhere + " and w_datetime between '" + dt1 + "' and '" + dt2 + "'";

}
DataTable dt = bbbll.GetListPageStoreProcedure(1, 10000, "[user],wallet,user2", strwhere, "level_Id desc", selectdate, out Counts);

HSSFWorkbook workbook = new HSSFWorkbook();//创建工作表
HSSFSheet sheet= workbook.CreateSheet("第一页");//
HSSFRow rowhead = sheet.CreateRow(0);
rowhead.CreateCell(0).SetCellValue("时间");
rowhead.CreateCell(1).SetCellValue("会员姓名");
rowhead.CreateCell(2).SetCellValue("注册会员");
rowhead.CreateCell(3).SetCellValue("会员等级");
//rowhead.CreateCell(4).SetCellValue("经手人");
rowhead.CreateCell(4).SetCellValue("直推人");
rowhead.CreateCell(5).SetCellValue("安置人");
rowhead.CreateCell(6).SetCellValue("领导奖");
rowhead.CreateCell(7).SetCellValue("直推奖");
rowhead.CreateCell(8).SetCellValue("碰值奖");

for (int rowindex = 1; rowindex <= dt.Rows.Count; rowindex++)
{
HSSFRow row = sheet.CreateRow(rowindex);
for (int cellindex = 0; cellindex < 9; cellindex++)
{
row.CreateCell(cellindex).SetCellValue(dt.Rows[rowindex-1][cellindex].ToString());


}
}
//创建一个文档流对象

using (FileStream fs = new FileStream(@"d:奖金资料.xls", FileMode.Create))
{

workbook.Write(fs); //将内存的文档对象写入到文档流中
}
MessageBox.Show(this, "下载成功放在d盘目录下");

2  文本下载

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //attachment;让浏览器弹出下载对话框保存返回报文
            //filename=是默认文件名,如果文件名中有中文等需要使用UrlEncode编码
            string encodeFileName = HttpUtility.UrlEncode("过滤词.txt");
            context.Response.AddHeader("Content-Disposition",
                string.Format("attachment;filename="{0}"", encodeFileName));

            context.Response.ContentType = "text/plain";
            Bll.FilterWords bll = new Bll.FilterWords();
            IList<Model.FilterWords> list=  bll.list();
            foreach (Model.FilterWords modal in list)
            {
                string str;
                if (modal.IsMod)
                {
                    str = "{Mod}";  //   审核词
                }
                else if (modal.IsForbid)
                {
                    str="{BANNED}";   //    禁用词
                }
                else
                {
                    str=modal.ReplaceWord;
                }
                context.Response.Write(modal.WordPattern + "=" + str + " ");

            }
            

原文地址:https://www.cnblogs.com/cdaq/p/3426249.html