Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Not able to Import Data From Excel 2003 to a DataTable
Please Help
Posted

Have a look:
Excel Data into datatable[^]
import MS Excel to datatable[^]

Export a DataTable to Excel in ASP.NET[^]

Following link shows how to import or export DataTable to Excel or HTML files by using GemBox.Spreadsheet .NET component.
Import or Export DataTable to Excel[^]
 
Share this answer
 
Comments
_Amy 13-Sep-12 3:34am    
5'ed! :)
Prasad_Kulkarni 13-Sep-12 3:37am    
Thank you Amit :D
A codeproject article: 9 Solutions to Export Data to Excel for ASP.NET[^]
And A related discussion: import MS Excel to datatable[^]
 
Share this answer
 
 
Share this answer
 
Simple code to do this
public DataTable GetTable(string filename, string SheetName, string outTableName)
    {
        try
        {
            string Con = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
                        @"Data Source=" + filename + ";" +
                        @"Extended Properties=" + Convert.ToChar(34).ToString() +
                        @"Excel 8.0;" + "Imex=2;" + "HDR=Yes;" + Convert.ToChar(34).ToString();
            OleDbConnection oleConn = new OleDbConnection(Con);
            oleConn.Open();
            OleDbCommand oleCmdSelect = new OleDbCommand();
            oleCmdSelect = new OleDbCommand(
                    @"SELECT * FROM ["
                    + SheetName
                    + "$" + "]", oleConn);
            OleDbDataAdapter oleAdapter = new OleDbDataAdapter();
            oleAdapter.SelectCommand = oleCmdSelect;
            DataTable dt = new DataTable(outTableName);
            oleAdapter.FillSchema(dt, SchemaType.Source);
            oleAdapter.Fill(dt);
            oleCmdSelect.Dispose();
            oleCmdSelect = null;
            oleAdapter.Dispose();
            oleAdapter = null;
            oleConn.Dispose();
            oleConn = null;
            return dt;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
 
Share this answer
 
Comments
Ankit Mishra 21-Oct-13 10:02am    
what is outtable and what to write for SchemaType.Source
[no name] 22-Oct-13 0:48am    
outTableName is the name for returning datatable.

and for below method "Source" is one of the SchemaType. dont do anything to it
FillSchema(DataTable, SchemaType)
C#
public void DtToExcel(DataTable table, string filename)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        HttpContext.Current.Response.Write(@");
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename="+filename+".xls");
        HttpContext.Current.Response.Charset = "utf-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
        //sets font
        HttpContext.Current.Response.Write("<font style="font-size:10.0pt; font-family:Calibri;">");
        HttpContext.Current.Response.Write("<br><br><br>");
        //sets the table border, cell spacing, border color, font of the text, background, foreground, font height
        HttpContext.Current.Response.Write("<table border="1" bgcolor="#FFFFFF" hold=" />          " bordercolor="#000000" cellspacing="0" cellpadding="0" style="font-size:10.0pt; font-family:Calibri; background:white;"> <tr>");
       
        for (int j = 0; j < table.Columns.Count; j++)
        {      //write in new column
            HttpContext.Current.Response.Write("<td>");
            //Get column headers  and make it as bold in excel columns
            HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write(table.Columns[j].ColumnName.ToString());
            HttpContext.Current.Response.Write("");
            HttpContext.Current.Response.Write("</td>");
        }
        HttpContext.Current.Response.Write("</tr>");
        foreach (DataRow row in table.Rows)
        {//write in new row
            HttpContext.Current.Response.Write("<tr>");
            for (int i = 0; i < table.Columns.Count; i++)
            {
                HttpContext.Current.Response.Write("<td>");
                HttpContext.Current.Response.Write(row[i].ToString());
                HttpContext.Current.Response.Write("</td>");
            }
            HttpContext.Current.Response.Write("</tr>");
        }
        HttpContext.Current.Response.Write("</table>");
        HttpContext.Current.Response.Write("</br></br></br></font>");
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
   }


C#
public DataTable GetTable(string filename, string SheetName, string outTableName)
    {
        try
        {
            string Con = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
                        @"Data Source=" + filename + ";" +
                        @"Extended Properties=" + Convert.ToChar(34).ToString() +
                        @"Excel 8.0;" + "Imex=2;" + "HDR=Yes;" + Convert.ToChar(34).ToString();
            OleDbConnection oleConn = new OleDbConnection(Con);
            oleConn.Open();
            OleDbCommand oleCmdSelect = new OleDbCommand();
            oleCmdSelect = new OleDbCommand(
                    @"SELECT * FROM ["
                    + SheetName
                    + "$" + "]", oleConn);
            OleDbDataAdapter oleAdapter = new OleDbDataAdapter();
            oleAdapter.SelectCommand = oleCmdSelect;
            DataTable dt = new DataTable(outTableName);
            oleAdapter.FillSchema(dt, SchemaType.Source);
            oleAdapter.Fill(dt);
            oleCmdSelect.Dispose();
            oleCmdSelect = null;
            oleAdapter.Dispose();
            oleAdapter = null;
            oleConn.Dispose();
            oleConn = null;
            return dt;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
 
Share this answer
 
I hope the following link will help you:- Import data from excel to datatable in asp.net
 
Share this answer
 
Comments
Deepu S Nair 20-Jan-15 2:32am    
Why are you answering old questions?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900