Recently made a project with MVC3, publish the project in the Data import function (Excel format, there is a fixed import data template) Incredibly can not be used to view the error log, found to be "System.InvalidOperationException: The "microsoft.ace.oledb.12.0" provider is not registered on the local computer... ". Find some information on the internet to solve the problem. As shown in.
Background function code: Import and export implementation code.
1 #region-Write excel-using IO2 /// <summary>3 ///writing to excel using IO4 /// </summary>5 /// <param name= "table" >Table</param>6 /// <param name= "file" >Save Path</param>7 Public Static BOOLDatatabletocsv (System.Data.DataTable table,stringfile)8 {9 stringtitle ="";Ten Try One { AFileStream fs =NewFileStream (file, filemode.openorcreate); - - //FileStream FS1 = File.Open (File, FileMode.Open, FileAccess.Read); the -StreamWriter SW =NewStreamWriter (NewBufferedStream (FS), System.Text.Encoding.Default); - - for(inti =0; I < table. Columns.count; i++) + { - +Title + = table. Columns[i]. ColumnName +"\ t";//fields: Automatically jumps to the next cell A at } - -title = title. Substring (0, title. Length-1) +"\ n"; - - SW. Write (title); - in foreach(System.Data.DataRow rowinchtable. Rows) - { to stringline =""; + for(inti =0; I < table. Columns.count; i++) - { theLine + = Row[i]. ToString (). Trim () +"\ t";//content: Automatically jumps to the next cell * } $line = line. Substring (0, line. Length-1) +"\ n";Panax Notoginseng SW. Write (line); - } the SW. Close (); + FS. Close (); A return true; the + } - Catch(Exception) $ { $ return false; - } - } the - #endregionWuyi the #region-method to read the Excel file in CSV format- - ///<summary> Wu ///ways to read Excel files in CSV format - ///</ummary> About ///<param name= "path" >full path of Excel to be read</param> $ ///<returns></returns> - Public StaticDataTable Readexcelwithstream (stringpath) - { -DataTable dt =NewDataTable (); A BOOLIsdthascolumn =false;//Mark whether the DataTable has generated columns +StreamReader reader =NewStreamReader (path, System.Text.Encoding.Default);//Data Flow the while(!Reader. Endofstream) - { $ stringMeaage =Reader. ReadLine (); the string[] Splitresult = Meaage. Split (New Char[] {','}, Stringsplitoptions.none);//reads a row with a comma-delimited array theDataRow row =dt. NewRow (); the for(inti =0; i < splitresult.length; i++) the { - if(!isdthascolumn)//If you have not yet generated a column in { theDt. Columns.Add ("column"+ I,typeof(string)); the } AboutRow[i] =Splitresult[i]; the } theDt. Rows.Add (row);//Adding Rows theIsdthascolumn =true;//columns are no longer generated when the first row is read and a row is marked after the existing column is read + } - returnDT; the }Bayi #endregion the the #region-method to read the Excel file in xls\xlsx format- - ///<summary> - ///ways to read an Excel file in xls\xlsx format the ///</ummary> the ///<param name= "path" >full path of Excel to be read</param> the ///<returns></returns> the Public StaticDataTable readexceltotable (stringpath) - { theDataSet ds =NewDataSet (); the //if hdr=yes,datatable default column name is Excel first row of data ... the //if the hdr=no,datatable default column name is F1,f2,f3 ...94 stringstrconn ="Provider=microsoft.ace.oledb.12.0;data source="+ Path +"; Extended properties= ' Excel 12.0; Hdr=yes;imex=1 ';";//Office 07 and later cannot have extra spaces and semicolons note the using(OleDbConnection conn =NewOleDbConnection (strconn)) the { the Conn. Open ();98DataTable sheetsname = conn. GetOleDbSchemaTable (OleDbSchemaGuid.Tables,New Object[] {NULL,NULL,NULL,"Table"});//get all the names of sheet About stringFirstsheetname = sheetsname.rows[0][2]. ToString ();//get the name of the first sheet. - stringStrexcel =string. Format ("SELECT * from [{0}]", firstsheetname);101OleDbDataAdapter adapter =NewOleDbDataAdapter (Strexcel, strconn);102 adapter. Fill (DS);103 Conn. Close ();104 } the returnDs. tables[0];106 }107 #endregion
When publishing, the server's PC must be installed on or above the Office version. Not importing data will fail!
System.InvalidOperationException: "microsoft.ace.oledb.12.0" Provider is not registered on the local computer.