开发过程中难免会遇到读取Excel文件的情况,本文主要记录读取Excel文件的方法由于本文目的是供自己和各位童鞋日后学习参考,没有考虑过多方面,把所有方法及属性全部放到了一个文件中。
<div>
<%-- 文件上传控件 用于将要读取的文件上传 并通过此控件获取文件的信息--%>
<asp:FileUpload ID="fileUpload" runat="server" />
<%-- 点击此按钮执行读取方法--%>
<asp:Button ID="btnRead" runat="server" Text="ReadStart" />
</div>
//Page_Load事件 注册按钮单击事件
protected void Page_Load(object sender, EventArgs e)
{
this.btnRead.Click += new EventHandler(btnRead_Click);
}
//按钮单击事件
protected void btnRead_Click(object sender, EventArgs e)
{
string currFilePath = string.Empty; //待读取文件的全路径
string currFileExtension = string.Empty; //文件的扩展名
HttpPostedFile file = this.fileUpload.PostedFile;
string fileName = file.FileName;
string tempPath = System.IO.Path.GetTempPath();//获取系统临时文件路径
fileName = System.IO.Path.GetFileName(fileName); //获取文件名(不带路径)
this.currFileExtension = System.IO.Path.GetExtension(fileName); //获取文件的扩展名
this.currFilePath = tempPath + fileName; //获取上传后的文件路径 记录到前面声明的全局变量
file.SaveAs(this.currFilePath); //上传
DataTable dt = new DataTable();