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 6110567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:31:51+00:00 2026-05-23T14:31:51+00:00

I am connecting to an Excel spreadsheet via OleDbConnection . How do I order

  • 0

I am connecting to an Excel spreadsheet via OleDbConnection .

How do I order by ColumnNumber ? I’d like to do something like :
SELECT * FROM [Sheet1$] ORDERBY ColumnNumber
where ColumnNumber is a number like 1 or 2 ?
Any ideas ?
Note: the file I’m trying to open has no headers.

 private String BuildConnectionStringXLS()
        {
            String fileName = GetFileName();

            Dictionary<string, string> props = new Dictionary<string, string>();
            props["Provider"] = "Microsoft.Jet.OLEDB.4.0";
            props["Data Source"] = fileName;
            props["Extended Properties"] = "\"Excel 8.0;HDR=No;IMEX=1\"";
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<string, string> prop in props) 
            {
                sb.Append(prop.Key); 
                sb.Append('=');
                sb.Append(prop.Value); 
                sb.Append(';'); 
            }
            return sb.ToString();
        }




    public DataTable GetFullTable(int columnToOrderBy)
        {
            String fileName = GetFileName();

            DataTable resultDataTable = new DataTable();
            String connectionString = BuildConnectionString();
            OleDbConnection conn = new OleDbConnection(connectionString);
            conn.Open();
            OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sheet1$] ORDERBY ColumnNumber", conn);
            da.Fill(resultDataTable);
            conn.Close();
            return resultDataTable;
        }
  • 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-23T14:31:51+00:00Added an answer on May 23, 2026 at 2:31 pm

    I find it surprising that you are calling [sheet1$] directly without referring to any linked server.

    Here are 2 approaches that are possible:

      Using Linked Server

  2. First create a linked server to access excel data as below:
  3. (I am assuming you have already have either JET or ACE provider for excel)

      exec sp_addLinkedServer @server='ExcelLnkdServr', 
                        @srvproduct='ACE 12.0', 
                        @provider='Microsoft.ACE.OLEDB.12.0', 
                        @datasrc='\\sysdev\loadExcel.xlsx',
                        @provstr='Excel 12.0;HDR=Yes';
    /*  If the above creation is successful, you can see a listing when use SP_LINKEDSERVERS in SSMS. Now you can query the sheet using order by as below: */
       select * from ExcelLnkdServr...[Sheet1$]                    
      order by 1 asc, 2 desc 

      Using Adhoc Queries

  4. You can OPENROWSET, OPENDATASOURCE or OPENQUERY as shown below: (Second option doesnt need any linked server creation )
  5.  select * from openquery(ExcelLnkdServr, 'SELECT * FROM [Sheet1$] order by 1 asc, 3 desc') 
    SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
        'Excel 12.0;Database=\\sysdev\loadExcel.xlsx;HDR=Yes','Select * from [Sheet1$] order by 1 asc') 
  • 0
  • Reply
  • Share
    Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp
    • Report

Sidebar

Related Questions

I am doing an import of data from excel spreadsheet to SQLServer 2005 via
The excel spreadsheet is connecting to SQL server 2005 using the connection string below:
I am trying to extract a table of values from an excel (2003) spreadsheet
I have an Excel spreadsheet that needs to display data from our SQL database.
I am trying to import a column of dates from a spreadsheet in Excel
Currently there is a Excel 2002 spreadsheet writing/retrieving data from a Access 2002 database.
How would I go about opening an ADO connection to an Excel 2007 spreadsheet?
I'm connecting to Excel with ADO, but it only works if I specify the
i connecting to a access database with php and adodb. Strings with characters like
Getting a weird problem when uploading a spreadsheet via C#. Here's an example of

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.