Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Feb 2004
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Export a Listbox

    Is there a way to export the contents of a listbox to say an Excel spreadsheet if it is not database driven?

    I have a dynamic listbox that allows people to sort it. Once they are done, I would like to give them the option to export it for offline use.

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Would it be possible to see this in action online? It'd be nice to get a picture of what you're talking about (because I'm having a bit of a hard time visualizing this..) so that we can offer a solution.

    Also, do you have PHP or ASP available on your webserver?

    Sadiq.

  • #3
    New Coder
    Join Date
    Jun 2004
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Quick Solution

    mqcarpenter,

    The simplest way is to write the content of the list to a comma delimited (CSV) file on the server.

    You can use server-side scripting (ASP,ColdFusion or PHP) to write the file then provide the end user a link to the file in a download/confirmation page.

    Excel will open the file without any problems

    For garbage collection, simple include code or schedule a task to clean the directory dependant upon the age of the file i.e. 1+ days old.


    If you explicitly need to create an Excel file, below is a link to an example of how to create an Excel spreadsheet using ASP. You could remove the ADO code and loop thru the listbox collection instead.


    CreateXLS_asp.txt


    MindCrafter
    www.mindcrafter.com

  • #4
    New Coder
    Join Date
    Feb 2004
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have PHP available. A csv would be fine, and allow a download would work. Is that a difficult thing to do?

    I do not have ASP available as an option.

  • #5
    New Coder
    Join Date
    Feb 2004
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sad69
    Would it be possible to see this in action online? It'd be nice to get a picture of what you're talking about (because I'm having a bit of a hard time visualizing this..) so that we can offer a solution.
    I do not think I have seen this before. I just want to be able to export a list so that it can be used offline by the person sorting it. Here is my listbox that is sortable:

    http://www.gruenebydesign.com/rotc/2...draft2004.html

    Also, do you have PHP or ASP available on your webserver?
    I have PHP, not ASP

  • #6
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh I see what you're saying now!

    Ok, check out this link:
    http://www.phpfreaks.com/tutorials/114/0.php

    I know it says it's for MySQL, but the idea is the same. Basically, you'd want to submit this page to a PHP script. There, you would parse the listbox (you may need all the elements in the listbox to be selected in order for them to all be submitted.. Javascript?) and create a comma or tab delimited string (not file) and then look at page 5 of the tutorial. See how it uses headers to tell the browser that this is an xls file? Do the same, and the print/echo your delimited string right after the headers.

    That should do it for you. Let me know if you run into any troubles.

    Sadiq.

  • #7
    New Coder
    Join Date
    Feb 2004
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have thought about this, and looked over the code, but this is not ideal. What I have is a listbox that is not populated from the db. I would rather keep it that way, mainly because I am unfamiliar with how to do this easily. I am not that PHP literate yet.

    The listbox feature is here: http://www.gruenebydesign.com/rotc/2...draft2004.html

    I just need a way to allow people to export the results in the right column, or allow them to view it in a separate window....

    Hmmm, maybe that is the direction I should head?

  • #8
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mqcarpenter
    I have thought about this, and looked over the code, but this is not ideal. What I have is a listbox that is not populated from the db. I would rather keep it that way, mainly because I am unfamiliar with how to do this easily.
    That's fine, you don't need to use a db, it's fine the way it is.

    Quote Originally Posted by mqcarpenter
    I am not that PHP literate yet.
    Well now's as good a time as any!

    Check out the attached file. Run it and analyze it as I'm pretty sure it does what you're wanting to achieve.

    Let me know how you make out, or if you have any questions.

    Sadiq.
    Attached Files Attached Files

  • #9
    New Coder
    Join Date
    Feb 2004
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sad, Thanks for the input.

    I tried modifying what you did to work with the existing form. It clears the form, but does not prompt a download or view. Remember that there are some external Javascripts to handle the istbox functionality.

    Any thoughts?

    Here is the form code:

    Code:
      <?php
     if(isset($_POST['submit'])) {
      $html_str = '';	
    	foreach($_POST['multi_sel'] as $option) {
    	 $html_str .= $option.'<br>';
    	}
    	
    	header("Content-type: application/octet-stream"); 
      header("Content-Disposition: attachment; filename=extraction.xls"); 
      header("Pragma: no-cache"); 
      header("Expires: 0"); 
      print $html_str;
     }
     else {
    ?>
    <script>
     function selectAll(_id) {
      var m_sel = document.getElementById(_id);
    	for(var i = 0; i < m_sel.options.length; i++) {
    	 m_sel.options[i].selected = true;
    	}
     }
    </script>
    <?php
     }//else
    ?>
                <SELECT NAME="list21" MULTIPLE SIZE=35 onDblClick="moveSelectedOptions(this.form['list21'],this.form['list11'],false)" method="post" action="<?php $_SERVER['PHP_SELF']; ?>" onsubmit="selectAll('multi_sel');">
     <select id="multi_sel" name="multi_sel[]" multiple="true">
     <input name="submit" value="submit" type="submit">
    			
    
    	</SELECT>
    	
    
    	</TD>
    </TR>
    </TABLE>
    
    </FORM>

  • #10
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, I think you've missed the point.. it wasn't going to be a direct copy+paste.. compare this new file (you should be able to use it directly.. let me know if it doesn't quite work out..) with what you did and my original file. Hopefully you can make some connections.

    One of the main issues is that your file has to have a .php extension, so the webserver knows to parse it with the PHP interpreter. BTW, you do have PHP installed, right?

    Let me know how this works out for you.

    Sadiq.
    Attached Files Attached Files

  • #11
    New Coder
    Join Date
    Feb 2004
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, I missed it. Thanks for looking out for me. It looks like there is an error in the "foreach" statement. It show up in the excel file when extracting. I will take a look and get back to you:

    http://www.gruenebydesign.com/rotc/2004/export02.php

    And yes, I have PHP, and had added the code into a PHP page.

  • #12
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well it looks like you got rid of the [] in the name of the select box. You have to keep it there or it won't work:
    <select ... NAME="list21[]" ...>

    Sadiq.

  • #13
    New Coder
    Join Date
    Feb 2004
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK I see what is going on. I had to add characters in front of your <BR> to see what it was doing. It is counting the options in list21, and printing the characters and breaks, not the content of the options.

    This is a great start though. I will look closely to see how to add the contents.

    http://www.gruenebydesign.com/rotc/2004/export02.php

  • #14
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Basically it passes the VALUE attribute of each option. If there's no value specified, it just treats it as an empty string.

    Looking at your select options, it doesn't look like you've specified any values. This might be a lot of work for you to copy+paste all the contents into the values, but that's probably the ONLY way you're going to get it to work.

    Actually, one thought comes to mind, but it would require altering your external Javascript file. Basically when it copies the option over from the left listbox to the right, when creating the option on the right it should assign newOption.value=oldOption.text, ontop of what it's already doing. It should be just a one-liner, but I haven't seen the code you're working with..

    Let me know how you make out.

    Sadiq.

  • #15
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, try using this external Javascript file instead, it should do the trick (so you don't have to put a value in for each option in listbox11).

    I changed line 158, if you're interested (or need to change it back).

    Let me know how it goes.

    Sadiq.
    Attached Files Attached Files


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •