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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:19:27+00:00 2026-06-15T19:19:27+00:00

I like to create excel file from customer table of north wind project. i

  • 0

I like to create excel file from customer table of north wind project. i have used below code for that,i like to know how i have to save excel file and where it will be saved.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            XNamespace ns = "urn:schemas-microsoft-com:office:spreadsheet";
            XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
            XNamespace x = "urn:schemas-microsoft-com:office:excel";
            XNamespace x2 = "http://schemas.microsoft.com/office/excel/2003/xml";
            XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet";
            XNamespace o = "urn:schemas-microsoft-com:office:office";
            XNamespace html = "http://www.w3.org/TR/REC-html40";
            XNamespace c = "urn:schemas-microsoft-com:office:component:spreadsheet";
             DataClasses1DataContext _DataContext;
            _DataContext = new DataClasses1DataContext();

            // Linq to XML - Document
            XDocument doc = new XDocument(
                new XDeclaration("1.0", "UTF-8", string.Empty),
                new XProcessingInstruction("mso-application", "progid=\"Excel.Sheet\""),
                new XElement(ns + "Workbook",
                    new XAttribute("xmlns", ns.NamespaceName),
                    new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
                    new XAttribute(XNamespace.Xmlns + "x", x.NamespaceName),
                    new XAttribute(XNamespace.Xmlns + "x2", x2.NamespaceName),
                    new XAttribute(XNamespace.Xmlns + "ss", ss.NamespaceName),
                    new XAttribute(XNamespace.Xmlns + "o", o.NamespaceName),
                    new XAttribute(XNamespace.Xmlns + "html", html.NamespaceName),
                    new XAttribute(XNamespace.Xmlns + "c", c.NamespaceName),
                    new XElement(o + "OfficeDocumentSettings",
                        new XAttribute("xmlns", o.NamespaceName)),
                    new XElement(x + "ExcelWorkbook",
                        new XAttribute("xmlns", x.NamespaceName)),
                    new XElement("Worksheet",
                        new XAttribute(ss + "Name", "Sheet1"),
                        new XElement("Table", // 1st Table
                            new XElement("Row", // First Row
                                new XElement("Cell", // First Cell on First Row
                                    new XElement("Data", new XAttribute(ss + "Type", "String"), "CompanyName") // Data in Cell A1
                                ),
                                new XElement("Cell",
                                    new XElement("Data", new XAttribute(ss + "Type", "String"), "ContactTitle") // Data in Cell B1
                               ),
                                new XElement("Cell",
                                    new XElement("Data", new XAttribute(ss + "Type", "String"), "Country") // Data in Cell C1
                                )


                            )
                        )
                    )
                )
            );

            // Loop through a collection. Each iteration is a new row
            foreach (Customer customer in _DataContext.Customers)
            {
                // Linq to XML - Data
                doc.Descendants("Row").First().AddAfterSelf(
                    new XElement("Row",
                        new XElement("Cell", // First Cell on First Row
                                    new XElement("Data", new XAttribute(ss + "Type", "String"), customer.CompanyName) // Data in Cell A1
                                ),
                                new XElement("Cell",
                                    new XElement("Data", new XAttribute(ss + "Type", "String"), customer.ContactTitle) // Data in Cell B1
                               ),
                                new XElement("Cell",
                                    new XElement("Data", new XAttribute(ss + "Type", "String"), customer.Country) // Data in Cell C1
                                )
                    )
                );
            }
            // Namespace fix. Deletes any empty xmlns="" text in every node.
            foreach (XElement e in doc.Root.DescendantsAndSelf())
            {
                if (e.Name.Namespace == string.Empty)
                {
                    e.Name = ns + e.Name.LocalName;
                }
            }
            doc.Save("e:\ahmed.xml");

        }
    }
}
  • 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-06-15T19:19:28+00:00Added an answer on June 15, 2026 at 7:19 pm

    So, you want to save the XML data in Excel sheet. See the following code . You have to reference the Excel Libaray first.

     object misValue = System.Reflection.Missing.Value;            
     Excel.Application myApp=new Excel.Application() ;
     Excel.Workbook myWbk = myApp.Workbooks.Add(misValue);
     Excel.Worksheet myWst = (Excel.Worksheet)myWbk.Worksheets.get_Item(1);
    
     //Load your XML
     DataSet ds = new DataSet();
     XmlReader xmlFile ;
     xmlFile = XmlReader.Create("e:\ahmed.xml", new XmlReaderSettings());
     ds.ReadXml(xmlFile);
    
     //Write your XML to Excel
     int i = 0;
     int j = 0;
     for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
           for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
               {
                  myWst.Cells[i + 1, j + 1] = ds.Tables[0].Rows[i].ItemArray[j].ToString();
               }
           }
    

    Link: http://www.codeproject.com/Tips/371595/Generate-Excel-from-XML-and-Generate-XML-from-Exce

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

Sidebar

Related Questions

Suppose I have a table like: create table { id numeric(5,3), code varchar(10) }
I am trying to create a CSV file from a dynamically generated table.I have
I have a project where I read values from a file. I'd like to
If I have a table like CREATE TABLE [FooTable]( [foo] [varchar](50) NOT NULL, [foo_boo]
I have Created a table like CREATE TABLE [dbo].[tab1]( [Id] [int] NOT NULL, [Name]
I have a hierarchical table on Oracle pl/sql. something like: create table hierarchical (
How do i quickly create CVS (Comma Delimited File Excel) file from DB sp?
Possible Duplicate: Create Excel (.XLS and .XLSX) file from C# Rounding double values in
How reliable and powerful is jxl? I'm trying to create an excel file from
I have read a cvs file and I would like to create a dictionary

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.