asp.net 将查询的数据保存到服务器的临时文件
1 using System; 2 using System.Web; 3 using System.IO; 4 using System.Runtime.Serialization; 5 using System.Runtime.Serialization.Formatters.Binary; 6 7 namespace BLLManager 8 { 9 /// <summary> 10 ///setTemp 的摘要说明 11 /// </summary> 12 public class SetTempFile 13 { 14 public SetTempFile() 15 { 16 // 17 //TODO: 在此处添加构造函数逻辑 18 // 19 } 20 21 /// <summary> 22 /// 对象转换成二进制流并写入文件 23 /// </summary> 24 /// <param name="obj">写入对象(可以使string 或DataTable等数据集信息)</param> 25 /// <param name="fileName">临时文件文件名</param> 26 /// <returns></returns> 27 public static bool saveObjToFile(object obj, string fileName) 28 { 29 try 30 { 31 MemoryStream stream = new MemoryStream(); 32 IFormatter bf = new BinaryFormatter(); 33 bf.Serialize(stream, obj); 34 byte[] bytes = stream.GetBuffer(); 35 stream.Close(); 36 stream.Dispose(); 37 38 string filePath = HttpContext.Current.Server.MapPath("~/tempData/"); 39 40 // 如果文件夹不存在 41 if (!Directory.Exists(filePath)) 42 { 43 // 创建文件夹 44 Directory.CreateDirectory(filePath); 45 } 46 47 FileStream outputFile = new FileStream(filePath + fileName + ".tempdcccccc", FileMode.OpenOrCreate); 48 outputFile.Write(bytes, 0, bytes.Length); 49 outputFile.Flush(); 50 outputFile.Close(); 51 outputFile.Dispose(); 52 53 return true; 54 } 55 catch (Exception) 56 { 57 return false; 58 } 59 } 60 61 62 63 /// <summary> 64 /// 二进制流文件还原成对象 65 /// </summary> 66 /// <param name="fileName">文件名(键值)</param> 67 /// <returns></returns> 68 public static object readFileToObj(string fileName) 69 { 70 string filePath = HttpContext.Current.Server.MapPath("~/tempData/"); 71 // 如果文件夹不存在 72 if (!Directory.Exists(filePath)) 73 { 74 return "false"; 75 } 76 else 77 { 78 try 79 { 80 FileStream inputFile = new FileStream(filePath + fileName + ".tempdcccccc", FileMode.Open); 81 BinaryReader br = new BinaryReader(inputFile); 82 byte[] bytes = br.ReadBytes((int)br.BaseStream.Length); 83 84 MemoryStream stream = new MemoryStream(bytes); 85 IFormatter bf = new BinaryFormatter(); 86 Object reobj = bf.Deserialize(stream); 87 88 stream.Close(); 89 stream.Dispose(); 90 br.Close(); 91 br.Dispose(); 92 inputFile.Close(); 93 inputFile.Dispose(); 94 95 return reobj; 96 } 97 catch (Exception) 98 { 99 return "false"; 100 } 101 102 } 103 } 104 105 /// <summary> 106 /// 删除临时数据文件 107 /// </summary> 108 /// <param name="fileName">文件名</param> 109 /// <returns></returns> 110 public static bool delTempFile(string fileName) 111 { 112 try 113 { 114 string filePath = HttpContext.Current.Server.MapPath("~/tempData/"); 115 if (File.Exists(filePath)) 116 { 117 File.Delete(filePath + fileName + ".tempdcccccc"); 118 return true; 119 } 120 else 121 { 122 return true; 123 } 124 } 125 catch (Exception) 126 { 127 return false; 128 } 129 130 } 131 132 } 133 }
写入到服务器临时文件 调用方法:
1 // dt:存放的数据信息 2 // this.DCID.Value : 文件名
3 SetTempFile.saveObjToFile(dt, this.DCID.Value);
示例导出excel调用:
1 try 2 { 3 // 读取 临时文件信息 4 object temp = SetTempFile.readFileToObj(this.DCID.Value); 5 if (object.Equals(temp, (object)"false")) 6 { 7 this.lbltishi.Text = "要导出的文件未查询到,请重新查询"; 8 } 9 else 10 { 11 // 导出数据 12 // 需要强类型转换,如果是datatable需转换成datatable 13 new ExportExcel().DownloadAsExcel("application/ms-excel", "临时文件.xls", (string)temp); 14 } 15 } 16 catch 17 { 18 this.lbltishi.Text = "导出出现未知异常"; 19 }

浙公网安备 33010602011771号