简体   繁体   中英

C# OleDbDataAdapter return null values in cases excel cell is not in Correct data type

I wanna read data from excel spreadsheet and insert them into Access data base(.accdb). there is a problem with data format in excel. some cells are in incorrect type. Correct format for example is 99999 and incorrect is 9999-888 . because of dash(-) ole data adapter return null values instead of text. how should i get whole cell block? I have tried change the cell format of excel from General to Text but the problem is still available. thanks

string connectionStringExcel = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openFileDialog1.FileName + ";Extended Properties=Excel 12.0;";
using (OleDbConnection connectionExcel = new OleDbConnection(connectionStringExcel))
{ 
OleDbDataAdapter daExcel = new OleDbDataAdapter(@"SELECT * FROM [priclist$]", connectionExcel);
DataTable dtExcel = new DataTable();
daExcel.Fill(dtExcel);
dataGridView1.DataSource = dtExcel;
}

I use the following code to transfer data from Excel to datagrid :

  private void btnreadExcell_MouseDown(object sender, MouseButtonEventArgs e)
    {
        try
        {
            string filePath = string.Empty;
            string fileExt = string.Empty;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
            Nullable<bool> result = ofd.ShowDialog();

            if (result == true)//if there is a file choosen by the user
            {
                filePath = ofd.FileName;//get the path of the file
                try
                {
                    DataTable dtExcel = new DataTable();
                    dtExcel = ReadExcel(filePath, fileExt);//read excel file
                                                           // dataGrid.Visible = true;
                    dataGrid.ItemsSource = dtExcel.DefaultView;
                    datagridheader();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }

            con.Close();
        }
        catch { MessageBox.Show("error");}
    }

    public DataTable ReadExcel(string fileName, string fileExt)
    {
        DataTable dtexcel = new DataTable();
        try
        {
            string conn = string.Empty;

            if (fileExt.CompareTo(".xls") == 0)//compare the extension of the file
                conn = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties='Excel 8.0;HRD=Yes;IMEX=1';";//for below excel 2007
            else
                conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';";//for above excel 2007
            using (OleDbConnection con = new OleDbConnection(conn))
            {
                try
                {
                    OleDbDataAdapter oleAdpt = new OleDbDataAdapter("select * from [Sheet1$]", con);//here we read data from sheet1
                    oleAdpt.Fill(dtexcel);//fill excel data into dataTable
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
            conn.Clone();
        }
        catch { MessageBox.Show("error"); }
        return dtexcel;
    }

By default, the Microsoft.ACE.OLEDB.12.0 provider tries to determine variable type for each column, and assumes the majority type is the column type.

You can override this behaviour by specifying IMEX=1 in the connection string, which uses text when encountering mixed-field types instead of setting the ones not matching the type to Null . Then you can handle the values in your C# code:

string connectionStringExcel = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openFileDialog1.FileName + ";Extended Properties=Excel 12.0;IMEX=1;";

Read more about the IMEX property by following the link in the comment below

My error has been Solved!!! with set both IMEX=1 And HDR=No. if you have changed your header in excel files(first row) use below code to solve same problem as i asked above. try this (two connection string have same meaning)

string connectionStringExcel = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openFileDialog1.FileName + ";Extended Properties='Excel 12.0;HDR=No;IMEX=1';"

Or

string connectionStringExcel = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +"T13970524.xlsx" + ";Extended Properties=\"Excel 12.0 Xml;HDR=No;IMEX=1\";";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM