ASP.NET中上传EXCEL文件一个比较快的方法
2006-02-08 12:14
1026 查看
public string SaveUploadExcel(HttpPostedFile hpf)//验证Excel文件的正确性
{
//检查文件格式各文件大小是否有效
if(hpf == null || hpf.ContentLength < 1 || System.IO.Path.GetExtension(hpf.FileName).ToLower() != ".xls")
{
throw new InnerException("文件上传失败或文件格式不正确。!");
}
//保存文件
try
{
string fileFullName = MakeFileName();
hpf.SaveAs(fileFullName);
return fileFullName;
}
catch//(Exception ex)
{
throw new InnerException("保存文件失败。");// + ex.Message);
}
}
public string MakeFileName()//生成唯一文件名的Excel文件
{
string filePath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) +"\\uploadFiles\\PlanExcel";
if(!Directory .Exists(filePath))
Directory.CreateDirectory(filePath);
string fileName = Convert.ToString(HttpContext.Current.Session["UserID"]) + "_"
+ DateTime.Now.ToString("yyyyMMddHHmmss")
+ ".xls";
return filePath + "\\" + fileName;
}
public DataTable ReadNoHeadExcelData(string fileFullName)//读取Excel文件最后返回一个DataTable,可以在返回DataTable后对这个DataTable进行检验!
{
string excelNoHeadConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\"";//连接字符串
OleDbConnection oleCn =new OleDbConnection();
try
{
string currConnStr = string.Format(excelNoHeadConnStr, fileFullName);//连接字符串
oleCn.ConnectionString = currConnStr;
DataTable dt = new DataTable();
oleCn.Open();
string excelBookName = oleCn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null).Rows[0][2].ToString();
if(excelBookName!=string.Empty && excelBookName.Trim()!="")
{
string currSqlStr = string.Format(excelSqlStr, excelBookName);//sql字符串
OleDbCommand oleCmd = new OleDbCommand(currSqlStr, oleCn);
OleDbDataAdapter oleDda = new OleDbDataAdapter(oleCmd);
oleDda.Fill(dt);
}
oleCn.Close();
return dt;
}
catch
{
string errorMsg="Excel文件格式不对,请尝试用excel打开该文件检查内容并另存一下,确保成为正确的excel文件,再重新上传";
System.Web.HttpContext.Current.Response.Write("<script>alert('"+ errorMsg +"')</script>");
oleCn.Close();
return null;
}
}
{
//检查文件格式各文件大小是否有效
if(hpf == null || hpf.ContentLength < 1 || System.IO.Path.GetExtension(hpf.FileName).ToLower() != ".xls")
{
throw new InnerException("文件上传失败或文件格式不正确。!");
}
//保存文件
try
{
string fileFullName = MakeFileName();
hpf.SaveAs(fileFullName);
return fileFullName;
}
catch//(Exception ex)
{
throw new InnerException("保存文件失败。");// + ex.Message);
}
}
public string MakeFileName()//生成唯一文件名的Excel文件
{
string filePath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) +"\\uploadFiles\\PlanExcel";
if(!Directory .Exists(filePath))
Directory.CreateDirectory(filePath);
string fileName = Convert.ToString(HttpContext.Current.Session["UserID"]) + "_"
+ DateTime.Now.ToString("yyyyMMddHHmmss")
+ ".xls";
return filePath + "\\" + fileName;
}
public DataTable ReadNoHeadExcelData(string fileFullName)//读取Excel文件最后返回一个DataTable,可以在返回DataTable后对这个DataTable进行检验!
{
string excelNoHeadConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\"";//连接字符串
OleDbConnection oleCn =new OleDbConnection();
try
{
string currConnStr = string.Format(excelNoHeadConnStr, fileFullName);//连接字符串
oleCn.ConnectionString = currConnStr;
DataTable dt = new DataTable();
oleCn.Open();
string excelBookName = oleCn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null).Rows[0][2].ToString();
if(excelBookName!=string.Empty && excelBookName.Trim()!="")
{
string currSqlStr = string.Format(excelSqlStr, excelBookName);//sql字符串
OleDbCommand oleCmd = new OleDbCommand(currSqlStr, oleCn);
OleDbDataAdapter oleDda = new OleDbDataAdapter(oleCmd);
oleDda.Fill(dt);
}
oleCn.Close();
return dt;
}
catch
{
string errorMsg="Excel文件格式不对,请尝试用excel打开该文件检查内容并另存一下,确保成为正确的excel文件,再重新上传";
System.Web.HttpContext.Current.Response.Write("<script>alert('"+ errorMsg +"')</script>");
oleCn.Close();
return null;
}
}
相关文章推荐
- asp.net导出Excel文件之方法比较
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- ASP.NET 中将文件上传到另外一个服务器的方法
- ASP.NET 中将文件上传到另外一个服务器的方法二
- asp.net导出Excel文件之方法比较
- ASP.NET WebApi 上传文件时异常 Failed to execute send on XMLHttpRequest 的一个处理方法
- 自己封装的一个ASP.NET上传文件的方法
- asp.net中打开Excel上传文件,读取数据的方法
- asp.net导出Excel文件之方法比较
- asp.net导出Excel文件之方法比较
- ASP.NET 中将文件上传到另外一个服务器的方法二
- asp.net导出Excel文件之方法比较
- 朋友封装的一个ASP.NET上传文件的方法
- asp.net导出Excel文件之方法比较
- asp.net导出Excel文件之方法比较
- asp.net导出Excel文件之方法比较
- asp.net(c#)上传文件时检测文件类型方法小结
- ASP.NET文件上传,为每个用户建立一个上传目录
- ASP.NET中上传并读取Excel文件数据,附后生成EXCEL及杀掉EXCEL进程
- ASP.NET MVC上传文件的几种方法