dsoframer打开Excel解决独占的方法

时间:2024-03-06 08:39:17

不要把控件直接拖到窗体上,而是使用代码New出来

private AxDSOFramer.AxFramerControl axf_Excel = new AxDSOFramer.AxFramerControl();

Thread thOpen ;
  private void ExcelCtrlEx_Load(object sender, EventArgs e)
        {
 thOpen = new Thread(new ThreadStart(FOpen));
            ((System.ComponentModel.ISupportInitialize)(this.axf_Excel)).BeginInit();
            axf_Excel.Dock = System.Windows.Forms.DockStyle.Fill;
            axf_Excel.Enabled = true;
            axf_Excel.Location = new System.Drawing.Point(0, 0);
            this.spc_Excel.Name = "spc_Excel";
           

            spc_Excel.Panel1.Controls.Add(axf_Excel);
            axf_Excel.Titlebar = false;
            axf_Excel.Menubar = false;
            axf_Excel.Toolbars = true;

            ((System.ComponentModel.ISupportInitialize)(this.axf_Excel)).EndInit();
            
            
            //启动现成加载EXCEL
            thOpen.Start();
}
 private void FOpen()
        {

            lock (axf_Excel)
            {
                try
                {
                    axf_Excel.Open(m_strPagePathName, false, "Excel.Sheet", "", "");
                    xBook = (Workbook)axf_Excel.ActiveDocument;
                   // xSheet = (xBook.Worksheets[1]);
                    xSheet = (Worksheet)xBook.ActiveSheet;
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
               
            }

          
        }
View Code

1、直接使用代码加载,加载的只能是UserControl,或是只要显示的FORM,如果要是在FORM中嵌套FROM,则比被嵌套的FORM就必须设置显示层次,且必须要设置能为非*,如果设置为非*,着也会出现外部EXCEL无法编辑的情况

2、加载的时候,使用异步线程加载,如果不使用,在打开多个时候,会有问题
3、在做保存等操作的时候,使用委托异步操作。例如:
 

MethodInvoker In = new MethodInvoker(SaveOnePage);
            this.BeginInvoke(In);
  private void SaveOnePage()
        {
              try
            {
                axf_Excel.Save(); 
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }