Import and export function, when calling ShowDialog error, the workaround is as follows:
Entry point for the WinForm form:
/// <summary> /// The main entry point for the application. /// </summary> [STAThread] privatestaticvoid Main (string[] args) { application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false);
Application.Run (New LoginForm ()); }
The error page program is as follows:
Public StringImportpath= ""; Global variable to display the import file path strength
Private voidBtnimport_click (Objectsender, EventArgs e) {Thread Importthread=NewThread (NewThreadStart (Importdialog)); Importthread.setapartmentstate (ApartmentState.STA); //Key Importthread.start (); Txtimportpath.text=Importpath; } Public voidImportdialog () {OpenFileDialog Open=NewOpenFileDialog (); Open. Filter="Excel file |*.xls;*.xlsx"; if(Open. ShowDialog () = =DialogResult.OK) {Importpath=Open. FileName; Readexceltotable (Importpath); Updatearea (); } }
Before you can call OLE, you must set the current thread to single-threaded apartment (STA) mode, and make sure that your main function has STAThreadAttribute