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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:30:08+00:00 2026-05-15T05:30:08+00:00

I have a Excel Workbook that I use as a report template. I change

  • 0

I have a Excel Workbook that I use as a report template. I change the datasource on each pivot and datatable in a C# app. When I change the datatable datasource it tweeks the columns. Is there a way to force the column order?

private void RefreshRawData(string dataSource, string connection)
{
    xl._Worksheet ws = (xl._Worksheet)xlTemplate.Worksheets["Raw Data"];
    xl.ListObject table = ws.ListObjects["Table_ExternalData_1"];
    xl.QueryTable qt = table.QueryTable;
    qt.CommandText = dataSource;
    qt.Connection = GetExcelConnectionString((string)qt.Connection);
    qt.BackgroundQuery = false;
    qt.Refresh(m);
    Marshal.ReleaseComObject(ws);
    Marshal.ReleaseComObject(table);
    Marshal.ReleaseComObject(qt);
    ws = null;
    table = null;
    qt = null;
}
  • 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-15T05:30:09+00:00Added an answer on May 15, 2026 at 5:30 am

    Welcome to the wild world of Interop.

    I had to delete and recreate the table.

    Please take note to the arguments in the ListObject.Add function
    They are odd creatures.

    private void RefreshRawData(string dataSource, string connection)
    {
        xl._Worksheet ws = (xl._Worksheet)xlTemplate.Worksheets["Raw Data"];
        xl.ListObject table = ws.ListObjects["Table_ExternalData_1"];
        xl.QueryTable qt = table.QueryTable;
        string connStr = GetExcelConnectionString((string)qt.Connection);
        table.Delete();
        xl.Range r = ws.get_Range("A1", "A1");
        table = ws.ListObjects.Add(xl.XlListObjectSourceType.xlSrcExternal, (object)connStr, m, xl.XlYesNoGuess.xlGuess, r);
        qt = table.QueryTable;
        qt.CommandType = xl.XlCmdType.xlCmdSql;
        qt.CommandText = dataSource;
        qt.RowNumbers = false;
        qt.FillAdjacentFormulas = false;
        qt.PreserveFormatting = true;
        qt.RefreshOnFileOpen = false;
        qt.BackgroundQuery = true;
        qt.RefreshStyle = xl.XlCellInsertionMode.xlInsertDeleteCells;
        qt.SavePassword = false;
        qt.SaveData = true;
        qt.AdjustColumnWidth = true;
        qt.RefreshPeriod = 0;
        qt.PreserveColumnInfo = true;
        qt.ListObject.DisplayName = "Table_ExternalData_1";
        qt.Refresh(false);
        Marshal.ReleaseComObject(ws);
        Marshal.ReleaseComObject(table);
        Marshal.ReleaseComObject(qt);
        ws = null;
        table = null;
        qt = null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Excel workbook that I want to use as a template. It
I have an Excel 2003 workbook that contains a macro to copy certain of
I have a program that spits out an Excel workbook in Excel 2003 XML
I have an access 2007 Database that outputs a report in excel format, the
So I have an excel workbook that has a nice global map of shaperange
I have an excel 2003 vsto workbook that I would like to make available
I have an embedded PowerPoint presentation in an Excel workbook. How can I edit
I have around 25 worksheets in my workbook (Excel spreadsheet). Is there a way
I have Excel add-in which I add so many class modules that it is
We have an Excel 2002/XP based application that interacts with SQL 2000/5 to process

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.