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.