Anda di halaman 1dari 4

final FormPanel formPanel = new FormPanel(); final HiddenField hiddenVal = new HiddenField<String>(); hiddenVal.setName("ExportedFilename"); formPanel.

add(hiddenVal);

// My grid protected EditorGrid<AircraftStatusBoardRow> editorGrid; // Column header is used to store the column header for the excel document final List<GridHeader> columnHeaders = new Vector(); Button exportButton = new Button(); exportButton.setIconStyle("export-icon"); exportButton.setToolTip("Export table to Excel"); exportButton.addSelectionListener(new SelectionListener() { @Override public void componentSelected(ComponentEvent ce) { editorGrid.stopEditing(); final List<GridHeader> columnHeaders = new Vector(); for (int a = 0; a < editorGrid.getColumnModel() .getColumnCount(); a++) { columnHeaders.add(new GridHeader(editorGrid .getColumnModel().getColumn(a).getHeader(), editorGrid.getColumnModel().getColumn(a).getId())); } // Need to get all the grid data. By default, whatever is on the page is used. service.getAllGridData(new AsyncCallback() { public void onFailure(Throwable caught) { Dialog errorDialog = new Dialog(); errorDialog.setHeading("Error"); errorDialog.setButtons(Dialog.OK); errorDialog.setBodyStyleName("pad-text"); errorDialog .addText("An error occurred exporting the table data: <p>" + caught.getMessage() + "<p>" + "Please contact your system administra tor."); errorDialog.setScrollMode(Scroll.AUTO); errorDialog.setHideOnButtonClick(true); errorDialog.show(); } public void onSuccess(Object result) { service.createExportExcelFile( (List<AircraftStatusBoardRow>) result, columnHeaders, new AsyncCallback() { public void onFailure(Throwable caught) { Dialog errorDialog = new Dialog(); errorDialog.setHeading("Error"); errorDialog.setButtons(Dialog.OK); errorDialog .setBodyStyleName("pad-text"); errorDialog

.addText("An error occurred expo rting the table data: <p>" + caught.getMessage() + "<p>" + "Please contact your s ystem administrator."); errorDialog.setScrollMode(Scroll.AUTO); errorDialog.setHideOnButtonClick(true); errorDialog.show(); } public void onSuccess(Object result) { String fileName = (String) result; hiddenVal.setValue(fileName); String url = GWT.getModuleBaseURL(); url = url + "maintenanceplannerexport"; formPanel.setAction(url); formPanel.submit(); } }); } }); } }); // customToolBar.add(exportButton); toolBar.insert(exportButton, 13);

//Now, here's the button code that I add to the grid's pagination toolbar. public String createExportExcelFile(List<AircraftStatusBoardRow> tableRecordData , List<GridHeader> columnHeader) throws MaintenancePlannerException { Date now = Calendar.getInstance().getTime(); SimpleDateFormat df = new SimpleDateFormat("MM-dd-yyyy"); String fileName = "ASBExport_" + df.format(now) + ".xls"; WritableWorkbook workbook; try { File exportFolder = new File("export"); if (!exportFolder.exists()) { exportFolder.mkdir(); } workbook = Workbook.createWorkbook(new File("export\\" + fileName)); WritableSheet sheet = workbook.createSheet("Exported Data", 0); createWorkSheet(sheet, columnHeader, tableRecordData); workbook.write(); workbook.close(); } catch (IOException e) {

new LogServiceWrapper().log(LogLevel.SEVERE, this.getClass().getName(), e.getMessage(), e); throw new MaintenancePlannerException(e); } catch (WriteException e) { new LogServiceWrapper().log(LogLevel.SEVERE, this.getClass().getName(), e.getMessage(), e); throw new MaintenancePlannerException(e); } return fileName; } private void createWorkSheet(WritableSheet sheet, List<GridHeader> columnHeader, List<AircraftStatusBoardRow> tableRecordData) throws WriteException { // Code left out. Just reference jExcelAPI to write out data, it's very straightforward }

//Finally, here's the servlet code that reads in the filename and sends the exce l file back to the client for open/save: public class ExportServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { OutputStream out = null; File exportedFile = null; try { String fileName = request.getParameter("ExportedFilename"); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); exportedFile = new File("export\\" + fileName); Workbook book = Workbook.getWorkbook(exportedFile); WritableWorkbook w = Workbook.createWorkbook(response .getOutputStream(), book); w.write(); w.close(); } catch (Exception e) { new LogServiceWrapper().log(LogLevel.SEVERE, this.getClass().getName(), e.getMessage(), e); throw new ServletException("Exception in Excel Sample Servlet", e); } finally { if (out != null) { out.close(); } if (exportedFile != null) { exportedFile.delete(); } }

} }

//Forgot the GridHeader just in case folks wanted to know what it looks like public class GridHeader implements Serializable { private String headerName; private String headerID; public GridHeader() { super(); this.headerID = ""; this.headerName = ""; } /** * @param headerName * @param headerID */ public GridHeader(String headerName, String headerID) { super(); this.headerName = headerName; this.headerID = headerID; } public String getHeaderName() { return headerName; } public void setHeaderName(String headerName) { this.headerName = headerName; } public String getHeaderID() { return headerID; } public void setHeaderID(String headerID) { this.headerID = headerID; } }

Anda mungkin juga menyukai