标签:
1 using Console_Core.BLL; 2 using Console_Core.Model; 3 using NPOI.HSSF.UserModel; 4 using NPOI.SS.UserModel; 5 using System; 6 using System.Collections.Generic; 7 using System.Drawing; 8 using System.Drawing.Imaging; 9 using System.IO; 10 using System.Linq; 11 using System.Reflection; 12 using System.Web; 13 14 namespace Web_Cassini 15 { 16 /// <summary> 17 /// download1 的摘要说明 18 /// </summary> 19 public class download1 : IHttpHandler 20 { 21 22 public void ProcessRequest(HttpContext context) 23 { 24 context.Response.ContentType = "text/plain"; 25 MyORM_BLL myORM_BLL = new MyORM_BLL(); 26 27 #region 默认把当前输出的文件 以附加形式 动态下载 到指定文件中 28 //默认把当前输出的文件 以附加形式 动态下载 到指定文件中 --AddHeader把表头输出到输出流 29 context.Response.AddHeader("Content-Disposition", "attachment;filename=" + context.Server.UrlDecode("动态文件.txt") + ""); 30 List<object> list = myORM_BLL.SelectAllModel(typeof(TC_STUDENT)); 31 foreach (var obj in list) 32 { 33 TC_STUDENT tc = obj as TC_STUDENT; 34 context.Response.Write(tc.USERNAME + "\t" + tc.PASSWORD + "\t" + tc.GENDER + "\t" + tc.PROFESSION); 35 } 36 #endregion 37 38 #region 默认把当前Excel 动态的以附件形式 下载 39 context.Response.AddHeader("Content-Disposition", "attachment;filename=" + context.Server.UrlEncode("学生信息.xls")); 40 List<object> list = myORM_BLL.SelectAllModel(typeof(TC_STUDENT)); 41 //引用NPOI 42 //创建一个workbook 43 IWorkbook workbook = new HSSFWorkbook(); 44 //创建sheet 45 ISheet sheet = workbook.CreateSheet("学生信息"); 46 //遍历List 每一个实例创建一个row 47 for (int i = 0; i < list.Count; i++) 48 { 49 TC_STUDENT tc = list[i] as TC_STUDENT; 50 IRow row = sheet.CreateRow(i); 51 Type ty = tc.GetType(); 52 PropertyInfo[] props = ty.GetProperties(); 53 //遍历 实例中的列 灭一个列创建一个cell 54 for (int j = 0; j < props.Length; j++) 55 { 56 string propName = props[j].Name; 57 object propValue = props[j].GetValue(tc); 58 ICell cell = row.CreateCell(j); 59 cell.SetCellValue(propValue.ToString()); 60 } 61 } 62 //把workbook写入到输出流,并以附件形式下载 63 workbook.Write(context.Response.OutputStream); 64 #endregion 65 66 #region 动态的 附加形式 下载图片 67 //动态的 附加形式 下载图片 68 context.Response.AddHeader("Content-Disposition", "attachment;filename=" + context.Server.UrlEncode("泡妞证.jpg") + ""); 69 //获得name 70 string name = context.Request["name"]; 71 //画出图片 72 using (Bitmap map = new Bitmap(500, 500)) 73 using (Graphics g = Graphics.FromImage(map)) 74 using (Font font1 = new Font(System.Drawing.FontFamily.GenericSerif, 30)) 75 using (Font font2 = new Font(System.Drawing.FontFamily.GenericSerif, 15)) 76 { 77 g.DrawString(name, font1, Brushes.Red, 110, 66); 78 g.DrawString(name, font2, Brushes.Red, 302, 150); 79 80 map.Save(context.Response.OutputStream, ImageFormat.Jpeg); 81 } 82 #endregion 83 84 #region 通过提取码 动态下载图片 85 //获得提取码 86 string code = context.Request["code"]; 87 //验证 提取码 88 if (code != "1234") 89 { 90 context.Response.Write("提取码错误"); 91 return; 92 } 93 //获得图片路径 写入流 Copy到输出流 94 string path = context.Server.MapPath("~/img/我爱你一万年158_038.jpg"); 95 using (Stream stream = File.OpenRead(path)) 96 { 97 //html头标记:动态附加形式 下载图片 98 context.Response.AddHeader("Content-Disposition", "attachment;filename=" + context.Server.UrlEncode("我爱你一万年158_038.jpg") + ""); 99 stream.CopyTo(context.Response.OutputStream); 100 } 101 #endregion 102 } 103 104 public bool IsReusable 105 { 106 get 107 { 108 return false; 109 } 110 } 111 } 112 }
标签:
原文地址:http://www.cnblogs.com/adolphyang/p/4770352.html