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

                    Working...
                    X