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

Source: Internet
Author: User
Tags ole

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

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.