How to check excel Date Format using C#? Example.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <style type="text/css">
        .auto-style1 {
            width: 50%;
        }
    </style>

</head>
<body>
    <form id="form1" runat="server">
 

    <table align="center" class="auto-style1">
        <tr>
            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" Width="200px" />&nbsp;&nbsp;<asp:Button ID="Button1" runat="server" Style="margin-left: 0px" Text="Upload" OnClick="Button1_Click" /></td>
         
        </tr>
        <tr>
            <td>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" HtmlEncode="False" ReadOnly="True" />
                        <asp:BoundField DataField="NAME" HeaderText="NAME" HtmlEncode="False" ReadOnly="True" />
                        <asp:BoundField DataField="JOINDATE" HeaderText="JOINDATE" HtmlEncode="False" DataFormatString="{0:d}" ReadOnly="True" />
                    </Columns>


                </asp:GridView>
            </td>

        </tr>
    </table>
      </form>
</body>
</html>
-----------------------------------------------
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Excel = Microsoft.Office.Interop.Excel.Application;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // processExcel("D:\\Book1.xlsx");
    }
    private void processExcel(string filename)
    {
        Application xlApp;
        Workbook xlWorkBook;
        Worksheet xlWorkSheet;

        var missing = System.Reflection.Missing.Value;

        xlApp = new ApplicationClass();
        xlWorkBook = xlApp.Workbooks.Open(filename);
        //xlWorkBook = xlApp.Workbooks.Open(filename, false, true, missing, missing, missing, true, XlPlatform.xlWindows, '\t', false, false, 0, false, true, 0);
        xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);

        Range xlRange = xlWorkSheet.UsedRange;
        Array myValues = (Array)xlRange.Cells.Value2;



        int vertical = myValues.GetLength(0);
        int horizontal = myValues.GetLength(1);

        System.Data.DataTable dt = new System.Data.DataTable();
        DateTime tempD = new DateTime();
        //if (DateTime.TryParse(myValues.GetValue(3, 3).ToString(), out tempD))
        //{

        //}
        // must start with index = 1
        // get header information
        for (int i = 1; i <= horizontal; i++)
        {
            dt.Columns.Add(new DataColumn(myValues.GetValue(1, i).ToString()));
        }

        // Get the row information
        for (int a = 2; a <= vertical; a++)
        {


            object[] poop = new object[horizontal];
            for (int b = 1; b <= horizontal; b++)
            {
                try
                {
                    poop[b - 1] = myValues.GetValue(a, b);
                    if (poop[2] != null)
                    {

                        double d = double.Parse(myValues.GetValue(a, b).ToString());
                        // DateTime conv = DateTime.FromOADate(d);
                        DateTime conv = DateTime.FromOADate(d);
                        //string date = conv.Date.ToString();
                        //string date = conv.ToShortDateString();
                        // poop[b - 1] = date;
                        poop[b - 1] = conv;
                    }
                    else
                    {
                        poop[b - 1] = myValues.GetValue(a, b);
                    }
                }
                catch (Exception ex)
                {
                    poop[b - 1] = "DateTime is not currect format !";
                }

            }
            DataRow row = dt.NewRow();
            row.ItemArray = poop;
            dt.Rows.Add(row);
        }
        //
        //foreach (DataRow dr in dt.Rows)
        //{
        //    double d = double.Parse(dr["JoinDate"].ToString());
        //    DateTime conv = DateTime.FromOADate(d);
        //    string date = conv.Date.ToString();


        //}

        // assign table to default data grid view

        GridView1.DataSource = dt;
        GridView1.DataBind();
        xlWorkBook.Close(true, missing, missing);
        xlApp.Quit();

        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);
    }

    private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            Response.Write("Unable to release the Object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string FilePath = FileUpload1.PostedFile.FileName;
        //string path = @"D:\Book1.xlsx";
        // string path = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);

        processExcel(FilePath);
    }
}
Share:

No comments:

Post a Comment

Monday, 8 January 2018

