ASP. NET reads the Excel content and displays it on the Interface

Source: Internet
Author: User

The client uploads a local Excel file to the server and displays it on the client.

 // Upload button  Protected void Btnup_click ( Object Sender, Eventargs E ){ Bool B = upload (fuexcel ); // Upload an Excel file  If (! B ){ Return ;} String Name = fuexcel. filename; String Filepath = server. mappath ( "~ /Upload /" ) + Name; Dataset DS = exceldatasource (filepath, excelsheetname (filepath) [0]. tostring (); gridview1.datasource = Ds; gridview1.databind ();} // File Upload Method  Private bool Upload ( Fileupload Myfileupload ){ Bool Flag = False ; // Whether upload is allowed  Bool Fileallow = False ; // Set the extended file name type that can be uploaded  String [] AllowExtensions = { ". Xls" }; // Obtain the website root directory path  String Path = Httpcontext . Current. Request. mappath ( "~ /Upload /" ); If (Myfileupload. hasfile ){ String Fileextension = system. Io. Path . Getextension (myfileupload. filename). tolower (); For ( Int I = 0; I <AllowExtensions. length; I ++ ){If (Fileextension = AllowExtensions [I]) {fileallow = True ;}} If (Fileallow ){ Try { // Store the file to a folder Myfileupload. saveas (path + myfileupload. filename); lblmes. Text = "File imported successfully" ; Flag = True ;} Catch ( Exception Ex) {lblmes. Text + = ex. Message; flag = False ;}} Else {Lblmes. Text = "Upload not allowed :" + Myfileupload. postedfile. filename + ", Only XLS files can be uploaded. Please check! "; Flag = False ;}} Else {Lblmes. Text = "Select the Excel file to import! " ; Flag = False ;} Return Flag ;}// This method exports data from Excel to dataset. The filepath is the absolute path of the Excel file, and the sheetname is the table name in the Excel file. 
 Public  Dataset Exceldatasource ( String Filepath, String Sheetname ){ String Strconn; strconn = "Provider = Microsoft. Jet. oledb.4.0; Data Source =" + Filepath + "; Extended properties = Excel 8.0 ;" ; Oledbconnection conn = New Oledbconnection (strconn); oledbdataadapter oada = New Oledbdataadapter ("Select * from [" + Sheetname + "]" , Strconn ); Dataset DS = New  Dataset (); Oada. Fill (DS); Conn. Close (); Return DS ;} // Obtain all sheetnames in Excel.  Public  Arraylist Excelsheetname ( String Filepath ){ Arraylist Al = New Arraylist (); String Strconn; strconn = "Provider = Microsoft. Jet. oledb.4.0; Data Source =" + Filepath + "; Extended properties = Excel 8.0 ;" ; Oledbconnection conn = New Oledbconnection (strconn); Conn. open (); Datatable Sheetnames = conn. getoledbschematable (system. Data. oledb. Oledbschemaguid . Tables, New Object [] { Null , Null ,Null , "Table" }); Conn. Close (); Foreach ( Datarow Dr In Sheetnames. Rows) {Al. Add (Dr [2]);} Return Al ;}
 
You can also specify the connection string parameters in the webconfig file:
 <  Connectionstrings  > <  Add  Name  = " Xls " Connectionstring  = " Provider = Microsoft. Jet. oledb.4.0; Data source1_sample1.xls; extended properties = Excel 8.0 " /> <  Add  Name  = " XLSX " Connectionstring  = " Provider = Microsoft. Ace. oledb.12.0; Data source1_sample.xlsx; extended properties = Excel 12.0 " /> </  Connectionstrings > 

Use

 
StringConnstring =Configurationmanager. Connectionstrings ["Xls"]. Connectionstring;

Pay attention to the file operation permissions in XP.

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.