asp.net上傳execl文件后,在頁面上加載顯示(示例代碼)
#region 上傳Execl文件
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string NewFileName = string.Empty;
string ErrorMess = UpLoadFile(FileUpload1, ".xls|.xlsx", 1024 * 5, Server.MapPath("/Report/SocialApply/"), 1, out NewFileName);
if (string.IsNullOrEmpty(ErrorMess))
{
Label1.Text = "√文件上傳成功";
ViewState["UpLoadFile"] = "/Report/SocialApply/" + NewFileName;
try
{
FileStream file = new FileStream
(Server.MapPath(ViewState["UpLoadFile"] as string),
FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);
DataTable dt1 = FairHR.Util.XmlExcelReport.ReadExcelToDataTable(file, 0, 0);
ViewState.Add("ViewDT", dt1);
GridView1.DataSource = ViewState["ViewDT"] as DataTable;
GridView1.DataBind();
file.Close();
//Maticsoft.Common.MessageBox.ResponseScript(Page, "$.messager.alert('系統(tǒng)提示', '操作成功!', 'info');");
}
catch
{
Maticsoft.Common.MessageBox.ResponseScript(Page, "$.messager.alert('系統(tǒng)提示', '請重新上傳Execl文件再操作', 'warning');");
}
}
else
{
Label1.Text = "×文件上傳失敗";
}
}
else
{
Label1.Text = "×請先選擇上傳文件";
}
ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "idAlert1", "closeLoad();", true);
}
#endregion
#region 上傳文件
/// <summary>
/// 上傳文件
/// </summary>
/// <param name="fu">上傳文件</param>
/// <param name="type">上傳文件類型,例如.jpg|.gif|.bmp</param>
/// <param name="size">限制上傳文件大小,單位為k</param>
/// <param name="path">上傳路徑,需使用server.mappath</param>
/// <param name="nametype">1為自動命名,0用原名</param>
/// <returns></returns>
public static string UpLoadFile(FileUpload fu, string type, int size, string path, int nametype, out string newFileName)
{
newFileName = null;
string erorr = null;
int Size = fu.PostedFile.ContentLength / 1024;
if (Size > size)
{
erorr = "上傳文件太大!";
return erorr;
}
string Type = fu.FileName;
if (Type.IndexOf(".") == -1) { erorr = "上傳文件類型有誤!"; return erorr; }
Type = Type.Substring(Type.LastIndexOf(".")).ToUpper();
type = type.ToUpper();
if (type.IndexOf(Type) == -1) { erorr = "上傳文件類型有誤!"; return erorr; }
string filename = "";
if (nametype == 1)
{
string nowdate = DateTime.Now.ToString();
nowdate = nowdate.Replace(":", "").Replace(" ", "").Replace("-", "").Trim();
Random r = new Random();
int a = r.Next(1000);
filename = nowdate + a.ToString() + Type;
newFileName = filename;
}
else
{
filename = fu.FileName;
if (System.IO.File.Exists(path + filename)) { erorr = "此文件名已經(jīng)存在!"; return erorr; }
}
fu.SaveAs(path + filename);
return erorr;//返回有錯的錯誤信息,沒有錯誤返回null
}
#endregion
相關(guān)文章
asp.net生成高質(zhì)量縮略圖通用函數(shù)(c#代碼),支持多種生成方式
這兩天正在研究報表中餅圖的繪圖方法,文章中的某些做法值得參考.2008-08-08
ASP.NET數(shù)據(jù)庫編程之Access連接失敗
ASP.NET數(shù)據(jù)庫編程之Access連接失敗...2006-09-09
.Net之微信小程序獲取用戶UnionID的實現(xiàn)
這篇文章主要介紹了.Net之微信小程序獲取用戶UnionID的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
在jquery repeater中添加設(shè)置日期,下拉,復(fù)選框等控件
JQueryElement 更新到了 3.5.1, 今天給大家主要講下如何在 Repeater 的模板中添加設(shè)置一些控件.2011-10-10
asp.net基于HashTable實現(xiàn)購物車的方法
這篇文章主要介紹了asp.net基于HashTable實現(xiàn)購物車的方法,涉及asp.net中HashTable結(jié)合session實現(xiàn)購物車功能的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12
Asp.Net 網(wǎng)站優(yōu)化系列之?dāng)?shù)據(jù)庫優(yōu)化措施 使用主從庫(全)
網(wǎng)站規(guī)模到了一定程度之后,該分的也分了,該優(yōu)化的也做了優(yōu)化,但是還是不能滿足業(yè)務(wù)上對性能的要求;這時候我們可以考慮使用主從庫。2010-06-06
asp.net通過消息隊列處理高并發(fā)請求(以搶小米手機為例)
這篇文章主要介紹了asp.net通過消息隊列處理高并發(fā)請求(以搶小米手機為例),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Asp.net ajax實現(xiàn)任務(wù)提示頁面的簡單代碼
這篇文章介紹了Asp.net ajax實現(xiàn)任務(wù)提示頁面的簡單代碼,有需要的朋友可以參考一下2013-11-11

