1.添加方法
/// <summary>
/// 上传方法/// </summary>
/// <param name="myFileUpload"></param>
/// <returns></returns>
private bool Upload(FileUpload myFileUpload)
{
bool flag = false; //是否允许上载
bool fileAllow = false; //设定允许上载的扩展文件名类型
string[] allowExtensions = { ".xls" }; //取得网站根目录路径
string path = HttpContext.Current.Request.MapPath("~/");
if (myFileUpload.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();
for (int i = 0; i < allowExtensions.Length; i++)
{
if (fileExtension == allowExtensions[i])
{ fileAllow = true; }
}
if (fileAllow)
{
try
{
//存储文件到文件夹
// myFileUpload.SaveAs(path + myFileUpload.FileName);
myFileUpload.SaveAs(path+"k.xls");
Response.Write("文件导入成功");
list_Client.Items.Clear();
flag = true;
}
catch (Exception ex)
{
}
}
else
{
Response.Write("不允许上载:" + myFileUpload.PostedFile.FileName + ",只能上传xls的文件,请检查!");
flag = false;
}
}
else
{
Response.Write("请选择要导入的excel文件!");
flag = false;
}
return flag;
}
2.上传事件
protected void Button4_Click(object sender, EventArgs e)
{
bool b = Upload(fuExcel); // 上传excel文件
Bind();
}
/**********************************************************html***********************************************************************/
3.静态页面:
<div><asp:FileUpload ID="fuExcel" runat="server" />
<asp:Button ID="Button4" runat="server" Text="导入数据" οnclick="Button4_Click" /></div>