Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

What is the error in function to export data from excel to dataset?

$
0
0

I have a function to export data from excel to Dataset which is as follows,

public DataSet GetDataFromExcel(string filePath)
{
    string strConn;
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
    "Data Source=" + filePath + ";" +
    "Extended Properties=Excel 8.0;";
    DataTable dt = new DataTable();
    dt = null;
    using (OleDbConnection oleDB = new OleDbConnection(strConn))
    {
        oleDB.Open();
        dt = oleDB.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        if (dt == null)
            return null;

        ArrayList arr = new ArrayList();
        //ListItemCollection items = new ListItemCollection();
        int i = 0;

        for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
        {
            string excelSheetName;
            string lastCharacter = "";

            excelSheetName = dt.Rows[rowIndex]["TABLE_NAME"].ToString();
            excelSheetName = excelSheetName.Replace("'", "");
            lastCharacter = excelSheetName.Substring(excelSheetName.Length - 1, 1);
            if (lastCharacter == "$")
            {
                arr.Add(dt.Rows[rowIndex]["TABLE_NAME"].ToString());
                //items.Add(dt.Rows[rowIndex]["TABLE_NAME"].ToString());
            }
        }
        //if (items.Count > 1)
        if (arr.Count > 1)
            return null;

        string sName;
        string query;

        //sName = items[0].ToString();
        sName = arr[0].ToString();
        sName = sName.Replace("'", "");
        sName = sName.Replace("$", "");

        query = "";
        query = String.Format("select * from [{0}$]", sName);
        OleDbDataAdapter da = new OleDbDataAdapter(query, strConn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;
    }
}

I tried to export data from one of my excel sheet which have 900 rows. The function only gets 253 rows. But I want all the rows. What is the problem with the function? Could you help me out? Thanks.


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>