ASP.NET 读取本地Excel
在网页中选择本地Excel文件并获取文件内容,需要将该文件首先上传到服务器上,然后再根据该文件在服务器上的地址获取该文件的内容,代码如下 :
/// <summary>
/// 上传文件的根目录
/// </summary>
private string bootPath = HttpContext.Current.Request.PhysicalApplicationPath + "Files\\";
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (file_note.HasFile)
{
//判断文件是否小于10Mb
if (file_note.PostedFile.ContentLength < 10485760)
{
string fileForm = file_note.FileName.Split('.')[file_note.FileName.Split('.').Length - 1];
string filePath = bootPath + string.Format("{0:yyyyMMddHHmmss}", DateTime.Now) + "." + fileForm;
file_note.PostedFile.SaveAs(filePath);
DataSet a = CommonClassLib.ExcelHelper.ReturnDataSetFromExcelFile(filePath);
Response.Write("<script language:javascript>javascript:window.opener=null;window.close();</script>");
}
} }
/// 上传文件的根目录
/// </summary>
private string bootPath = HttpContext.Current.Request.PhysicalApplicationPath + "Files\\";
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (file_note.HasFile)
{
//判断文件是否小于10Mb
if (file_note.PostedFile.ContentLength < 10485760)
{
string fileForm = file_note.FileName.Split('.')[file_note.FileName.Split('.').Length - 1];
string filePath = bootPath + string.Format("{0:yyyyMMddHHmmss}", DateTime.Now) + "." + fileForm;
file_note.PostedFile.SaveAs(filePath);
DataSet a = CommonClassLib.ExcelHelper.ReturnDataSetFromExcelFile(filePath);
Response.Write("<script language:javascript>javascript:window.opener=null;window.close();</script>");
}
} }