Copy codeThe Code is as follows:
# Region upload the Execl File
Protected void button#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 Uploaded successfully ";
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 prompt ',' operation successful! ', 'Info ');");
}
Catch
{
Maticsoft. Common. MessageBox. ResponseScript (Page, "$. messager. alert ('System prompt ', 'upload the Execl file again and then operation', 'warning ');");
}
}
Else
{
Label1.Text = "X File Upload Failed ";
}
}
Else
{
Label1.Text = "× Select Upload file first ";
}
ScriptManager. RegisterClientScriptBlock (Page, this. GetType (), "idAlert1", "closeLoad ();", true );
}
# Endregion
# Region upload files
/// <Summary>
/// Upload a file
/// </Summary>
/// <Param name = "fu"> upload a file </param>
/// <Param name = "type"> upload a file, for example, .jpg |. gif |. bmp </param>
/// <Param name = "size"> restrict the size of the uploaded file, in the unit of k </param>
/// <Param name = "path"> upload path, which must use server. mappath </param>
/// <Param name = "nametype"> 1 is automatically named, and 0 is used as the original name </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 = "uploading files is too large! ";
Return erorr;
}
String Type = fu. FileName;
If (Type. IndexOf (".") =-1) {erorr = "the upload file Type is incorrect! "; Return erorr ;}
Type = Type. Substring (Type. LastIndexOf ("."). ToUpper ();
Type = type. ToUpper ();
If (type. IndexOf (Type) =-1) {erorr = "the upload file type is incorrect! "; 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. If no error is returned, null is returned.
}
# Endregion