Question

I have an Excel file and want to display excel file in a data grid view if user click a button. Columns are

Date, Day, Start time, End time, Totaltime, Difftime and Work done.

And my sample row data is as follows for A10 row and A11 rows.

A10 01-03-2014 Saturda 8:20 22:30 13:40 4:40 Design

A11 02-03-2014 Sunday 9:00 21:00 11:30 2:30 Coding

My rows lies in the range from 10 to 40 and columns are from A-G

If I import it to data grid view dates and times are not displaying properly. 01/03/2014 appears as 404977. I want code to export the Excel file to datagridview and also datagridview should display the same as Excel. Can anyone explain how to do it in C# with date day, start, end, total, diff, work done as datagridview columns?

enter image description here

No correct solution

OTHER TIPS

I assume you want to import to a window forms datagridview,

Excel 2003,

private void button1_Click(object sender, EventArgs e)
    {
        String name = "Yoursheetname";
        String constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                        "C:\\Sample.xls" + 
                        ";Extended Properties='Excel 8.0;HDR=YES;';";

        OleDbConnection con = new OleDbConnection(constr);
        OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con);
        con.Open();

        OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
        DataTable data = new DataTable();
        sda.Fill(data);
        grid_items.DataSource = data;
    }

for Excel 2007,

 private void button1_Click(object sender, EventArgs e)
    {
        String name = "Yoursheetname";
        String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                        "C:\\Sample.xlsx" + 
                        ";Extended Properties='Excel 12.0 XML;HDR=YES;';";

        OleDbConnection con = new OleDbConnection(constr);
        OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con);
        con.Open();

        OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
        DataTable data = new DataTable();
        sda.Fill(data);
        grid_items.DataSource = data;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top