0

Listbox - Exporting data to CSV excel format

asked 2009-06-15 15:14:38 +0800

genildof gravatar image genildof
9 1 1 1
http://br.linkedin.com/in...

Hi,

I was looking on ZK forum and on documentation a way to export data from listbox to the csv format. I did not find much information about it, so I just implemented something that solved my problem, I am sharing below a simplified version. As the JSP Display Tag component, it would be interesting if the listbox embedded the exporting functionality on future versions.

<?xml version="1.0" encoding="UTF-8"?>
<zk xmlns="http://www.zkoss.org/2005/zul">
  <window title="Simple listbox content exporting test"  width="250px" border="normal">
    <zscript>
  		String[] data = new String[30];
  		for(int j=0; j < data.length; ++j) {
  			data = "option " + j;
  		}
  		ListModel strset = new SimpleListModel(data);

      public static void export_to_csv(Listbox listbox) {
        String s = ";";
        StringBuffer sb = new StringBuffer();

        for (Object head : listbox.getHeads()) {
          String h = "";
          for (Object header : ((Listhead) head).getChildren()) {
            h += ((Listheader) header).getLabel() + s;
          }
          sb.append(h + "\n");
        }
        for (Object item : listbox.getItems()) {
          String i = "";
          for (Object cell : ((Listitem) item).getChildren()) {
            i += ((Listcell) cell).getLabel() + s;
          }
          sb.append(i + "\n");
        }
        Filedownload.save(sb.toString().getBytes(), "text/plain", "gf_dot_analyst_at_gmail_dot_com.csv");
      }
    </zscript>

    <listbox id="list" rows="10" model="${strset}">
      <listhead>
        <listheader label="Options" sort="auto"/>
        <listheader label="Values" sort="auto"/>
      </listhead>
    </listbox>
    <button label="Export to csv" onClick="export_to_csv(list)"/>
  </window>
</zk>


Tks to ZK team,

Genildo Ferreira
Brazil.

delete flag offensive retag edit

19 Replies

Sort by ยป oldest newest

answered 2009-06-16 01:13:57 +0800

jumperchen gravatar image jumperchen
3909 2 8
http://jumperchen.blogspo... ZK Team

Hi,

Thanks for the sharing.
You can also post it to ZK Feature.

/Jumper

link publish delete flag offensive edit

answered 2009-06-16 15:15:22 +0800

genildof gravatar image genildof
9 1 1 1
http://br.linkedin.com/in...

Posted to feature request (ID 2807216).
Thanks for your attention,

Genildo.

link publish delete flag offensive edit

answered 2009-07-13 13:10:29 +0800

hamroune gravatar image hamroune
60

Hi,
it works fine but when the mold is "paging", it exports the firts items only.
thx

link publish delete flag offensive edit

answered 2009-07-16 06:58:32 +0800

hamroune gravatar image hamroune
60

hi,
is there any idea to resolve this?
thanks a lot

link publish delete flag offensive edit

answered 2009-07-19 23:34:24 +0800

genildof gravatar image genildof
9 1 1 1
http://br.linkedin.com/in...

Hi hamroune,
this solution works for the default mode only. In paging mode, you have to go through each page so that the items are rendered. If you are using databinding/live data, you can export the model of the listbox by changing few lines of the above code.

link publish delete flag offensive edit

answered 2009-07-21 11:23:38 +0800

hamroune gravatar image hamroune
60

hi genildof,
can you share few lines, I have'nt issue, of course I can do this by using List<JavaBean>,

but I have not find a generic method availible for all listbox independant from the model.

Thkx

link publish delete flag offensive edit

answered 2009-07-22 11:38:04 +0800

sousa1981 gravatar image sousa1981
573 4

My comments at https://sourceforge.net/tracker/?func=detail&aid=2807216&group_id=152762&atid=785194

link publish delete flag offensive edit

answered 2009-07-22 11:43:04 +0800

sousa1981 gravatar image sousa1981
573 4

+1

The CSV Specification can be found at
http://en.wikipedia.org/wiki/Comma-separated_values

There is a couple of framework which already implement the specification,
please see http://www.csvobjects.org/

It would be fine to have like paging an toolbox "Export Options": CSV
EXCEL XML

The toolbox can be hidden or not simple as:

<listbox id="list" export="csv, excel, xml">

So it mean default empty <listbox id="list" export=""> is to not show the
export toolbox

Regards

link publish delete flag offensive edit

answered 2009-07-22 18:33:06 +0800

robertpic71 gravatar image robertpic71
1275 1

>> but I have not find a generic method availible for all listbox independant from the model.
Maybe genildof means use reflection for the model (i.e. use apache bean utils). This could be a generic way for the model.
This could be problem. i.e. the list containts person with first- and lastname, the modelbean containts many fields...

Another approach could be (not tested):
If Model
no --> your code
yes --> getModel, get the ListitemRenderer
use the ListitemRender to convert the model to the listitem --> work with your code

something like this

for(Object bean : model) {
Listitem listitem = new Listitem():
lisitemRender.render(listitem, bean);
// yourcode
}

However, i would prefer a popup like a print dialog: export the actual page or all pages....

/Robert

link publish delete flag offensive edit

answered 2010-03-10 06:11:33 +0800

heruprayugo gravatar image heruprayugo
21
heruprayugo.wordpress.com

Thanks very much for your sharing...
helpful for me.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-06-15 15:14:38 +0800

Seen: 6,941 times

Last updated: May 24 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More