乏mily

导航

在可以调用OLE之前,必须将当前线程设置为单线程单元(STA)模式,请确保您的Main函数带有STAThreadAttribute

导入导出功能,在调用ShowDialog时的错误,解决办法如下:

WinForm窗体的入口点:

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        private static void Main(String[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
       Application.Run(new LoginForm());
}

报错页面程序如下:

    public string importPath=""; //全局变量,用于显示导入文件路劲

private void btnImport_Click(object sender, EventArgs e) { Thread importThread = new Thread(new ThreadStart(ImportDialog)); importThread.SetApartmentState(ApartmentState.STA); //重点 importThread.Start(); txtImportPath.Text = importPath; } public void ImportDialog() { OpenFileDialog open = new OpenFileDialog(); open.Filter = "Excel文件|*.xls;*.xlsx"; if (open.ShowDialog() == DialogResult.OK) { importPath = open.FileName; ReadExcelToTable(importPath); UpdateArea(); } }

 

posted on 2015-06-01 12:07  乏mily  阅读(10041)  评论(2编辑  收藏  举报