Link to home
Start Free TrialLog in
Avatar of arunbh
arunbh

asked on

how refresh excel sheet without open the file.

how i can refresh excel sheet without opening the file by using the asp.net c# code..
this is my code but their have problume .


///=============================================

    string EFile = "BExcel.xls";
            string ESFile = @System.Windows.Forms.Application.StartupPath + "\\ExcelFile\\" + EFile;
            EXL = new Excel.Application();
            EXL.DisplayFullScreen = false;
            try
            {
                EXL.WindowState = Excel.XlWindowState.xlMaximized;
                 
         
            }
            catch
            {
                MessageBox.Show("Please resize your Excel window size");
            }
            System.Windows.Forms.Application.DoEvents();
            //Workbook book =EXL.Workbooks.Add(ESFile);
            Workbook book = EXL.Workbooks.Add(ESFile);
            System.Windows.Forms.Application.DoEvents();
         
            book.RefreshAll();            
            System.Windows.Forms.Application.DoEvents();
            System.Windows.Forms.Application.DoEvents();
         
            Worksheet sheet = (Worksheet)book.ActiveSheet;            
            System.Windows.Forms.Application.DoEvents();
            System.Windows.Forms.Application.DoEvents();
         //=============================================
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Where do you have a problem ?

            EXL = new Excel.Application();
EXL.Visible = false;
            EXL.DisplayFullScreen = false;
Avatar of arunbh
arunbh

ASKER

this code help open an Excel Sheet then system create temp excel sheet in refresh mood but that excel sheet have without (.xls). If i try more time then error produce.
Do you close your excel-application in your code or not ?
Avatar of arunbh

ASKER

when more time active an excel sheet then this error produce:---{ Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))}.
 task Manager Processes there have more EXCEL.EXE is running.
I use process that help kill the process after some time produce same Error.
I Use BExcel.xls file there have data relation between MS Access table
If I change in database then change the Excel File Data.
Worksheet sheet = (Worksheet)book.ActiveSheet;            
            System.Windows.Forms.Application.DoEvents();
            System.Windows.Forms.Application.DoEvents();

....

at the end of your procedure:
EXL.close
set book = nothing
set sheet = nothing
set EXL = nothing
Avatar of arunbh

ASKER

Hi
Dhaest :
I am also doing same but thats not sutisfy me
this is my code please give me any code for this solution
this very urgent me please give any solution

 private void RFExcel()
        {
         
            System.Diagnostics.Process[] ExcelPro = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process ProParent in ExcelPro)
            {              
                System.Diagnostics.Process[] ProName = System.Diagnostics.Process.GetProcessesByName(ProParent.ProcessName);
                foreach (System.Diagnostics.Process ProChild in ProName)
                {
                    try
                    {
                        DPValue = ProChild.ProcessName.ToString();        
                        if (DPValue.Equals("EXCEL"))
                        {
                            ProChild.Kill();
                            ProChild.Close();
                            ProChild.Dispose();
                        }
                    }
                    catch (Exception EX)
                    {
                        label1.Text = "Error!..." + EX.Message.ToString();
                    }
                }
            }
            StartExcelRefresh();
        }
        private void StartExcelRefresh()
        {
            string EFile = "BExcel.xls";
            string ESFile = @System.Windows.Forms.Application.StartupPath + "\\ExcelFile\\" + EFile;
            EXL = new Excel.Application();
            EXL.DisplayFullScreen = false;
            try
            {
                EXL.WindowState = Excel.XlWindowState.xlMaximized;
                //EXL.WindowState = Excel.XlWindowState.xlNormal;
                //EXL.Left = 0; EXL.Width = 770;
                //EXL.Top = 49.5; EXL.Height = 494;              
            }
            catch
            {
                MessageBox.Show("Please resize your Excel window size");
            }
            System.Windows.Forms.Application.DoEvents();
            Workbook book = EXL.Workbooks.Add(ESFile);
            System.Windows.Forms.Application.DoEvents();
            book.RefreshAll();            
            System.Windows.Forms.Application.DoEvents();
            System.Windows.Forms.Application.DoEvents();
            Worksheet sheet = (Worksheet)book.ActiveSheet;            
            System.Windows.Forms.Application.DoEvents();
            System.Windows.Forms.Application.DoEvents();            
            disposeExisObj();              
        }
        private void disposeExisObj()
        {
            try
            {
                if (EXL != null)
                {                    
                    //----------------
                    EXL.Visible = false;
                    EXL.DisplayAlerts = false;
                    EXL.Quit();
                    EXL = null;
                   EXL = Close();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                   
                }            
            }
            catch
            {
           
            }
        }
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arunbh

ASKER

Hi
Dhaest  
Thanks more to support me. I will try this code.  If any problems then i will contact to you.
Can you give some feedback if one of the answers was sufficient or do you still have any problems ?
Forced accept.

Computer101
EE Admin