read excel sheet cell values

i am using .net frame work 2.0

i am using following code for read excel worksheet.  i want to do couple of changes in this code.


1. open the existing excel sheet in the given path (below code is creating new sheet . i do not want to create new sheet )
2.  Read all the values in the sheet and assign the values to the dataTable  

Let me know how to do this ?



private void button2_Click(object sender, EventArgs e)
        {
            Excel.Application excelApp = new Excel.Application();  // Creates a new Excel Application
            excelApp.Visible = true;  // Makes Excel visible to the user.

            // The following line if uncommented adds a new workbook
            //Excel.Workbook newWorkbook = excelApp.Workbooks.Add();

            // The following code opens an existing workbook
            string workbookPath = "c:/temp/SomeWorkBook.xlsx";  // Add your own path here

            Excel.Workbook excelWorkbook = null;

            try
            {
                excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0,
                    false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",true, 
                    false, 0,true, false, false);
            }
            catch
            {
                //Create a new workbook if the existing workbook failed to open.
                //excelWorkbook = excelApp.Workbooks.Add();
            }

            // The following gets the Worksheets collection
            Excel.Sheets excelSheets = excelWorkbook.Worksheets;

            // The following gets Sheet1 for editing
            string currentSheet = "Sheet1";
            Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);

            // The following gets cell A1 for editing
           // Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A1");
            string cell = (string)(excelWorksheet.Cells[1, 1] as Excel.Range).Text;
            // The following sets cell A1's value to "Hi There"
            //excelCell.Value2 = "Hi There";
        }

Open in new window

Varshini SAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try

        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        string workbookPath = "c:\temp\SomeWorkBook.xlsx";
        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open(workbookPath , Type.Missing, false, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        xlApp.Visible = true;
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        object value =  xlWorkSheet.UsedRange.Value;

Open in new window

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Varshini SAuthor Commented:
i do not want to open the excel sheet. i want to read behind the scenes.
0
Rgonzo1971Commented:
then

xlApp.Visible = false;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.