Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
For Reading excel file i use this code.i have abc.xls file
when i import abc.xls file excelSheet.get_Range("A1", "A1").Text read junck value



i copy all content of abc.xls to new file pqr.xls it show correct in
excelSheet.get_Range("A1", "A1").Text
Whats the probelm i am not able to figure out issue.any idea?


C#
Microsoft.Office.Interop.Excel.Application excelApp = null;
            Microsoft.Office.Interop.Excel.Workbook excelWrkBk = null;
            Microsoft.Office.Interop.Excel.Worksheet excelSheet = null;
            Microsoft.Office.Interop.Excel.Range excelRange = null;
            try
            {
                excelApp = new Microsoft.Office.Interop.Excel.Application();

                excelWrkBk = excelApp.Workbooks.Open(SourceFileName,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, true);



                excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelWrkBk.Sheets[1];

                while (true)
                {


                    if (!Convert.ToString(excelSheet.get_Range("A1", "A1").Text).Contains("Sl No."))
                    {
                        excelRange = (Microsoft.Office.Interop.Excel.Range)excelSheet.get_Range("A1", "AG1");
                        excelRange.Delete(Microsoft.Office.Interop.Excel.XlDeleteShiftDirection.xlShiftUp);
                        
                    }
                    else
                    {
                        break;
                    }
                }
                excelWrkBk.Save();
                excelWrkBk.Close(true, true, Type.Missing);

               
               // excelWrkBk.Close(true, SourceFileName, false);
                return GetReadOtherExcelFile(SourceFileName);
            }
            catch
            {
                throw;
            }
            finally
            {
                excelRange = null;
                excelSheet = null;
                excelWrkBk = null;
                if (excelApp != null) excelApp.Quit();
                excelApp = null;
            }
Posted
Updated 21-Jan-14 2:35am
v2
Comments
MCY 21-Jan-14 5:02am    
As an advice, I say you should stay away from office interop implementations. Instead use free third party api's such as NPOI
ZurdoDev 21-Jan-14 7:34am    
What's your question? This is not clear.

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



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