#region Upload execl file
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 = "√ File upload success";
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" (' System hint ', ' Operation successful!) ', ' info ');
}
Catch
{
Maticsoft.Common.MessageBox.ResponseScript (Page, "$.messager.alert" (' System hint ', ' please upload execl file again ', ' warning ');
}
}
Else
{
Label1.Text = "x file upload failed";
}
}
Else
{
Label1.Text = "x Please select upload file First";
}
Scriptmanager.registerclientscriptblock (Page, this.) GetType (), "IdAlert1", "closeload ();", true);
}
#endregion
#region Upload File
<summary>
Uploading files
</summary>
<param name= "Fu" > upload file </param>
<param name= "type" > upload file type, such as. Jpg|. Gif|. Bmp</param>
<param name= "size" > Limit upload file size, in units of k</param>
<param name= "path" > upload path, need to use server.mappath</param>
<param name= "NameType" >1 for automatic naming, 0 used formerly </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 = "Upload file is too big!" ";
return Erorr;
}
String Type = Fu. FileName;
if (Type.indexof (".") = = 1) {Erorr = "Upload file type is wrong!" "; return Erorr; }
Type = type.substring (Type.lastindexof (".")). ToUpper ();
Type = type. ToUpper ();
if (type. IndexOf (type) = = 1) {Erorr = "Upload file type is wrong!" "; 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 = "This file name already exists!" "; return Erorr; }
}
Fu. SaveAs (path + filename);
Return erorr;//error message returned with no error return null
}
#endregion