How to read self generated XML and export it to Excel

Last post 07-01-2009 7:42 AM by kavita_khandhadia. 6 replies.

Sort Posts:

  • How to read self generated XML and export it to Excel

    06-30-2009, 5:52 AM

    Hey!

    For my "ajaxed" VB.NET Website I am using the xElement to create (with LinQ) a xml - response like that: 

        
        Public Function xmlLs(ByVal userID As Integer, ByVal date As Date) As XElement
            Return New XElement("lsXml", _
            From ls In dcBrix.T_LS _
                    Where ls.LS_USER_ID = userID And ls.LS_DATE = date _
                    Order By ls.LS_ID Descending _
                            Select New XElement("ls", _
                                            New XElement("lsNr", ls.LS_NUMBER), _
                                            New XElement("lsDate", ls.LS_DATE), _
                                            New XElement("lsCount", ls.LS_HOURS) _
                            ) _
            )
        End Function

     

    result:

    <?xml version='1.0' encoding='UTF-8' ?>
    <lsXml>
       <ls>
          <lsNr>128907325105351250</lsNr>
          <lsDate>15.11.2008</lsDate>
          <lsCount>1.50</lsCount>
       </ls>
        ....
    </lsXml>
    

    This works great!

    Now the problem:
    I have to export the xml-date to Excel (xls, csv).
    How can I read my own generated xml and then make the dialogbox to download the data as an Excel-spreadsheet?

    Thank you very much for any help!


     

  • Re: How to read self generated XML and export it to Excel

    06-30-2009, 8:16 AM

    This link might help u...

    http://www.dotnetspider.com/resources/17115-Excel-Sheet-XML-Operations.aspx


    Please mark this post as Answer if it is of help to you!

    " Every wall is a door..! "
  • Re: How to read self generated XML and export it to Excel

    06-30-2009, 9:07 AM

    Hi! Thank you very much.
    I think here they talk about: "This code is used to import excel sheet to database and some Excel sheet functions".

    I hope - if there is a way to load my XML into a XmlReader or a XmlDocument - the export with a XSL-File shouldn't be the big problem.
    It must be something like this:

    ds.ReadXml(Server.MapPath("Authors.xml"))

    But instead of the file Authors.xml should be the link that gives back the self generated XML-String.
    Do you know how this could run?

     

    Thanks in advance!

  • Re: How to read self generated XML and export it to Excel

    06-30-2009, 10:01 AM
    Answer

    As in ur first post u have asked that u want to create an Excel Sheet from XML (if m wrong then correct me) . I have provided u the link here u can see the method named BuildExcel ()..

    I have tried it my self this method

    public static string BuildExcel()
        {
            XmlDocument doc = new XmlDocument();
        

            doc.Load("D:\\Kavita\\PROJECTS\\GTTIntegratedBookingEngine\\WebServiceInvocationDemo\\XMLFile8.xml");

            string xslPath = "D:\\Kavita\\PROJECTS\\GTTIntegratedBookingEngine\\WebServiceInvocationDemo\\XSLTFile.xsl";
            try
            {
                //XmlDocument doc = new XmlDocument();
                //doc.LoadXml(ds.GetXml());
                System.Xml.Xsl.XslTransform xslt = new XslTransform();
                xslt.Load(xslPath);
                XmlElement root = doc.DocumentElement;
                XPathNavigator nav = root.CreateNavigator();
                //string _path = xslPath.Substring(0, xslPath.Length - 4);
                string xlsPath = "D:\\Kavita\\xmlSheet.xls";
                XmlWriter writer = new XmlTextWriter(xlsPath, Encoding.UTF8);
                xslt.Transform(nav, null, writer, null);
                writer.Close();
                return xlsPath;


            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    it works. When I view the XSLTFile.xls sheet .. i see my data... though i see some junk characters too.. I will work on it after u tell me if this is wot u are looking for?


    Please mark this post as Answer if it is of help to you!

    " Every wall is a door..! "
  • Re: How to read self generated XML and export it to Excel

    06-30-2009, 10:12 AM
    Answer
    • All-Star
      59,891 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,550
    • TrustedFriends-MVPs
  • Re: How to read self generated XML and export it to Excel

    07-01-2009, 7:31 AM
    Answer

    Hi Kavita!

    You are right. Thank you. But in this post there are those fixed files. I dont have a fixed path for the XML-file.

    kavita_khandhadia:
            doc.Load("D:\\Kavita\\PROJECTS\\GTTIntegratedBookingEngine\\WebServiceInvocationDemo\\XMLFile8.xml");
    string xslPath = "D:\\Kavita\\PROJECTS\\GTTIntegratedBookingEngine\\WebServiceInvocationDemo\\XSLTFile.xsl";
    string xlsPath = "D:\\Kavita\\xmlSheet.xls";
     

    The only fixed file should be the XSLTFile.xsl.
    But I think I got a way:

            Dim ds As DataSet = New DataSet()
            Dim dt As DataTable
            Dim xmlRdr As System.Xml.XmlReader
            Dim uri As New Uri("http://" & Request.ServerVariables("HTTP_HOST") & "/service/...)
            Response.Clear()
            Response.ContentType = "application/vnd.ms-excel"
            Response.AddHeader("Content-Disposition", "attachment; filename=brix.xls")
            Response.Charset = ""
            Response.Buffer = True
    
            xmlRdr = System.Xml.XmlReader.Create(uri.ToString)
            ds.ReadXml(xmlRdr)
            dt = ds.Tables(0)
            Dim xdd As System.Xml.XmlDataDocument = New System.Xml.XmlDataDocument(ds)
            Dim xt As System.Xml.Xsl.XslCompiledTransform = New System.Xml.Xsl.XslCompiledTransform()
            xt.Load(Server.MapPath("Excel.xslt"))
            xt.Transform(xdd, Nothing, Response.OutputStream)
    
            Response.End()

    Hi Mudassar,
    a real cool post! - Thanks a lot.

    Last - maybe stupid - question:
    Where do you get a XSLTFile for (standard-) converting to Excel? 

     

  • Re: How to read self generated XML and export it to Excel

    07-01-2009, 7:42 AM

    Ya i know..i wrote it for an example ..u can also load the XmlDocument doc dynamically.. i wont explain furthure as i think u have got ur answer..please mark as answer if it did....

    Cheers :)

    Please mark this post as Answer if it is of help to you!

    " Every wall is a door..! "
Page 1 of 1 (7 items)