Domanda

I'm importing an excel file into a DataTable, and then getting the information i need from each subsequent DataRow, which are then to be inserted into a list. I have a method that i call when i need to import an Excel (.xlsx or .xls) file into a DataTable, and i use it 6 or 7 other places in my program, so i'm pretty sure there aren't any errors there.

My problem is that when i access a DataRow, on this specific DataTable, the first few fields contain values, but everything else is just null. If i look at it in Locals window i can see that the DataRow looks like this:

[0] {"Some string value"}
[1] {}
[2] {}
[3] {}

When it should look like this:

[0] {"Some string value"}
[1] {"Another string value"}
[2] {"Foo"}
[3] {"Bar"}

Here is the method that handles the import:

public List<DataTable> ImportExcel(string FileName)
        {
            List<DataTable> _dataTables = new List<DataTable>();
            string _ConnectionString = string.Empty;
            string _Extension = Path.GetExtension(FileName);
            //Checking for the extentions, if XLS connect using Jet OleDB
            if (_Extension.Equals(".xls", StringComparison.CurrentCultureIgnoreCase))
            {
                _ConnectionString =
                    "Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties=Excel 8.0";
            }
            //Use ACE OleDb
            else if (_Extension.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase))
            {
                _ConnectionString =
                    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0";
            }

            DataTable dataTable = null;
            var count = 0;
            using (OleDbConnection oleDbConnection =
                new OleDbConnection(string.Format(_ConnectionString, FileName)))
            {
                oleDbConnection.Open();
                //Getting the meta data information.
                //This DataTable will return the details of Sheets in the Excel File.
                DataTable dbSchema = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, null);
                foreach (DataRow item in dbSchema.Rows)
                {
                    //reading data from excel to Data Table
                    using (OleDbCommand oleDbCommand = new OleDbCommand())
                    {
                        oleDbCommand.Connection = oleDbConnection;
                        oleDbCommand.CommandText = string.Format("SELECT * FROM [{0}]",
                            item["TABLE_NAME"].ToString());

                        using (OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter())
                        {
                                if (count < 3)
                                {
                                    oleDbDataAdapter.SelectCommand = oleDbCommand;
                                    dataTable = new DataTable(item["TABLE_NAME"].ToString());
                                    oleDbDataAdapter.Fill(dataTable);
                                    _dataTables.Add(dataTable);
                                    count++;
                                }
                        }
                    }
                }
            }
            return _dataTables;
        }

Any thoughts?

È stato utile?

Soluzione 2

Turns out the fault was with the excel document. Apparently excel documents has a hidden table called Shared Strings. It was because of this table i couldn't find the values i was looking for.

Altri suggerimenti

You may need to add ;IMEX=1 to "Extended Properties" in your connection string. But ultimately, reading excel files with OleDb is flimsy at best. You should use a 3rd party library that deals with them nativly like:

NPOI for XLS https://code.google.com/p/npoi/

EPPlus for XLSX http://epplus.codeplex.com/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top