Advertisement

12.17.2007 at 09:10AM PST, ID: 23028401
[x]
Attachment Details

C# Read From Excel : Some fields not read

Asked by Rowdyone52 in Microsoft Visual C#.Net, .Net Editors & IDEs, Visual Studio .NET 2005

Tags: excel, read, from

I have an excel sheet.  It can be seen <a href="http://www.gchma.com/issue.jpg">Here</a>

I am trying to read in each sheet and extract certain fields however some fields just dont seem to be part of the query.  The fields I cannot get are Charge Date="03/22/07" and Acc#=11111

Code Below.
 private void button3_Click(object sender, EventArgs e)
        {
            ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";Extended Properties='Excel 8.0;HDR=No'";

            ADODB.Connection myConnect = new ADODB.Connection();
            ADOX.Catalog myADOX = new ADOX.Catalog();

            myConnect.Open(ConnString, null, null, 0);

            myADOX.ActiveConnection = myConnect;
            foreach (ADOX.Table sheet in myADOX.Tables)
            {
                DataTable dt = ReadExcelSheet(sheet.Name);
            }
            myConnect.Close();
            myADOX.ActiveConnection = null;
        }
        //--------------------------------------------------------------------
        private DataTable ReadExcelSheet(string sheetName)
        {
            DataTable dt = new DataTable();
            OleDbConnection objConn = new OleDbConnection(ConnString);
            objConn.Open();


            if (objConn.State == ConnectionState.Open)
            {
                OleDbDataAdapter da;
                DataSet ds;

                OleDbCommand OldCmd = new OleDbCommand();
                OldCmd.CommandText = "SELECT * FROM ["+sheetName+"]";
                OldCmd.CommandTimeout = 120;
                OldCmd.CommandType = CommandType.Text;
                OldCmd.Connection = objConn;
                da = new OleDbDataAdapter();
                da.SelectCommand = OldCmd;
                ds = new DataSet();

                da.Fill(ds);
                dt = ds.Tables[0];
                objConn.Close();

                int row = 1;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    switch (row)
                    {
                        case 1:
                            if (dt.Rows[i][0].ToString().Trim() == string.Empty)
                            {
                                row = 0;
                                i = i + 8;
                            }
                            else
                            {
                                Console.Write(dt.Rows[i][0].ToString() + ";" + dt.Rows[i][8].ToString() + ";" + dt.Rows[i][10].ToString() + ";" + dt.Rows[i][13].ToString() + ";");
                            }
                            break;
                        case 3: Console.Write(dt.Rows[i][0].ToString());
                            break;
                        case 5: Console.Write(dt.Rows[i][0].ToString());
                            break;
                        case 7: Console.Write(dt.Rows[i][0].ToString());
                            break;
                        case 9: row = 0;
                            Console.WriteLine();
                            break;
                    }
                    row++;
                }
            }
            return dt;
        }Start Free Trial
[+][-]12.23.2007 at 09:04AM PST, ID: 20522005

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual C#.Net, .Net Editors & IDEs, Visual Studio .NET 2005
Tags: excel, read, from
Sign Up Now!
Solution Provided By: angelIII
Participating Experts: 2
Solution Grade: A
 
 
[+][-]01.14.2008 at 03:26PM PST, ID: 20658260

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628