yamid

yamid

  • NA
  • 98
  • 0

Still have problem to Convert cell Value from Excel to Double in c#.....

Feb 23 2009 8:07 PM

Hi

I have still problem to convert a cell value to double...

we have already the code ,below, that can read an excel file

I tried to convert the cell value to double (double x = Convert.ToDouble(oSheet.Cells.Cells[1, 1]);) but there is an error:

how it is possible to convert the cell value obtained from excel to Double?


Unable to cast COM object of type 'System.__ComObject' to interface type 'System.IConvertible'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{805E3B62-B5E9-393D-8941-377D8BF4556B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

 

 

_________________________________________________________

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

double x = Convert.ToDouble(oSheet.Cells.Cells[1, 1]);

Console.WriteLine(x);

//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)