smartGWT, OSGi export to excel and PDF
2012-01-09 01:34
435 查看
刚给公司一个web项目添加了前台export表格到excel以及pdf的功能,下面把实现过程记录在这篇blog中。
一些背景交待:
1、UI技术使用的是smartgwt,MVP模式,表格ListGrid使用的是DataSource
2、整个项目基于OSGi,下面提到的ExportService接口和ExportServiceImpl类不在同一个OSGi bundle中,原因是ExportService需要向几乎所有bundle开放,所以放到了common的bundle中。
第一步,写一个ExportForm
注意:exportButton.setStartRow(false);是必要的,因为smartgwt中有些控件,比如Button,默认会另起一行,不管setNumCol(int)设置为多大。
第二步,把这个form控件加到合适的地方。(省略ExportService接口, ExportServiceImpl类, 以及Export单例类的实现,有些代码下面有引用到)
第三步,在ListGrid所在的View对应的Presenter中添加exportButton的ClickHandler处理。当用户点击这个按钮,Presenter就会new一次DataSource(因为export需要export所有的数据,而ListGrid的display只需要一页的数据,所以不能直接从ListGrid export),设置好所有的参数,然后fetch data。将response.getData(), ListGrid.getFields(), 用户选择export的format, 以及fileName传给ExportService.
这样注册之后,每当前台call这个Export,就会找一个GWT.getModuleBaseURL() + value()的url,即http://....../发出申请的gwt project的名称/export。上面笔者提到这个service是被share的,任何一个gwt project都会call到这个service,因此需要使用第二种注册方法。
2、使用((ServiceDefTarget) exportService).setServiceEntryPoint("/.../export");这样注册之后,每当前台call这个Export,都会使用固定的URL。
接下来在ExportServiceImpl所在的项目的plugin.xml中注册export service的implementation。
第五步,在ExportServiceImpl中使用java.io实现到Excel的输出,使用itextpdf实现到PDF的输出。
关于itextpdf的使用,这里有一个很好的tutorial blog
一些背景交待:
1、UI技术使用的是smartgwt,MVP模式,表格ListGrid使用的是DataSource
2、整个项目基于OSGi,下面提到的ExportService接口和ExportServiceImpl类不在同一个OSGi bundle中,原因是ExportService需要向几乎所有bundle开放,所以放到了common的bundle中。
第一步,写一个ExportForm
public class ExportForm extends DynamicForm { /** * The FORMAT. */ public static final String FORMAT = "format"; /** * The EXPORT. */ public static final String EXPORT = "export"; /** * Constructs a new <code>ExportForm</code> instance. */ public ExportForm() { SelectItem exportType = new SelectItem(FORMAT, "Export Format"); LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>(); valueMap.put("pdf", "PDF"); valueMap.put("Excel", "Excel"); exportType.setValueMap(valueMap); exportType.setDefaultToFirstOption(true); ButtonItem exportButton = new ButtonItem(EXPORT, "Export"); exportButton.setStartRow(false); setFields(exportType, exportButton); setNumCols(4); setTitleSuffix(null); setWidth100(); } }
注意:exportButton.setStartRow(false);是必要的,因为smartgwt中有些控件,比如Button,默认会另起一行,不管setNumCol(int)设置为多大。
第二步,把这个form控件加到合适的地方。(省略ExportService接口, ExportServiceImpl类, 以及Export单例类的实现,有些代码下面有引用到)
第三步,在ListGrid所在的View对应的Presenter中添加exportButton的ClickHandler处理。当用户点击这个按钮,Presenter就会new一次DataSource(因为export需要export所有的数据,而ListGrid的display只需要一页的数据,所以不能直接从ListGrid export),设置好所有的参数,然后fetch data。将response.getData(), ListGrid.getFields(), 用户选择export的format, 以及fileName传给ExportService.
dataSource.fetchData(new Criteria(), new DSCallback() { @Override public void execute(DSResponse response, Object rawData, DSRequest request) { Export.getInstance().export(response.getData(), gridDisplay.getDataGrid().getFields(), format, fileName); } }); 第四步,注册ExportService,什么是RemoteServiceRelativePath annotation?
ExportServiceImpl之间的servlet mapping。先把ExportService注册到一个service path上,有两种方法:1、使用@RemoteServiceRelativePath()这个annotation@RemoteServiceRelativePath("export")
Associates a RemoteService with a relative path. This annotation will cause the client-side proxy to automatically invoke the ServiceDefTarget.setServiceEntryPoint(String) method with GWT.getModuleBaseURL() + value() as its argument. Subsequent calls to ServiceDefTarget.setServiceEntryPoint(String) will override this default path.
这样注册之后,每当前台call这个Export,就会找一个GWT.getModuleBaseURL() + value()的url,即http://....../发出申请的gwt project的名称/export。上面笔者提到这个service是被share的,任何一个gwt project都会call到这个service,因此需要使用第二种注册方法。
2、使用((ServiceDefTarget) exportService).setServiceEntryPoint("/.../export");这样注册之后,每当前台call这个Export,都会使用固定的URL。
private ExportServiceAsync exportService;
private static Export instance;
private Export() { exportService = GWT.create(ExportService.class); ((ServiceDefTarget) exportService).setServiceEntryPoint("/projectNameAlias/export"); }
public static Export getInstance() { if (instance == null) { instance = new Export(); } return instance; }
接下来在ExportServiceImpl所在的项目的plugin.xml中注册export service的implementation。
<servlet alias="/projectNameAlias/export" class="packageName.server.ExportServiceImpl"> </servlet> </extension> <extension point="org.eclipse.equinox.http.registry.servlets">
第五步,在ExportServiceImpl中使用java.io实现到Excel的输出,使用itextpdf实现到PDF的输出。
关于itextpdf的使用,这里有一个很好的tutorial blog
相关文章推荐
- Export GridView with Images from database to Word Excel and PDF Formats
- csharp: Export DataSet into Excel and import all the Excel sheets to DataSet
- using vb.net export a datatable to Excel and save as file
- Exporting from Crystal Reports to PDF, Word, Excel and HTML
- csharp: Export DataSet into Excel and import all the Excel sheets to DataSet
- Export RadGrid content to Excel/Word/CSV/PDF with Ajax enabled
- Saving Workbooks to PDF and XPS Formats in Excel 2007 (C#.net word excel powerpoint (ppt) 转换成 pdf 文件)
- a simple and easy way to export grid(grid view) to excel
- Export Grid Data To Excel In Advance Kendo UI Using MVC WEB API And Entity Framework
- ReadCompareExcelToDictionary (Resource file export and import)
- export grid to excel fast and wyswyg
- Export GridView To WordExcelPDFCSV in ASP.Net
- csharp: Export DataSet into Excel and import all the Excel sheets to DataSet
- ASP.NET Export DataTable to Excel
- gridview汇出EXCEL (ExportGridViewToExcel(dt, HttpContext.Current.Response);)
- 通过tableExport.js插件来实现导出Excel/Pdf/txt/json等
- Publishing and Synchronizing Excel 2007 Tables to SharePoint Lists (From MSDN)
- how to monitor system logs and export to files simultaneously
- iText parse html with RichText and images to pdf
- How to Export and Import Oracle Database Statistics Information