Skip to content

How to read excel file with double values, into c#

yamid

Asked Feb 23, 2009, 9:10 AM EST3 answers
Share
LinkedInX

Who can please let me know:

How to read the excel cells having DOUBLE values into c# from worksheet1.Then  do some calculation and write it back into other excel worksheet2?

tnx

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    0
    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;
            }
        }
    }
    0
  • 0

    tnx for your reply but , I could not do some mathematical operation on the value of each cell, because they are obj. would you please tell me how to convert those cell values to Double?

     

    thanks

    0
  • Bechir Bejaoui
    0
    OK, I give you a solution but it is not the best one

     object myConvertingObject = oSheet2.Cells.Cells[row, col] as Range ;
     double myConvertedObject = (Double)myConvertingObject;
    0