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; } }