Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have done windows application for Import The Excel Sheet In Html Format

I have Open Excel Sheet as
*****
C#
Microsoft.Office.Interop.Excel.Application excelApp;
Microsoft.Office.Interop.Excel.Workbook workbook;
Microsoft.Office.Interop.Excel.Worksheet worksheet;
Microsoft.Office.Interop.Excel.Range range;
excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Range Cell1;

workbook = excelApp.Workbooks.Open(FileName, Missing.Value, Missing.Value,
               Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
               Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets.get_Item(1);
                range = worksheet.UsedRange;

****


/* Operation on Excel sheet by Cell Wise*/


then close work book


C#
if (workbook!=null)
 workbook.Close(true, null, null);
 workbook = null;


its Work Fine For winows XP

But Gives Exception while Closing in windows 7
like
HTML
((Microsoft.Office.Interop.Excel.WorkbookClass)(workbook)).AutoUpdateSaveChanges' threw an exception of type 'System.Runtime.InteropServices.COMException'


If any one Know Issue Please Help me

Thanks in advance
Posted
Updated 14-Sep-11 22:02pm
v2

1 solution

I am doing it something like this. This is working for me in win 7. Have a look Please


C#
private void CreateExcellFile(string submittedFullPath)
        {
Ex.Application exelApp = null;
                        Ex.Workbook exelWorkBook = null;
                        Ex.Worksheet exelWorkSheet = null;
                        object misValue = System.Reflection.Missing.Value;

                        try
                        {
                            ////Create an Excell Application
                            exelApp = new Ex.ApplicationClass();

                            if (exelApp != null)
                            {
                                if (exelApp.Workbooks != null)
                                {
                                    ////Add an Workbook to the current Excell Application
                                    exelWorkBook = exelApp.Workbooks.Add(misValue);
                                }
                            }

                            if (exelWorkBook != null)
                            {
                                ////Getting Sheet1
                                exelWorkSheet = (Ex.Worksheet)exelWorkBook.Worksheets.get_Item(1);

                                if (exelWorkSheet != null)
                                {
                                    //// Naming the Table Data Column
                                    exelWorkSheet.Cells[1, 1] = "Title";
                                    exelWorkSheet.Cells[1, 2] = "FirstName";
                                    exelWorkSheet.Cells[1, 3] = "Surname";
                                    exelWorkSheet.Cells[1, 4] = "Email";
                                    exelWorkSheet.Cells[1, 5] = "TelePhoneNumber";
                                    exelWorkSheet.Cells[1, 6] = "OrderNumber";
                                    exelWorkSheet.Cells[1, 7] = "SubmissionDate";
                                }

                                ////Save the Excell file to its Specified location
                                exelWorkBook.SaveAs(submittedFullPath, Ex.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Ex.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                            }
                        }
                        catch
                        {
                        }
                        finally
                        {
                            if (exelWorkBook != null)
                            {
                                exelWorkBook.Close(true, misValue, misValue);
                            }

                            if (exelApp != null)
                            {
                                exelApp.Quit();
                            }
                        }

                        this.ReleaseObject(exelWorkSheet);
                        this.ReleaseObject(exelWorkBook);
                        this.ReleaseObject(exelApp);
}


Here Ex = Microsoft.Office.Interop.Excel;

C#
private void ReleaseObject(object submittedObj)
        {
            try
            {
                //// By force realeasing the Object if the Garbage Collector not doing it right now
                System.Runtime.InteropServices.Marshal.ReleaseComObject(submittedObj);
                submittedObj = null;
            }
            catch
            {
                submittedObj = null;
            }
            finally
            {
                GC.Collect();
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900