Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to export ListGrid to Excel/CSV?

    How to export ListGrid to Excel/CSV?

    Can i have some sample code for that?

    I've seen SmartGWTEE code for Export, but it's not working.

    Nyone got the solution for this?

    Thanks,
    Leo..

    #2
    The CVS export in the Excel/CSV sample is working fine. If you're having issues, please mention the browser and OS you're using.

    Comment


      #3
      I'm using SmartGWT listgrid.
      Is it possible to export listgrid using same code available for SmartGWTEE?

      Thanks,
      Leo

      Comment


        #4
        The ability to export by just calling exportData is a feature of SmartGWT Pro or Enterprise only. See smartclient.com/product

        Comment


          #5
          Originally posted by leo82in
          I'm using SmartGWT listgrid.
          Is it possible to export listgrid using same code available for SmartGWTEE?

          Thanks,
          Leo
          Does the list you are trying to export contain all the records in client side or it's incomplete (for example when using a rest data source it could be incomplete)?

          I can help you with this.

          Comment


            #6
            Ok just a simple example that generates the CSV data from a listgrid into standard output on hosted mode, I hope you get the idea and adapt to your needs.

            Code:
            public void onModuleLoad() {
            	final ListGrid listGrid = new ListGrid();
            	listGrid.setFields(new ListGridField("Col1"), new ListGridField("Col2"), new ListGridField("Col3"));
            	Record[] data = new Record[5];
            	int counter = 0;
            	for (int i = 0; i < data.length; i++) {
            		data[i] = new ListGridRecord();
            		data[i].setAttribute("Col1", counter++);
            		data[i].setAttribute("Col2", counter++);
            		data[i].setAttribute("Col3", counter++);
            	}
            	listGrid.setData(data);
            	IButton button = new IButton("Export CSV");
            	button.addClickHandler(new ClickHandler() {
            		public void onClick(ClickEvent event) {
            			StringBuilder exportedCSV = exportCSV(listGrid);
            			System.out.println(exportedCSV);
            		}
            	});
            
            	VLayout layout = new VLayout();
            	layout.addMember(button);
            	layout.addMember(listGrid);
            	layout.draw();
            		
            }
            
            /**
             * Export data from a listrgrid
             * @param listGrid the {@link ListGrid}
             * @return a {@link StringBuilder} containing data in CSV format
             */
            private StringBuilder exportCSV(ListGrid listGrid) {
            	StringBuilder stringBuilder = new StringBuilder(); // csv data in here
            	
            	// column names
            	ListGridField[] fields = listGrid.getFields();
            	for (int i = 0; i < fields.length; i++) {
            		ListGridField listGridField = fields[i];
            		stringBuilder.append("\"");
            		stringBuilder.append(listGridField.getName());
            		stringBuilder.append("\",");
            	}
            	stringBuilder.deleteCharAt(stringBuilder.length() - 1); // remove last ","
            	stringBuilder.append("\n");
            	
            	// column data
            	ListGridRecord[] records = listGrid.getRecords();
            	for (int i = 0; i < records.length; i++) {
            		ListGridRecord listGridRecord = records[i];
            		ListGridField[] listGridFields = listGrid.getFields();
            		for (int j = 0; j < listGridFields.length; j++) {
            			ListGridField listGridField = listGridFields[j];
            			stringBuilder.append("\"");
            			stringBuilder.append(listGridRecord.getAttribute(listGridField.getName()));
            			stringBuilder.append("\",");
            		}
            		stringBuilder.deleteCharAt(stringBuilder.length() - 1); // remove last ","
            		stringBuilder.append("\n");
            	}
            	return stringBuilder;
            }

            Comment


              #7
              this method works fine.

              but how i can export in browser this CSV?

              thanks

              Originally posted by mihai007
              Ok just a simple example that generates the CSV data from a listgrid into standard output on hosted mode, I hope you get the idea and adapt to your needs.

              Code:
              public void onModuleLoad() {
              	final ListGrid listGrid = new ListGrid();
              	listGrid.setFields(new ListGridField("Col1"), new ListGridField("Col2"), new ListGridField("Col3"));
              	Record[] data = new Record[5];
              	int counter = 0;
              	for (int i = 0; i < data.length; i++) {
              		data[i] = new ListGridRecord();
              		data[i].setAttribute("Col1", counter++);
              		data[i].setAttribute("Col2", counter++);
              		data[i].setAttribute("Col3", counter++);
              	}
              	listGrid.setData(data);
              	IButton button = new IButton("Export CSV");
              	button.addClickHandler(new ClickHandler() {
              		public void onClick(ClickEvent event) {
              			StringBuilder exportedCSV = exportCSV(listGrid);
              			System.out.println(exportedCSV);
              		}
              	});
              
              	VLayout layout = new VLayout();
              	layout.addMember(button);
              	layout.addMember(listGrid);
              	layout.draw();
              		
              }
              
              /**
               * Export data from a listrgrid
               * @param listGrid the {@link ListGrid}
               * @return a {@link StringBuilder} containing data in CSV format
               */
              private StringBuilder exportCSV(ListGrid listGrid) {
              	StringBuilder stringBuilder = new StringBuilder(); // csv data in here
              	
              	// column names
              	ListGridField[] fields = listGrid.getFields();
              	for (int i = 0; i < fields.length; i++) {
              		ListGridField listGridField = fields[i];
              		stringBuilder.append("\"");
              		stringBuilder.append(listGridField.getName());
              		stringBuilder.append("\",");
              	}
              	stringBuilder.deleteCharAt(stringBuilder.length() - 1); // remove last ","
              	stringBuilder.append("\n");
              	
              	// column data
              	ListGridRecord[] records = listGrid.getRecords();
              	for (int i = 0; i < records.length; i++) {
              		ListGridRecord listGridRecord = records[i];
              		ListGridField[] listGridFields = listGrid.getFields();
              		for (int j = 0; j < listGridFields.length; j++) {
              			ListGridField listGridField = listGridFields[j];
              			stringBuilder.append("\"");
              			stringBuilder.append(listGridRecord.getAttribute(listGridField.getName()));
              			stringBuilder.append("\",");
              		}
              		stringBuilder.deleteCharAt(stringBuilder.length() - 1); // remove last ","
              		stringBuilder.append("\n");
              	}
              	return stringBuilder;
              }

              Comment


                #8
                How would I use this StringBuilder to export to CSV?

                Originally posted by mihai007
                Ok just a simple example that generates the CSV data from a listgrid into standard output on hosted mode, I hope you get the idea and adapt to your needs.

                Code:
                public void onModuleLoad() {
                	final ListGrid listGrid = new ListGrid();
                	listGrid.setFields(new ListGridField("Col1"), new ListGridField("Col2"), new ListGridField("Col3"));
                	Record[] data = new Record[5];
                	int counter = 0;
                	for (int i = 0; i < data.length; i++) {
                		data[i] = new ListGridRecord();
                		data[i].setAttribute("Col1", counter++);
                		data[i].setAttribute("Col2", counter++);
                		data[i].setAttribute("Col3", counter++);
                	}
                	listGrid.setData(data);
                	IButton button = new IButton("Export CSV");
                	button.addClickHandler(new ClickHandler() {
                		public void onClick(ClickEvent event) {
                			StringBuilder exportedCSV = exportCSV(listGrid);
                			System.out.println(exportedCSV);
                		}
                	});
                
                	VLayout layout = new VLayout();
                	layout.addMember(button);
                	layout.addMember(listGrid);
                	layout.draw();
                		
                }
                
                /**
                 * Export data from a listrgrid
                 * @param listGrid the {@link ListGrid}
                 * @return a {@link StringBuilder} containing data in CSV format
                 */
                private StringBuilder exportCSV(ListGrid listGrid) {
                	StringBuilder stringBuilder = new StringBuilder(); // csv data in here
                	
                	// column names
                	ListGridField[] fields = listGrid.getFields();
                	for (int i = 0; i < fields.length; i++) {
                		ListGridField listGridField = fields[i];
                		stringBuilder.append("\"");
                		stringBuilder.append(listGridField.getName());
                		stringBuilder.append("\",");
                	}
                	stringBuilder.deleteCharAt(stringBuilder.length() - 1); // remove last ","
                	stringBuilder.append("\n");
                	
                	// column data
                	ListGridRecord[] records = listGrid.getRecords();
                	for (int i = 0; i < records.length; i++) {
                		ListGridRecord listGridRecord = records[i];
                		ListGridField[] listGridFields = listGrid.getFields();
                		for (int j = 0; j < listGridFields.length; j++) {
                			ListGridField listGridField = listGridFields[j];
                			stringBuilder.append("\"");
                			stringBuilder.append(listGridRecord.getAttribute(listGridField.getName()));
                			stringBuilder.append("\",");
                		}
                		stringBuilder.deleteCharAt(stringBuilder.length() - 1); // remove last ","
                		stringBuilder.append("\n");
                	}
                	return stringBuilder;
                }

                Comment


                  #9
                  I have download issue that is not related to the data grid or any data issue. I want to be able to handle a download from a diffrent URL. I am currently using:

                  Window.open(url) or Location.assign(url) and it seem to work fine. However, I need to able to handle some logging as to if the file finised downloading. Any help would be appreciated.

                  Thanks,

                  Comment


                    #10
                    Hi ,

                    i'm working with LGPL version from smartgwt and i need the feature to export the ListGrid Data to Excel file . i heard it's only available for PRO and EE

                    is it possible to buy this futrue only ? and whom i can contact ?

                    Appreciate you fast response

                    Comment


                      #11
                      Hello,

                      in order to implement a listgrid export functionality from the front end, you can combine mihai007's example with a javascript method like the following: https://stackoverflow.com/questions/...-and-save-file which will be contained in an JSNI method (http://www.gwtproject.org/doc/latest...asicsJSNI.html).

                      i.e. on button click call the JSNI method which contains the javascript code from the above stackoverflow example that implements the save file operation.

                      Code:
                      ...
                         export(exportedCSV.toString(), "export.csv", "text/csv");
                      ...
                      
                          public static native void export(String data, String filename, String type) /*-{
                              var file = new Blob([ data ], {
                                  type : type
                              });
                              var a = document.createElement("a"), url = URL.createObjectURL(file);
                              a.href = url;
                              a.download = filename;
                              $doc.body.appendChild(a);
                              a.click();
                              setTimeout(function() {
                                  $doc.body.removeChild(a);
                                  window.URL.revokeObjectURL(url);
                              }, 0);
                      
                          }-*/;

                      Comment

                      Working...
                      X