How to check excel Date Format using C#? Example.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <style type="text/css">
        .auto-style1 {
            width: 50%;
        }
    </style>

</head>
<body>
    <form id="form1" runat="server">
 

    <table align="center" class="auto-style1">
        <tr>
            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" Width="200px" />&nbsp;&nbsp;<asp:Button ID="Button1" runat="server" Style="margin-left: 0px" Text="Upload" OnClick="Button1_Click" /></td>
         
        </tr>
        <tr>
            <td>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" HtmlEncode="False" ReadOnly="True" />
                        <asp:BoundField DataField="NAME" HeaderText="NAME" HtmlEncode="False" ReadOnly="True" />
                        <asp:BoundField DataField="JOINDATE" HeaderText="JOINDATE" HtmlEncode="False" DataFormatString="{0:d}" ReadOnly="True" />
                    </Columns>


                </asp:GridView>
            </td>

        </tr>
    </table>
      </form>
</body>
</html>
-----------------------------------------------
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Excel = Microsoft.Office.Interop.Excel.Application;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // processExcel("D:\\Book1.xlsx");
    }
    private void processExcel(string filename)
    {
        Application xlApp;
        Workbook xlWorkBook;
        Worksheet xlWorkSheet;

        var missing = System.Reflection.Missing.Value;

        xlApp = new ApplicationClass();
        xlWorkBook = xlApp.Workbooks.Open(filename);
        //xlWorkBook = xlApp.Workbooks.Open(filename, false, true, missing, missing, missing, true, XlPlatform.xlWindows, '\t', false, false, 0, false, true, 0);
        xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);

        Range xlRange = xlWorkSheet.UsedRange;
        Array myValues = (Array)xlRange.Cells.Value2;



        int vertical = myValues.GetLength(0);
        int horizontal = myValues.GetLength(1);

        System.Data.DataTable dt = new System.Data.DataTable();
        DateTime tempD = new DateTime();
        //if (DateTime.TryParse(myValues.GetValue(3, 3).ToString(), out tempD))
        //{

        //}
        // must start with index = 1
        // get header information
        for (int i = 1; i <= horizontal; i++)
        {
            dt.Columns.Add(new DataColumn(myValues.GetValue(1, i).ToString()));
        }

        // Get the row information
        for (int a = 2; a <= vertical; a++)
        {


            object[] poop = new object[horizontal];
            for (int b = 1; b <= horizontal; b++)
            {
                try
                {
                    poop[b - 1] = myValues.GetValue(a, b);
                    if (poop[2] != null)
                    {

                        double d = double.Parse(myValues.GetValue(a, b).ToString());
                        // DateTime conv = DateTime.FromOADate(d);
                        DateTime conv = DateTime.FromOADate(d);
                        //string date = conv.Date.ToString();
                        //string date = conv.ToShortDateString();
                        // poop[b - 1] = date;
                        poop[b - 1] = conv;
                    }
                    else
                    {
                        poop[b - 1] = myValues.GetValue(a, b);
                    }
                }
                catch (Exception ex)
                {
                    poop[b - 1] = "DateTime is not currect format !";
                }

            }
            DataRow row = dt.NewRow();
            row.ItemArray = poop;
            dt.Rows.Add(row);
        }
        //
        //foreach (DataRow dr in dt.Rows)
        //{
        //    double d = double.Parse(dr["JoinDate"].ToString());
        //    DateTime conv = DateTime.FromOADate(d);
        //    string date = conv.Date.ToString();


        //}

        // assign table to default data grid view

        GridView1.DataSource = dt;
        GridView1.DataBind();
        xlWorkBook.Close(true, missing, missing);
        xlApp.Quit();

        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);
    }

    private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            Response.Write("Unable to release the Object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string FilePath = FileUpload1.PostedFile.FileName;
        //string path = @"D:\Book1.xlsx";
        // string path = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);

        processExcel(FilePath);
    }
}

No comments:

Post a Comment

Popular

Total Pageviews

Archive