We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

[4682] Query Tool to Excel using C# and .NET

Last post 06-16-2008 11:47 AM by BarbaMariolino. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [4682] Query Tool to Excel using C# and .NET

    This thread is for discussions of Query Tool to Excel using C# and .NET.

    • Post Points: 30
  • 05-26-2005 3:39 PM In reply to

    • dhyan
    • Not Ranked
    • Joined on 05-26-2005
    • New Member
    • Points 15

    .net don't recognize datetime format from excel ce

    I've converted the format of the column to this format MMM-d-yyyy hh:mm:ss.fff ...... but while fetching the values of those columns from excel spreadsheet i get raw data. ex: 33232.4323  .. which is the raw data format of datetime.
    and .NET couldn't recognize this returned format. Plz anyone tell me wot to do
    • Post Points: 0
  • 09-19-2005 6:55 PM In reply to

    • rishib
    • Not Ranked
    • Joined on 09-19-2005
    • New Member
    • Points 5
    I am also stuck in this. Did you figure this out ?
    • Post Points: 0
  • 09-22-2005 1:02 PM In reply to

    • Bambo
    • Not Ranked
    • Joined on 04-27-2004
    • New Member
    • Points 85

    Simple C# code


    Hi, everyone.

    I have been out for a while. I am just learning C# and i discovered its a wonderful language.

    I just started my windows application, but i'm having problems starting it.
    Could you help me with a very simple code.

    The problem i have with it is that once i enter the value in a textbox, how do i convert the textbox value to an int data type.

    Thanks.
    • Post Points: 0
  • 10-11-2005 11:05 PM In reply to

    try this out:
    public  class YourClass {

    private static readonly DateTime march1st1900 = new DateTime(1900, 03, 01);
    private static readonly DateTime december31st1899 = new DateTime(1899, 12, 31);
    private static readonly DateTime january1st1904 = new DateTime(1904, 01, 01);
    private static readonly TimeSpan date1904adjustment = new TimeSpan(4 * 365 + 2, 0, 0, 0, 0);
    private static readonly TimeSpan before1stMarchAdjustment = new TimeSpan(1, 0, 0, 0);

    /// <summary>
    /// Converts the Excel date to the Date which we can read.
    /// </summary>
    /// <param name="excelDate"></param>
    /// <returns></returns>
    DateTime ConvertExcelDateToDate(object excelDate)
    {
       DateTime date = (DateTime)excelDate;
       if (date1904)
           return date + date1904adjustment;
       if (date < march1st1900)
           return date + before1stMarchAdjustment;
       return date;
    }

    /// <summary>
    /// Converts a Date to the Excel date representation
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    object ConvertDateToExcelDate(DateTime date)
    {
               
       if (date1904)
       {
           if (date >= january1st1904)
               return date - date1904adjustment;
           else
               return date.ToString(DateTime.Now.ToString("MM/dd/yyyy"));
       }
       if (date >= march1st1900)
           return date;
       if (date < march1st1900 && date > december31st1899)
           return date - before1stMarchAdjustment;
       return date.ToString(DateTime.Now.ToString("MM/dd/yyyy"));
    }

    } // end class

    Usage:

    object excelDate = myRange.get_Value(Type.Missing);
    DateTime goodDate = ConvertExcelDateToDate(excelDate);

    Hope this helps..the usage code just retrieves the range value and passes it to the Conversion method
    • Post Points: 10
  • 05-30-2006 9:05 AM In reply to

    • P-Ball
    • Not Ranked
    • Joined on 05-30-2006
    • Indonesia
    • New Member
    • Points 10

    Uhm.... try using DateTime.FromOADate(excelDate)

     

    • Post Points: 10
  • 06-29-2006 2:53 PM In reply to

    • KevM
    • Not Ranked
    • Joined on 06-29-2006
    • Ireland
    • New Member
    • Points 5

    Re: Re:

    Cheers P-Ball

    • Post Points: 5
  • 06-16-2008 11:47 AM In reply to

    Re: [4682] Query Tool to Excel using C# and .NET

    Hi,

    As you probably know Excel Automation has many issues, so i think the best way to work with Excel file is by using some third party component. I recommend you to try GemBox.Spreadsheet - .NET component for reading/writing Excel files (XLS, XLSX and CSV) and writing to HTML. Here are a few reasons that Explain why is GemBox.Spreadsheet better than Excel Automation.
    There is also a free version of GemBox.Spreadsheet available. Free version comes with 150 rows limit and can be used in commercial applications.

    Mario
    GemBox Software
    --
    GemBox.Spreadsheet for .NET - Easily read and write Excel (XLS, XLSX or CSV) or export to HTML files from your .NET apps.
    --
    • Post Points: 5
Page 1 of 1 (8 items)