Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4270958
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:22:37+00:00 2026-05-21T07:22:37+00:00

I have wrote a code that can read an excel 2007 file using Microsoft

  • 0

I have wrote a code that can read an excel 2007 file using Microsoft Data Access Engine the below code snippet works fine for most of the files but it happens to work for most of the excel files i.e. .xlsx,.xls but when it fails at objConn.Open(); for excel files that have excel formatting problems please refer to the image below

Excel file with formatting errors

It would failed to open the ole Db Connection stating error External table is not in the expected format . One more problem with this import procedure is that

            OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM "+ SelectionSheet, objConn);

is not able to read sheets starting with spaces any help with solving this issue would be highly appreciated.

       public DataTable ReadExcel(string Path, ArrayList IgnoreString, ArrayList IgnoreColumn)
        {
            DataTable dtReturn = new DataTable();
            DataTable dtPrintable = new DataTable();
            DataTable dtTemp = new DataTable();
            try
            {
                string sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                                        "Data Source=" + Path + ";" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"";

                OleDbConnection objConn = new OleDbConnection(sConnectionString);




                objConn.Open();
                DataTable dtSheetnames = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                DataTable dtTesting = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.DbInfoLiterals, new object[] {});
                DataTable dtTesting2 = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, new object[] { });


                string SelectionSheet = dtSheetnames.Rows[0][2].ToString();

                if (SelectionSheet.Contains("'") )
                {
                    SelectionSheet = SelectionSheet.Remove(0, 1);
                    SelectionSheet = "[" + SelectionSheet;
                    SelectionSheet = SelectionSheet.Remove(SelectionSheet.Length - 1, 1);
                    // -- Mod by zeemz on 23 dec
                    //    string PrintArea = SelectionSheet + "Print_Area]";

                    SelectionSheet = SelectionSheet + "]";
                }
                else
                {
                    SelectionSheet = "["+ SelectionSheet + "]";
                }


                //OleDbCommandBuilder objCmdBuilder = new OleDbCommandBuilder(


                OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM "+ SelectionSheet, objConn);




                OleDbDataAdapter objAdapter = new OleDbDataAdapter();
                DataSet objDataSet = new DataSet();

                DataSet PrintAreads = new DataSet();

                objAdapter.SelectCommand = objCmdSelect;
                objAdapter.Fill(objDataSet);

                // -- Mod by zeemz on 23 dec
                //objCmdSelect.CommandText = "SELECT * FROM " + PrintArea;
                //objAdapter.Fill(PrintAreads);




                objConn.Close();

                dtReturn = objDataSet.Tables[0].Copy();
               // dtPrintable = PrintAreads.Tables[0].Copy();

                // -- Mod by zeemz on 23 dec
                //if (dtPrintable.Columns.Count != dtReturn.Columns.Count)
                //{
                //    int TotalPrintable = dtPrintable.Columns.Count;
                //    int TotalComing = dtReturn.Columns.Count;
                //    int StartRemovingPos = TotalComing - TotalPrintable;

                //    for (int i = TotalPrintable; dtPrintable.Columns.Count != dtReturn.Columns.Count; i++)
                //    {

                //        dtReturn.Columns.RemoveAt(i);
                //        i = i - 1 ;
                //    }


                //}


                int iCount = 0;
                while (iCount <= dtReturn.Rows.Count - 1)
                {
                    if (isRowEmpty(dtReturn.Rows[iCount]))
                    {
                        dtReturn.Rows.RemoveAt(iCount);
                    }
                    else
                    {
                        iCount += 1;
                    }
                }


                //now applying the filters

                //column ignore
                for (int i = IgnoreColumn.Count - 1; i >= 0; i--)
                {
                    dtReturn.Columns.RemoveAt((int)IgnoreColumn[i]);
                }

                //string ignore
                for (int i = IgnoreString.Count - 1; i >= 0; i--)
                {
                    for (int j = dtReturn.Rows.Count - 1; j >= 0; i--)
                    {
                        foreach (DataColumn dCol in dtReturn.Columns)
                        {
                            if (dtReturn.Rows[j][dCol.ColumnName].ToString().ToLower().Contains(IgnoreString[i].ToString().ToLower()))
                            {
                                dtReturn.Rows.RemoveAt(j);
                                break;
                            }
                        }
                    }
                }


                /* Hack to get rid of DateTime Columns */
                // added by zeemz
                dtTemp = dtReturn.Clone();
                dtTemp.Clear();
                foreach (DataColumn tempColumn in dtTemp.Columns)
                {
                //    if (tempColumn.DataType == typeof(DateTime))
//                    {
                        tempColumn.DataType = typeof(String);
  //                  }
                }
                foreach (DataRow tempRow in dtReturn.Rows)
                {
                    DataRow insRow = dtTemp.NewRow();
                    foreach (DataColumn tempColumn in dtReturn.Columns)
                    {

                        if (tempColumn.DataType == typeof(DateTime))
                        {
                            if (!String.IsNullOrEmpty(tempRow[tempColumn.ColumnName.ToString()].ToString()))
                            {
                                insRow[tempColumn.ColumnName.ToString()] = Convert.ToDateTime(tempRow[tempColumn.ColumnName.ToString()].ToString()).ToString("yyyyMMddhhmmss");
                            }
                            else
                            {
                                insRow[tempColumn.ColumnName.ToString()] = "";
                            }
                        }
                        else
                        {

                            insRow[tempColumn.ColumnName.ToString()] = tempRow[tempColumn.ColumnName.ToString()].ToString();
                        }

                    }
                    dtTemp.Rows.Add(insRow);
                }



            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dtTemp;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:22:38+00:00Added an answer on May 21, 2026 at 7:22 am

    I happen to find out the problem exists when you modify the xlsx file manually or programatically without using excel once you modify the xlsx the format doesn’t stay intact and the above mentioned error comes up because OleDbDataAdapter is unable to handle the modified file and Excel it self can fix the corrupt files shows the file as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a small .NET add in to excel 2007 that read data from
I try to wrote code that read data from stdin: size_t bufSize = 1024;
I'm using OleDb to select data from excel spreadsheets. Each spreadsheet can contain many
I have been using Java's ConcurrentMap for a map that can be used from
OLEDB can be used to read and write Excel sheets. Consider the following code
I have an application that allows users to write their own code in a
I have a mobile platform that I am trying to write some communications code
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
Hi guys I wrote this code and i have two errors. Invalid rank specifier:
For a school assignment I have to write x86 assembly code, except I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.