Tough Date conversion problem.. importing from excel

  • Thread starter Thread starter jereviscious
  • Start date Start date
J

jereviscious

Hi all - Last resort time.

I'm importing data from a spreadsheet that I receive from one of my vendor
using interop.excel.

The date field in excel is entered as 4/7/2006, but when I retrieve the
value, it returns a double 38449.0.

I can't figure out how to convert this value into a proper date. I have no
Idea what excel is doing with this date. Also, when I enter in 38449 into
that SAME column in excel it DOES convert it to 4/7/2006.

Here is some code in case anyone is interested in what I'm doing. The last
line is where its blowing up. Look for Convert.ToDateTime...

--------------------------------------------------------------
Microsoft.Office.Interop.Excel.Application excel = null;

Microsoft.Office.Interop.Excel.Workbook wb = null;

Microsoft.Office.Interop.Excel.Worksheet wks=null;

Microsoft.Office.Interop.Excel.Range rng = null;

int intRows;

object[,] data = null;

object missing = Type.Missing;

System.Data.DataTable
dtMedicalInvoices=dsMedicalInvoices1.Medical_Sales_Tracing_Invoices;

System.Data.DataTable
dtMedicalBranches=dsMedicalBranches1.Medical_Sales_Tracing_Distributor_Branches;

try

{

excel = new Microsoft.Office.Interop.Excel.Application();

wb = excel.Workbooks.Open(FilePath, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing);

//excel.Visible = true;

wb.Activate();

}

catch (COMException ex)

{

MessageBox.Show("Error accessing Excel: " + ex.ToString());

}

catch (Exception ex)

{

MessageBox.Show("Error: " + ex.ToString());

}



wks = (Worksheet)wb.ActiveSheet;

intRows = wks.UsedRange.Rows.Count;

for (int I=2;I<=intRows;I++)

{

//Range of the columns in the worksheet I to AB

// Don't get confused between int I and the worksheet column I

rng = wks.get_Range("I"+(string)I.ToString(),"AB"+(string)I.ToString());

data = (object[,])rng.Value2;

DataRow x=dtMedicalInvoices.NewRow();

x[0] = Convert.ToDateTime(data[1,2]);

}
 
The following will solve your problem:

double d = 38449.0;
DateTime dt = DateTime.FromOADate(d);
// dt is now a .NET date time.

Basically it's passing it as an OLE Automation Date value. The above static
method will convert it into a DateTime structure.

HTH

- Andy
 
Beautiful, Thanks.
Andy Bates said:
The following will solve your problem:

double d = 38449.0;
DateTime dt = DateTime.FromOADate(d);
// dt is now a .NET date time.

Basically it's passing it as an OLE Automation Date value. The above
static method will convert it into a DateTime structure.

HTH

- Andy
jereviscious said:
Hi all - Last resort time.

I'm importing data from a spreadsheet that I receive from one of my
vendor
using interop.excel.

The date field in excel is entered as 4/7/2006, but when I retrieve the
value, it returns a double 38449.0.

I can't figure out how to convert this value into a proper date. I have
no
Idea what excel is doing with this date. Also, when I enter in 38449 into
that SAME column in excel it DOES convert it to 4/7/2006.

Here is some code in case anyone is interested in what I'm doing. The
last
line is where its blowing up. Look for Convert.ToDateTime...

--------------------------------------------------------------
Microsoft.Office.Interop.Excel.Application excel = null;

Microsoft.Office.Interop.Excel.Workbook wb = null;

Microsoft.Office.Interop.Excel.Worksheet wks=null;

Microsoft.Office.Interop.Excel.Range rng = null;

int intRows;

object[,] data = null;

object missing = Type.Missing;

System.Data.DataTable
dtMedicalInvoices=dsMedicalInvoices1.Medical_Sales_Tracing_Invoices;

System.Data.DataTable
dtMedicalBranches=dsMedicalBranches1.Medical_Sales_Tracing_Distributor_Branches;

try

{

excel = new Microsoft.Office.Interop.Excel.Application();

wb = excel.Workbooks.Open(FilePath, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing);

//excel.Visible = true;

wb.Activate();

}

catch (COMException ex)

{

MessageBox.Show("Error accessing Excel: " + ex.ToString());

}

catch (Exception ex)

{

MessageBox.Show("Error: " + ex.ToString());

}



wks = (Worksheet)wb.ActiveSheet;

intRows = wks.UsedRange.Rows.Count;

for (int I=2;I<=intRows;I++)

{

//Range of the columns in the worksheet I to AB

// Don't get confused between int I and the worksheet column I

rng = wks.get_Range("I"+(string)I.ToString(),"AB"+(string)I.ToString());

data = (object[,])rng.Value2;

DataRow x=dtMedicalInvoices.NewRow();

x[0] = Convert.ToDateTime(data[1,2]);

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top