ASP.NET文件操作帮助类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 | public class FileHepler { /// <summary> /// 上传一个文件 /// </summary> /// <param name="key"></param> /// <returns></returns> public static FileModel RequestFile( string key, bool isUsed = false ) { HttpPostedFile file = HttpContext.Current.Request.Files[key]; if (isUsed) { return GetFileModel1(file); } return GetFileModel(file); } public static FileModel SaveFile( string key, string saveUrl, bool isChange= false , bool isUsed = false ) { HttpPostedFile file = HttpContext.Current.Request.Files[key]; FileModel model = new FileModel(); try { if (isChange) { saveUrl = saveUrl + file.FileName; } else { if (Path.GetExtension(file.FileName) != Path.GetExtension(saveUrl)) { saveUrl = saveUrl.Replace(Path.GetExtension(saveUrl), Path.GetExtension(file.FileName)); } } string url = HttpContext.Current.Request.MapPath(saveUrl); file.SaveAs(url); if (isUsed) { model = GetFileModel1(file); } else { model = GetFileModel(file); } model.FileOldUrl = saveUrl; model.FileUrl = url; } catch (Exception ex) { model.Flag = false ; model.Result = ex.Message; } return model; } public static FileModel SaveFirstFile( string saveUrl, bool isChange = false , bool isUsed = false ) { HttpPostedFile file = HttpContext.Current.Request.Files[0]; FileModel model = new FileModel(); try { if (isChange) { saveUrl = saveUrl + file.FileName; } else { if (Path.GetExtension(file.FileName) != Path.GetExtension(saveUrl)) { saveUrl = saveUrl.Replace(Path.GetExtension(saveUrl), Path.GetExtension(file.FileName)); } } string url = HttpContext.Current.Request.MapPath(saveUrl); file.SaveAs(url); if (isUsed) { model = GetFileModel1(file); } else { model = GetFileModel(file); } model.FileOldUrl = saveUrl; model.FileUrl = url; } catch (Exception ex) { model.Flag = false ; model.Result = ex.Message; } return model; } public static FileModel SaveFile(HttpPostedFile _file, string saveUrl, bool isChange = false , bool isUsed = false ) { HttpPostedFile file = _file; FileModel model = new FileModel(); try { if (isChange) { saveUrl = saveUrl + file.FileName; } else { if (Path.GetExtension(file.FileName) != Path.GetExtension(saveUrl)) { saveUrl = saveUrl.Replace(Path.GetExtension(saveUrl), Path.GetExtension(file.FileName)); } } string url = HttpContext.Current.Request.MapPath(saveUrl); file.SaveAs(url); if (isUsed) { model = GetFileModel1(file); } else { model = GetFileModel(file); } model.FileOldUrl = saveUrl; model.FileUrl = url; } catch (Exception ex) { model.Flag = false ; model.Result = ex.Message; } return model; } public static List<FileModel> SaveFileList( int fileNum, string saveUrl, bool isChange = false , bool isUsed = false ) { List<FileModel> filelist = new List<FileModel>(); for ( int i = 0; i < fileNum; i++) { HttpPostedFile file = HttpContext.Current.Request.Files[i]; FileModel model = new FileModel(); try { if (isChange) { saveUrl = saveUrl + file.FileName; } else { if (Path.GetExtension(file.FileName) != Path.GetExtension(saveUrl)) { saveUrl = saveUrl.Replace(Path.GetExtension(saveUrl), Path.GetExtension(file.FileName)); } } string url = HttpContext.Current.Request.MapPath(saveUrl); file.SaveAs(url); if (isUsed) { model = GetFileModel1(file); } else { model = GetFileModel(file); } model.FileOldUrl = saveUrl; model.FileUrl = url; } catch (Exception ex) { model.Flag = false ; model.Result = ex.Message; } filelist.Add(model); } return filelist; } /// <summary> /// 上传多个文件,通过文件索引获取 /// </summary> /// <param name="fileNum"></param> /// <returns></returns> public static List<FileModel> RequestFileList( int fileNum, bool isUsed = false ) { List<FileModel> filelist = new List<FileModel>(); for ( int i = 0; i < fileNum; i++) { HttpPostedFile file = HttpContext.Current.Request.Files[i]; if (file != null ) { FileModel model = new FileModel(); if (isUsed) { model = GetFileModel1(file); } else { model = GetFileModel(file); } filelist.Add(model); } } return filelist; } public static List<FileModel> RequestFileList2( int fileNum, bool isUsed = false ) { List<FileModel> filelist = new List<FileModel>(); for ( int i = 0; i < fileNum; i++) { HttpPostedFile file = HttpContext.Current.Request.Files[i]; if (file != null ) { FileModel model = new FileModel(); if (isUsed) { model = GetFileModel1(file); } else { model = GetFileModel(file); } filelist.Add(model); } else { break ; } } return filelist; } public static FileModel RequestFirstFile( bool isUsed = false ) { FileModel model = new FileModel(); HttpPostedFile file = HttpContext.Current.Request.Files[0]; if (file != null ) { if (isUsed) { model = GetFileModel1(file); } else { model = GetFileModel(file); } } return model; } public static FileModel GetFileModel(HttpPostedFile file) { FileModel model = new FileModel(); model.FileName = file.FileName; model.FileSingleName = Path.GetFileNameWithoutExtension(file.FileName); model.FileExt = Path.GetExtension(file.FileName).Replace( "." , "" ); model.FileSize = file.ContentLength; model.FileStream = file.InputStream; model.NewFile = file; if (file == null ) { return null ; } int FileLen; FileLen = file.ContentLength; System.IO.Stream MyStream; byte [] bytes = new byte [FileLen]; MyStream = file.InputStream; MyStream.Read(bytes, 0, FileLen); MyStream.Close(); MyStream.Dispose(); model.FileByte = bytes; return model; } public static FileModel GetFileModel1(HttpPostedFile file) { FileModel model = new FileModel(); model.FileName = file.FileName; model.FileSingleName = Path.GetFileNameWithoutExtension(file.FileName); model.FileExt = Path.GetExtension(file.FileName).Replace( "." , "" ); model.FileSize = file.ContentLength; model.FileStream = file.InputStream; model.NewFile = file; if (file == null ) { return null ; } int FileLen; FileLen = file.ContentLength; System.IO.Stream MyStream; byte [] bytes = new byte [FileLen]; MyStream = file.InputStream; MyStream.Read(bytes, 0, FileLen); model.NewStream = MyStream; model.FileByte = bytes; return model; } public static DataTable GetExcelDatatable( string fileUrl) { //office2007之前 仅支持.xls //const string cmdText = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;IMEX=1';"; //支持.xls和.xlsx,即包括office2010等版本的 HDR=No代表第一行是标题,不是数据;Yes为数据 string cmdText = "Provider=Microsoft.Ace.OleDb.12.0;Data Source={0};Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'" ; // string cmdText = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;IMEX=1'"; DataTable dt = null ; //建立连接 OleDbConnection conn = new OleDbConnection( string .Format(cmdText, fileUrl)); //打开连接 if (conn.State == ConnectionState.Broken || conn.State == ConnectionState.Closed) { conn.Open(); } //获取所有的sheet的名称 即多个表格数据 DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null ); //获取Excel的第一个Sheet名称 string sheetName = schemaTable.Rows[0][ "TABLE_NAME" ].ToString().Trim(); //查询sheet中的数据 string strSql = "select * from [" + sheetName + "]" ; OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn); DataSet ds = new DataSet(); da.Fill(ds); dt = ds.Tables[0]; da.Dispose(); //释放资源 以免影响后面对excel操作 dt.Dispose(); ds.Dispose(); return dt; } public static DataTable GetExcelDatatable1( string fileUrl) { //office2007之前 仅支持.xls //const string cmdText = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;IMEX=1';"; //支持.xls和.xlsx,即包括office2010等版本的 HDR=No代表第一行是标题,不是数据;Yes为数据 string cmdText = "Provider=Microsoft.Ace.OleDb.12.0;Data Source={0};Extended Properties='Excel 12.0; HDR=No; IMEX=1'" ; // string cmdText = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;IMEX=1'"; DataTable dt = null ; //建立连接 OleDbConnection conn = new OleDbConnection( string .Format(cmdText, fileUrl)); //打开连接 if (conn.State == ConnectionState.Broken || conn.State == ConnectionState.Closed) { conn.Open(); } //获取所有的sheet的名称 即多个表格数据 DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null ); //获取Excel的第一个Sheet名称 string sheetName = schemaTable.Rows[0][ "TABLE_NAME" ].ToString().Trim(); //查询sheet中的数据 string strSql = "select * from [" + sheetName + "]" ; OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn); DataSet ds = new DataSet(); da.Fill(ds); dt = ds.Tables[0]; da.Dispose(); //释放资源 以免影响后面对excel操作 dt.Dispose(); ds.Dispose(); return dt; } public static DataSet GetExcelDataSet( string fileUrl) { //office2007之前 仅支持.xls //const string cmdText = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;IMEX=1';"; //支持.xls和.xlsx,即包括office2010等版本的 HDR=No代表第一行是标题,不是数据;Yes为数据 string cmdText = "Provider=Microsoft.Ace.OleDb.12.0;Data Source={0};Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'" ; // string cmdText = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;IMEX=1'"; DataSet fileds = null ; //建立连接 OleDbConnection conn = new OleDbConnection( string .Format(cmdText, fileUrl)); //打开连接 if (conn.State == ConnectionState.Broken || conn.State == ConnectionState.Closed) { conn.Open(); } //获取所有的sheet的名称 即多个表格数据 DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null ); for ( int i = 0; i < schemaTable.Rows.Count; i++) { //获取Excel的第一个Sheet名称 string sheetName = schemaTable.Rows[i][ "TABLE_NAME" ].ToString().Trim(); //查询sheet中的数据 string strSql = "select * from [" + sheetName + "]" ; OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn); DataSet ds = new DataSet(); da.Fill(ds); fileds.Tables.Add(ds.Tables[0]); da.Dispose(); //释放资源 以免影响后面对excel操作 fileds.Dispose(); ds.Dispose(); } return fileds; } public static DataSet GetExcelDataSet1( string fileUrl) { //office2007之前 仅支持.xls //const string cmdText = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;IMEX=1';"; //支持.xls和.xlsx,即包括office2010等版本的 HDR=No代表第一行是标题,不是数据;Yes为数据 string cmdText = "Provider=Microsoft.Ace.OleDb.12.0;Data Source={0};Extended Properties='Excel 12.0; HDR=No; IMEX=1'" ; // string cmdText = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;IMEX=1'"; DataSet fileds = null ; //建立连接 OleDbConnection conn = new OleDbConnection( string .Format(cmdText, fileUrl)); //打开连接 if (conn.State == ConnectionState.Broken || conn.State == ConnectionState.Closed) { conn.Open(); } //获取所有的sheet的名称 即多个表格数据 DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null ); for ( int i = 0; i < schemaTable.Rows.Count; i++) { //获取Excel的第一个Sheet名称 string sheetName = schemaTable.Rows[i][ "TABLE_NAME" ].ToString().Trim(); //查询sheet中的数据 string strSql = "select * from [" + sheetName + "]" ; OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn); DataSet ds = new DataSet(); da.Fill(ds); fileds.Tables.Add(ds.Tables[0]); da.Dispose(); //释放资源 以免影响后面对excel操作 fileds.Dispose(); ds.Dispose(); } return fileds; } } public class FileModel { public byte [] FileByte { get ; set ; } public string FileName { get ; set ; } public string FileSingleName { get ; set ; } public string FileExt { get ; set ; } public long FileSize { get ; set ; } public Stream FileStream { get ; set ; } public Stream NewStream { get ; set ; } public bool Flag { get ; set ; } = true ; public string Result { get ; set ; } public string FileUrl { get ; set ; } public string FileOldUrl { get ; set ; } public HttpPostedFile NewFile { get ; set ; } } |
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利
如有需要可联系(qq:2831804923 / 微信号:wh0612910)
----------------------------------------------------------------------------------------------------------------------
愿本文对阁下有帮助,在下谢之:
如有需要可联系(qq:2831804923 / 微信号:wh0612910)
----------------------------------------------------------------------------------------------------------------------
愿本文对阁下有帮助,在下谢之:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步