473,378 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Accessing Excel object in windows c# application

Hi Folks,

I have a question regarding my windows c# application.
This application just reads MS Excel file and puts the data in to sql server
database.
In that excel file ,it has one named cell as "OutputStampTime".
I am facing problem while accessing this cell value.
When I open excel, I can see the value as "6:49 AM" . But when I click on
that cell
in the formula bar I am seeing as 01/27/2005 06:49:47 AM.

When I tried to read that value using c# code I am getting that value as
06:49 AM (If I use range Text property) or "38380.2845648148" (If I use
range value property).
I want to get exact time from this cell . (i.e as 06:49:47 AM).
How can I convert 38380.2845648148 to corresponding time in c#???
In Excel if you format the same cell as general number it is showing as
38380.2845648148 .

But How can I programatically format that value using c#,Excel code??
This excel file which I am reading is protected. we don't know password to
unprotect it.

The following is the sample code....


Excel.Range rng1,rng2,rng3,rng4;
Excel.Worksheet worksheet = new Excel.Worksheet() ;
Excel.Sheets sheets;
Excel.Workbook theWorkbook;
int result;
string SheetName;
string sTime1;
Excel.Application ExcelObj = null;
ExcelObj = new Excel.Application();

theWorkbook =ExcelObj.Workbooks.Open("C:\\TestExcel1.xls", 0, true,5,"",
"", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true);

sheets = theWorkbook.Worksheets;

for (int i=1; i<=sheets.Count; i++)
{
worksheet = (Excel.Worksheet) theWorkbook.Worksheets.get_Item(i);
SheetName = worksheet.Name.Trim();
result = string.Compare(SheetName,"Sheet1",true);
if (result == 0)
{
break;
}
}

rng1 = worksheet.get_Range("OutputStampDate","OutputStamp Date");
sTime1 = rng1.Text.ToString();

//"38380.2845648148" {38380.284564814814} [System.Double]:
theWorkbook.Close((System.Boolean)false,System.Ref lection.Missing.Value,
System.Reflection.Missing.Value );

How can I convert double value to datetime value in c#?
I tried several options of using string.format and convert.ToDateTime but
NO Luck.

I appreciate If some one through light on this and direct me to some sample
codes..

Thanking you
Kumar


Nov 16 '05 #1
2 30536
Kumar,

You can make the conversion on the Excel side using built-in Excel
functions. Once you get the Range object, use the WorksheetFunction "Text"
from Excel's OM to get the exact time.

