0

I imported an Excel table to gridview. In my own notebook, everything is ok. However, in some notebooks Excel cells return back empty. I'm using ASP.NET 4.0 and Visual C#

using System;
using System.Data.OleDb;
using System.Data;
using System.IO;

namespace ReadExcelInToDataSet
{
    public partial class Default : System.Web.UI.Page
    {
        OleDbConnection oledbConn;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string path = System.IO.Path.GetFullPath(Server.MapPath("~/Table_sverks.xls"));
                if (Path.GetExtension(path) == ".xls")
                {
                    oledbConn = new   OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
                }
                else if (Path.GetExtension(path) == ".xlsx")
                {
                    oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
                }
                oledbConn.Open();
                OleDbCommand cmd = new OleDbCommand();
                OleDbDataAdapter oleda = new OleDbDataAdapter();
                DataSet ds = new DataSet();
                cmd.Connection = oledbConn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "SELECT  * FROM [Sheet1$]";
                oleda = new OleDbDataAdapter(cmd);
                oleda.Fill(ds);
                grvData.DataSource = ds.Tables[0];
                grvData.DataBind();
            }

            catch (Exception ex)
            {
                lblError.Text = ex.ToString();
            }
            finally
            {
                oledbConn.Close();
            }
        }
    }
}
Charles Caldwell
  • 16,649
  • 4
  • 40
  • 47

1 Answers1

0

I think it's the same issue in this question OleDB & mixed Excel datatypes : missing data

Community
  • 1
  • 1
kaito ked
  • 175
  • 1
  • 1
  • 15