Announcement

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

  • leo82in
    started a topic How to export ListGrid to Excel/CSV?

    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..

  • vagkavan
    replied
    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);
    
        }-*/;

    Leave a comment:


  • aelshal
    replied
    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

    Leave a comment:


  • ttran
    replied
    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,

    Leave a comment:


  • lchen
    replied
    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;
    }

    Leave a comment:


  • diegocmsantos
    replied
    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;
    }

    Leave a comment:


  • mihai007
    replied
    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;
    }

    Leave a comment:


  • mihai007
    replied
    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.

    Leave a comment:


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

    Leave a comment:


  • leo82in
    replied
    I'm using SmartGWT listgrid.
    Is it possible to export listgrid using same code available for SmartGWTEE?

    Thanks,
    Leo

    Leave a comment:


  • smartgwt.dev
    replied
    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.

    Leave a comment:

Working...
X