rng1 = worksheet.get_Range("OutputStampDate","OutputStamp Date");
sTime1 = ExcelObj.Application.WorksheetFunction.Text(rng1.T ext,@"HH:DD:SS
AM/PM");

DateTime dtTime = DateTime.Parse(sTime1);

Try and see if that works for you

--

Charles
www.officezealot.com

"Kumar" <Ku***@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
Hi Folks,

I have a question regarding my windows c# application.
This application just reads MS Excel file and puts the data in to sql
server
database.
In that excel file ,it has one named cell as "OutputStampTime".
I am facing problem while accessing this cell value.
When I open excel, I can see the value as "6:49 AM" . But when I click on
that cell
in the formula bar I am seeing as 01/27/2005 06:49:47 AM.

When I tried to read that value using c# code I am getting that value as
06:49 AM (If I use range Text property) or "38380.2845648148" (If I use
range value property).
I want to get exact time from this cell . (i.e as 06:49:47 AM).
How can I convert 38380.2845648148 to corresponding time in c#???
In Excel if you format the same cell as general number it is showing as
38380.2845648148 .

But How can I programatically format that value using c#,Excel code??
This excel file which I am reading is protected. we don't know password to
unprotect it.

The following is the sample code....


Excel.Range rng1,rng2,rng3,rng4;
Excel.Worksheet worksheet = new Excel.Worksheet() ;
Excel.Sheets sheets;
Excel.Workbook theWorkbook;
int result;
string SheetName;
string sTime1;
Excel.Application ExcelObj = null;
ExcelObj = new Excel.Application();

theWorkbook =ExcelObj.Workbooks.Open("C:\\TestExcel1.xls", 0, true,5,"",
"", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true);

sheets = theWorkbook.Worksheets;

for (int i=1; i<=sheets.Count; i++)
{
worksheet = (Excel.Worksheet) theWorkbook.Worksheets.get_Item(i);
SheetName = worksheet.Name.Trim();
result = string.Compare(SheetName,"Sheet1",true);
if (result == 0)
{
break;
}
}

rng1 = worksheet.get_Range("OutputStampDate","OutputStamp Date");
sTime1 = rng1.Text.ToString();

//"38380.2845648148" {38380.284564814814} [System.Double]:
theWorkbook.Close((System.Boolean)false,System.Ref lection.Missing.Value,
System.Reflection.Missing.Value );

How can I convert double value to datetime value in c#?
I tried several options of using string.format and convert.ToDateTime
but
NO Luck.

I appreciate If some one through light on this and direct me to some
sample
codes..

Thanking you
Kumar

Nov 16 '05 #2
Hi Charles,

Thank You very much for your help.
The following syntax is worked for me.
sTime1 = ExcelObj.Application.WorksheetFunction.Text(rng1.V alue,@"HH:MM:SS
AM/PM");

I almost worked for 6 hours on this problem yesterday.
I appreciate for all your help.

Thanking you
Kumar

"Charles Maxson" wrote:
Kumar,

You can make the conversion on the Excel side using built-in Excel
functions. Once you get the Range object, use the WorksheetFunction "Text"
from Excel's OM to get the exact time.

rng1 = worksheet.get_Range("OutputStampDate","OutputStamp Date");
sTime1 = ExcelObj.Application.WorksheetFunction.Text(rng1.T ext,@"HH:DD:SS
AM/PM");

DateTime dtTime = DateTime.Parse(sTime1);

Try and see if that works for you

--

Charles
www.officezealot.com

"Kumar" <Ku***@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
Hi Folks,

I have a question regarding my windows c# application.
This application just reads MS Excel file and puts the data in to sql
server
database.
In that excel file ,it has one named cell as "OutputStampTime".
I am facing problem while accessing this cell value.
When I open excel, I can see the value as "6:49 AM" . But when I click on
that cell
in the formula bar I am seeing as 01/27/2005 06:49:47 AM.

When I tried to read that value using c# code I am getting that value as
06:49 AM (If I use range Text property) or "38380.2845648148" (If I use
range value property).
I want to get exact time from this cell . (i.e as 06:49:47 AM).
How can I convert 38380.2845648148 to corresponding time in c#???
In Excel if you format the same cell as general number it is showing as
38380.2845648148 .

But How can I programatically format that value using c#,Excel code??
This excel file which I am reading is protected. we don't know password to
unprotect it.

The following is the sample code....


Excel.Range rng1,rng2,rng3,rng4;
Excel.Worksheet worksheet = new Excel.Worksheet() ;
Excel.Sheets sheets;
Excel.Workbook theWorkbook;
int result;
string SheetName;
string sTime1;
Excel.Application ExcelObj = null;
ExcelObj = new Excel.Application();

theWorkbook =ExcelObj.Workbooks.Open("C:\\TestExcel1.xls", 0, true,5,"",
"", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true);

sheets = theWorkbook.Worksheets;

for (int i=1; i<=sheets.Count; i++)
{
worksheet = (Excel.Worksheet) theWorkbook.Worksheets.get_Item(i);
SheetName = worksheet.Name.Trim();
result = string.Compare(SheetName,"Sheet1",true);
if (result == 0)
{
break;
}
}

rng1 = worksheet.get_Range("OutputStampDate","OutputStamp Date");
sTime1 = rng1.Text.ToString();

//"38380.2845648148" {38380.284564814814} [System.Double]:
theWorkbook.Close((System.Boolean)false,System.Ref lection.Missing.Value,
System.Reflection.Missing.Value );

How can I convert double value to datetime value in c#?
I tried several options of using string.format and convert.ToDateTime
but
NO Luck.

I appreciate If some one through light on this and direct me to some
sample
codes..

Thanking you
Kumar


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Otie | last post by:
I found the following under the GetObject help notes and in the example for GetObject: "This example uses the GetObject function to get a reference to a specific Microsoft Excel worksheet...
6
by: Matthew Wieder | last post by:
I have the following requirements: Build a stand-alone C# application that asks the user to click in a cell in an Excel spreadsheet, and then displays the address of that cell in the C#...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
22
by: Howard Kaikow | last post by:
There's a significant problem in automating Excel from VB .NET. Reminds me of a problem I encountered almost 3 years ago that was caused by the Norton Auntie Virus Office plug-in. Can anybody...
2
by: scorpion53061 | last post by:
This excel find/replace code works great under Excel 2003 but bombs with an error (pasted below the code) in Excel 2000. Can anyone suggest an alternative to make both happy? I am using vb.net...
0
by: cappa | last post by:
Hi, i've a problem with a visual basic .net application that use excel. In local everything is fine, but i've to install this application in sometoher computer, and theese pc don't have the same...
0
by: tmg181 | last post by:
I currently upgraded a VB application to a VB.NET application with only a few modifications. One of the functions of the application is to create an Excel spreadsheet with the given values that were...
1
by: Carlos | last post by:
I'm getting RPC error when I try to register analys32.xll in the VB program. ErrorCode=-2147417851 Message="El servidor lanzó una excepción. (Exception from HRESULT: 0x80010105...
22
by: robertgregson | last post by:
Using C#, .NET3.5, Visual Studio 2008 and WCF on Windows VISTA SP1, I have written a service, service host (as a C# console application) and a client. The service uses...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.