yamid

yamid

  • NA
  • 98
  • 0

Re:Bechir: How to convert a value obtained by excel cells into a double (c#)

Feb 23 2009 5:22 PM
Hi
Thanks to Bechir, here we can load an excel file and access to the cell valus, but the problem is that how to use these values as double.
I want to access all of the cells in different worksheets and and do some mathematical operations like add the cell values to each other ( while they are obj).
thanks alot 
 
 
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.Office.Interop.Excel;

namespace ConsoleApplication1
{
    class Program
    {

        static ApplicationClass App;
        static  Workbooks oBooks;
        static  Workbook oBook;
        static  Worksheet oSheet;

        static void Main(string[] args)
        {
            App = null;
            oBooks = null;
            oBook = null;
            oSheet = null;
            Console.WriteLine("Enter the Excel file path");
            string filepath = Console.ReadLine();
            App = new ApplicationClass();
            App.Visible = true;
            oBooks = App.Workbooks;
            //Open a new workbook
            oBook = oBooks.Open(filepath,
                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);

            oSheet = (Worksheet)App.ActiveSheet;
            /* After that you can get your cells either by
             * range */
            Range myRange = oSheet.get_Range("A1", "C3");
            //Or by cell
            oSheet.Cells.Cells[1, 1] = 2;
            
            //You can  get or create another sheet and transmit the content
            if (App.Worksheets[2] != null)
            {
                Worksheet oSheet2 = App.Worksheets[2] as Worksheet;
                oSheet2.Cells.Cells[1, 1] = oSheet.Cells.Cells[1, 1];
            }
            oBook.Save();
            App.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(App);
            App = null;
        }
    }
}


Answers (2)