After asp.net uploads the execl file, it loads the display on the page (sample code)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.