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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:14:38+00:00 2026-05-27T15:14:38+00:00

How can i export my data from SQL server 2008 into Excel 2010 or

  • 0

How can i export my data from SQL server 2008 into Excel 2010 or later ?

i have tried on SQL way:

sp_configure 'show advanced options', 0;
GO
RECONFIGURE;
GO
sp_configure 'Ad Hoc Distributed Queries', 0;
GO
RECONFIGURE;
GO
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
'Excel 8.0;Database=C:\testing.xls;Extended Properties=EXCEL 12.0;HDR=YES', 
'SELECT NO_ORDRE, Date FROM [Sheet1$]') 
SELECT [NO_ORDRE], GETDATE() FROM ORDRE
GO

Unfortuntely i receive error:
The OLE DB provider ‘Microsoft.Jet.OLEDB.4.0’ can not be used for distributed queries because the provider is configured to run in STA mode.

and then i tried on C# way:

 public class ExportToExcel
    {
        private Excel.Application app;

        private Excel.Workbook workbook;
        private Excel.Worksheet previousWorksheet;
       // private Excel.Range workSheet_range;
        private string folder;

        public ExportToExcel(string folder)
        {

            this.folder = folder;
            this.app = null;
            this.workbook = null;
            this.previousWorksheet = null;
           // this.workSheet_range = null;

            createDoc();
        }

        private void createDoc()
        {
            try
            {
                app = new Excel.Application();
                app.Visible = false;
                workbook = app.Workbooks.Add(1);
            }
            catch (Exception excThrown)
            {
                throw new Exception(excThrown.Message);
            }
            finally
            {
            }
        }

        public void shutDown()
        {
            try
            {
                workbook = null;
                app.Quit();
            }
            catch (Exception excThrown)
            {
                throw new Exception(excThrown.Message);
            }
            finally
            {
            }
        }

        public void ExportTable(string query, string sheetName)
        {
            SqlDataReader myReader = null;
            try
            {
                using (var connectionWrapper = new Connexion())
                {
                    var connectedConnection = connectionWrapper.GetConnected();
                    Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets.Add(Missing.Value, Missing.Value, 1, Excel.XlSheetType.xlWorksheet);

                    worksheet.Name = sheetName;
                    previousWorksheet = worksheet;

                    SqlCommand myCommand = new SqlCommand(query, connectionWrapper.conn);

                    myReader = myCommand.ExecuteReader();

                    int columnCount = myReader.FieldCount;

                    for (int n = 0; n < columnCount; n++)
                    {
                        //Console.Write(myReader.GetName(n) + "\t");
                        createHeaders(worksheet, 1, n + 1, myReader.GetName(n));
                    }

                    int rowCounter = 2;
                    while (myReader.Read())
                    {
                        for (int n = 0; n < columnCount; n++)
                        {
                            //Console.WriteLine();
                            //Console.Write(myReader[myReader.GetName(n)].ToString() + "\t");
                            addData(worksheet, rowCounter, n + 1, myReader[myReader.GetName(n)].ToString());
                        }
                        rowCounter++;
                    }

                }
            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());

            }
            finally
            {
                if (myReader != null && !myReader.IsClosed)
                {
                    myReader.Close();
                }   
                myReader = null;
            }

        }

        public void createHeaders(Excel.Worksheet worksheet, int row, int col, string htext)
        {
            worksheet.Cells[row, col] = htext;
        }

        public void addData(Excel.Worksheet worksheet, int row, int col, string data)
        {
            worksheet.Cells[row, col] = data;
        }

        public void SaveWorkbook()
        {

            String folderPath = "C:\\My Files\\" + this.folder;

            if (!System.IO.Directory.Exists(folderPath))
            {
                System.IO.Directory.CreateDirectory(folderPath);
            }

            string fileNameBase = "db";
            String fileName = fileNameBase;
            string ext = ".xlsx";
            int counter = 1;

            while (System.IO.File.Exists(folderPath + fileName + ext))
            {
                fileName = fileNameBase + counter;
                counter++;
            }

            fileName = fileName + ext;

            string filePath = folderPath + fileName;

            try
            {
                workbook.SaveAs(filePath, Excel.XlFileFormat.xlWorkbookDefault, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());

            }
        }



    }

unfortunately i got error:
Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

Any idea how can i export SQL to Excel ?

  • 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-27T15:14:39+00:00Added an answer on May 27, 2026 at 3:14 pm

    Your best bet might be to just write it out to a CSV. Excel registers itself as the file handler for CSV files, so it will open in excel by default.

    For example:

    private void SQLToCSV(string query, string Filename)
    {
    
        SqlConnection conn = new SqlConnection(connection);
        conn.Open();
        SqlCommand cmd = new SqlCommand(query, conn);
        SqlDataReader dr = cmd.ExecuteReader();
    
        using (System.IO.StreamWriter fs = new System.IO.StreamWriter(Filename))
        {
            // Loop through the fields and add headers
            for (int i = 0; i < dr.FieldCount; i++)
            {
                string name = dr.GetName(i);
                if (name.Contains(","))
                    name = "\"" + name + "\"";
    
                fs.Write(name + ",");
            }
            fs.WriteLine();
    
            // Loop through the rows and output the data
            while (dr.Read())
            {
                for (int i = 0; i < dr.FieldCount; i++)
                {
                    string value = dr[i].ToString();
                    if (value.Contains(","))
                        value = "\"" + value + "\"";
    
                    fs.Write(value + ",");
                }
                fs.WriteLine();
            }
    
            fs.Close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the easiest way to export data to excel from SQL Server 2000. I
(ANSWER) How to export some Geography data from a Microsoft Sql Server 2008. You'll
I have an SSIS package set up to export data from a SQL Server
I use SQL Server Import and Export Wizard to transfer data from a remote
I'm exporting data programatically from Excel to SQL Server 2005 using SqlBulkCopy. It works
i am preparing myself for importing data from excel sheet to sql server 2005.I
I ran into a issue about Import/Export on SQL Server. I have a database
I have almost 100MB of example.sql file. (Actually data export file from another database)
I have a SQL Server database, i need to export all of the data
Any idea how to go about formatting the data so I can export from

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.