Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to load and display the execl file on the page after asp.net uploads the file

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to load and display the execl file on the page after asp.net uploads the execl file". In the daily operation, it is believed that many people have doubts about how to load and display the execl file on the page after uploading the execl file by asp.net. The editor consulted all kinds of materials and sorted out a simple and useful operation method. I hope it will be helpful to answer the question of "how to load and display on the page after asp.net uploads the execl file"! Next, please follow the editor to study!

The copy code is as follows:

# 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 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', 'Please re-upload Execl file before operation', 'warning'); ") }} else {Label1.Text = "× file upload failed";}} else {Label1.Text = "× Please choose to upload file first";} ScriptManager.RegisterClientScriptBlock (Page, this.GetType (), "idAlert1", "closeLoad ()) ", true) } # endregion # region upload file / upload file / / upload file / / upload file type, such as .jpg | .gif | .bmp / limit the size of the uploaded file in k / upload path Need to use server.mappath / / 1 for automatic naming, 0 with the original name / 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 = "incorrect file type is uploaded!" ; return erorr;} Type = Type.Substring (Type.LastIndexOf ("."). ToUpper (); type = type.ToUpper (); if (type.IndexOf (Type) = =-1) {erorr = "incorrect file type uploaded!" ; 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;// returns an error message, no error returns null} # endregion. This is the end of the study on "how to load and display the execl file on the page after asp.net uploads the execl file". Hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report