سؤال

I am using GXT. I have a Grid.

com.sencha.gxt.widget.core.client.grid.Grid<RiaBean>  grid;

I want to export this to excel file.

i dont want to use external Jar files.

Can any body Help.

هل كانت مفيدة؟

المحلول

Your Grid is composed of ColumnConfig<RiaBean,?>.

Every ColumnConfig<RiaBean,?> is linked to a ValueProvider<RiaBean,?>. Every ValueProvider<RiaBean,?> contains a methodgetPath() which is intended to return the path of the displayed elements.

Hence, you can easily get the paths of your displayed elements, send them to the server and get back the value by Introspection or EL.

For example, let's take this class

public class RIABean{
  private String a;
  private Integer b;
  private Boolean c;
  private Integer idFoo;
}

Use an interface which extends PropertyAccess to easily define your ValueProviders. It will also generate the methods getPath() with the accurate value.

public interface RIABeanPropertyAccess extends PropertyAccess<RIABean>{
  //The generated getPath() method returns "a"
  ValueProvider<RIABean,String> a();
  //The generated getPath() method returns "b"
  ValueProvider<RIABean,Integer> b();
  //The generated getPath() method returns "c"
  ValueProvider<RIABean, Boolean> c();
  //The generated getPath() method returns "foo.id"
  @Path("foo.id")
  ValueProvider<RIABean, Integer> idFoo();
}

Create the ColumnModel for your grid:

RIABeanPropertyAccess pa=GWT.create(RIABean.class);
List<ColumnConfig<RIABean,?>> listCols=new ArrayList<ColumnConfig<RIABean,?>>();
listCols.add(new ColumnConfig(pa.a(),100,"Header text for column A");
listCols.add(new ColumnConfig(pa.b(),100,"Header text for column B");
listCols.add(new ColumnConfig(pa.c(),100,"Header text for column C");
ColumnModel colModel=new ColumnModel(listCols);

When the user clicks on "export" button, just iterate on the list of ColumnConfig<RiaBean,?> of your grid in order to get the Path of each of them and send this list of paths to the server. The server might then use introspection/reflexion/EL to get the values corresponding to each path.

There is no way to generate the file on client side. As the server must do it, it is the easiest way I know and that's what we do in my team.

Finally, ask yourself if you really need an excel file or if a csv file would be enough. The csv file can easily be done without any library and can be opened with Excel.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top