Export a file with wicket 1.5.3

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Export a file with wicket 1.5.3

lacribeiro11
Hi everyone,

I am working with wicket 1.5.3 and I am trying to export a csv file, but since i update to this version. My code isn't working:

                        WebResponse response = (WebResponse) getResponse();
                        response.setAttachmentHeader("file.csv");
                        response.setContentType("text/csv");

                        OutputStream out = response.getOutputStream();// it doesn't recognises any more getOutputStream
                                 
                                  //organizer is a object with the data
                                  PrintWriter writer = new PrintWriter(out);
                        writer.write(organizer.toCSV());
                        writer.close();

There is an example to do the same in this version?
Thank you very much for your help

With the best regards
Luis
Reply | Threaded
Open this post in threaded view
|

Re: Export a file with wicket 1.5.3

nhsoft.yhw
here is my code to export excel file

final Link lnkExport = new Link("export") {

                                public void onClick() {
                                       
                                        getRequestCycle().scheduleRequestHandlerAfterCurrent(new IRequestHandler() {
                       
                                                @Override
                                                public void detach(IRequestCycle requestCycle) {
                                                       
                                                }

                                                @Override
                                                public void respond(IRequestCycle requestCycle) {
                                                        try {
                                                                JxlExportOrderList jxlExportOrderList = new JxlExportOrderList(dataProvider.getOrderIds(), coOrderService);
                                                                String fileName = String.format("118114life_%s.xls", new Object[]{DateUtils.format(Calendar.getInstance().getTime(), "yyMMddHHmm")});
                                                               
                                                                HttpServletResponse httpResponse = (HttpServletResponse)requestCycle.getResponse().getContainerResponse();
                                                                httpResponse.setContentType( "application/vnd.ms-excel" );
                                                                httpResponse.setHeader("Content-disposition", "attachment; filename=" + fileName );
                                                                jxlExportOrderList.exportToExcel(httpResponse.getOutputStream());
                                                        } catch (Exception e) {
                                                                throw new RuntimeException(e);
                                                        }
                                                }
                                        });
                                }
                        };
                        add(lnkExport);
my first wicket application: www.118114life.com
Reply | Threaded
Open this post in threaded view
|

Re: Export a file with wicket 1.5.3

lacribeiro11
Hi
thank you very much :)
I was playing around with the code and I found a work around as well.
Here I have a code sample, where I will export a translation table

        private class TranslationExport extends Link<Void>{
                private static final long serialVersionUID = 1L;

                public TranslationExport(String id) {
                        super(id);
                        // TODO Auto-generated constructor stub
                }

                @Override
                public void onClick() {
                        // This object will have the list of translation
                        TranslationOrganizer organizer = new TranslationOrganizer (translationRepository.findAll());
                       

                        // Create file and send it
                        WebResponse response = (WebResponse) getResponse();
                        response.setAttachmentHeader("Translation.csv");
                        response.setContentType("text/csv");
                        response.write(organizer.toCSV());
                       
                       
                }
               
        }

public class TranslationOrganizer {

        private ArrayList<TranslationExport> translationExports = new ArrayList<TranslationExport>();
public String toCSV() {
                String csv = "basename;";
                for(Languages l : Languages.values())
                        csv+=l.getLocale()+";";
                csv+="tableindex;tablename\n";
               
                for(TranslationExport tr : translationExports)
                        csv+=tr.toString();
                return csv;
        }
}

with this code I can export values from a database to a file and save it.
It took I long time to reach it, but is a work around.
I will also try your code.
Once more thank you very much
Luis