Save in the specified directory according to the uploaded Excel file:
1 protected voidBtnupload_directclick (Objectsender, Directeventargs e)2 {3 stringFileName = This. Fileuploader.filename;4 stringExtension =path.getextension (fileName). ToUpper ();5 byte[] content = This. fileuploader.filebytes;6 if(Extension = =". XLS"|| extension==". XLSX")7 {8 if(! Directory.Exists (Server.MapPath ("~/fileupload/")))9 {TenDirectory.CreateDirectory (Server.MapPath ("~/fileupload/")); One } A Try - { - stringFilePath = Server.MapPath ("~/fileupload/") + DateTime.Now.ToString ("YYYYMMDDHHMMSS") +extension; the FileUploader.PostedFile.SaveAs (filePath); -DataSet ds =Excelhelper.loaddatafromexcel (filePath); -DataTable Tbdata = ds. tables[0]; - for(inti =0; i < TbData.Columns.Count; i++) + { -Tbdata.columns[i]. ColumnName =Excelhelper.getcolumnname (Tbdata.columns[i]. ColumnName); + } A //Storeuser.datasource = ds. Tables[0]; at //Storeuser.databind (); - } - Catch(Exception ex) - { - Throwex; - } in } - Else to { +X.messagebox.alert ("Tips","file format is incorrect"); - } the}View Code
Read the data in Excel into the dataset:
1 /// <summary>2 ///Load Excel Data3 /// </summary>4 /// <param name= "FilePath" >file path</param>5 /// <returns>Data Source</returns>6 Public StaticDataSet Loaddatafromexcel (stringFilePath)7 {8 stringConnectionString ="Provider=Microsoft.Jet.OLEDB.4.0;Data source="+ FilePath +"; Extended properties= ' Excel 8.0; Hdr=false;imex=1 '";9OleDbConnection conn =NewOleDbConnection (connectionString);Ten Conn. Open (); OneString sql ="SELECT * FROM [sheet1$]"; A -OleDbDataAdapter oledaexcel =NewOleDbDataAdapter (SQL, conn); -DataSet Oledsexcel =NewDataSet (); theOledaexcel.fill (Oledsexcel,"Sheet1"); - Conn. Close (); - - returnOledsexcel; +}View Code
Ext.net Importing Excel