Go Back   SmartClient Forums > Smart GWT Technical Q&A
Wiki Register Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 27th Jul 2009, 03:27
leo82in leo82in is offline
Registered Developer
 
Join Date: Jul 2009
Posts: 11
Default 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..
Reply With Quote
  #2  
Old 27th Jul 2009, 04:35
smartgwt.dev smartgwt.dev is offline
Registered Developer
 
Join Date: Nov 2008
Posts: 1,091
Default

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.
Reply With Quote
  #3  
Old 28th Jul 2009, 02:46
leo82in leo82in is offline
Registered Developer
 
Join Date: Jul 2009
Posts: 11
Default

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

Thanks,
Leo
Reply With Quote
  #4  
Old 28th Jul 2009, 03:36
Isomorphic Isomorphic is online now
Administrator
 
Join Date: May 2006
Posts: 35,695
Default

The ability to export by just calling exportData is a feature of SmartGWT Pro or Enterprise only. See smartclient.com/product
Reply With Quote
  #5  
Old 28th Jul 2009, 05:07
mihai007 mihai007 is offline
Registered Developer
 
Join Date: Dec 2008
Posts: 273
Default

Quote:
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.
Reply With Quote
  #6  
Old 28th Jul 2009, 05:46
mihai007 mihai007 is offline
Registered Developer
 
Join Date: Dec 2008
Posts: 273
Default

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;
}
Reply With Quote
  #7  
Old 29th Jun 2010, 12:35
diegocmsantos diegocmsantos is offline
Registered Developer
 
Join Date: Jun 2010
Posts: 1
Default

this method works fine.

but how i can export in browser this CSV?

thanks

Quote:
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;
}
Reply With Quote
  #8  
Old 3rd Nov 2010, 14:31
lchen lchen is offline
Registered Developer
 
Join Date: Sep 2009
Posts: 168
Default

How would I use this StringBuilder to export to CSV?

Quote:
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;
}
Reply With Quote
  #9  
Old 13th Dec 2010, 11:14
ttran ttran is offline
Registered Developer
 
Join Date: Jun 2010
Posts: 144
Default

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,
Reply With Quote
  #10  
Old 13th Sep 2011, 04:20
aelshal aelshal is offline
Registered Developer
 
Join Date: Nov 2010
Posts: 10
Default

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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search


© 2010,2011 Isomorphic Software. All Rights Reserved