1

I have a Application on which i'm uploading a Excel File and displaying the data inside a GridView. My problems is now that, When I'm uploading the File gets uploaded. If the Data contains a MIX of Intergers and Text data, Text data's are not shown inside the Gridview and the DataSet on which im loading it.

public DataTable GetExcelData(string _FileName)
{
    DataSet ds = new DataSet();

    string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + _FileName + ";Extended Properties=Excel 8.0;";

    OleDbConnection connection = new OleDbConnection(connectionString);
    connection.Open();

    string sheetname = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["table_name"].ToString();
    try
    {
        OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + sheetname + "]", connection);
        adapter.Fill(ds);
        connection.Close();
        return ds.Tables[0]; ;
    }
    catch (Exception ex)
    {
        try
        {
            if (connection.State == ConnectionState.Open)
                connection.Close();
        }
        catch (Exception) { }

        throw ex;
    }
}

This is code from which i'm loading the data onto the DataSet.

Ill show you an Example data


Column1

  1. 1234
  2. 2345
  3. 4567
  4. T123
  5. 123Q
  6. 6789

If we upload an Excel sheet with the Data Above it will only display it like this..


Column1

  1. 1234
  2. 2345
  3. 4567
  4. .
  5. .
  6. 6789

You can see in the Above T123,123Q is missing(it will be BLANK data). This is the Problem with my application when i'm Uploading an Excel sheet.

Has anyone come across a situation like this? Is there any way we can resolve this?

1 Answer 1

2

Try this connection string... Its work fine now

This connection string is define by use its work for XLS

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + _FileName + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"

For XLSX

string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + _FileName +";Extended Properties='Excel 12.0;HDR=YES;'"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.