
| Key: |
NTBL-235
|
| Type: |
Functional Story
|
| Status: |
Closed
|
| Resolution: |
Fixed
|
| Priority: |
Major
|
| Assignee: |
Unassigned
|
| Reporter: |
Edwin Park
|
| Votes: |
0
|
| Watchers: |
0
|
|
If you were logged in you would be able to see more operations.
|
|
|
| Acceptance Criteria: |
The excel export must
Preserve column and row header
Preserve all data in body
Export entire table
Preserve style (color, font, border)
Demonstrate using an example. Reuse any of the existing ones.
The excel export must
Preserve column and row header
Preserve all data in body
Export entire table
Preserve style (color, font, border)
Demonstrate using an example. Reuse any of the existing ones.
|
Export all viewable data in the table as an Excel spreadsheet. The exported spreadsheet should contain cells from all grid regions (column/row headers, body, corner) and should be styled in the same way the cells would appear in the table.
|
|
Description
|
Export all viewable data in the table as an Excel spreadsheet. The exported spreadsheet should contain cells from all grid regions (column/row headers, body, corner) and should be styled in the same way the cells would appear in the table. |
Show » |
Sort Order:
|
Not sure what you had planned for this but we currently export our Nat tables to Excel via Apache POI, which works fine. I'm including our code as a reference if you need it. It doesn't color cells, but I'm sure that wouldn't bee too much of a thing to add. The code also checks visible columns and does "padding" for non-selected cells in rows, etc.
The GlazedNatGridViewer class is just a wrapper around a Nat table and contains convenience methods. Let me know if you need any of them.
/** * Saves a table Nat table as excel document using Apache POI. * * @param title Title of Excel document sheet. * @param table Table to save * @param fileName Output file name * @param selectionOnly Output only what's selected in the table * @param job Job to show export status on, method will call job.done() when finished * @param monitor IProgressMonitor to update export status on */ public static void saveTableAsExcel(String title, GlazedNatGridViewer table, String fileName, boolean selectionOnly, Job job, IProgressMonitor monitor) { try { monitor.subTask("Building Excel Document"); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(title); if (selectionOnly) { Point[] points = table.getCellSelection(); List<Integer> cols = new ArrayList<Integer>(); for (Point p : points) { if (!cols.contains(p.x)) cols.add(p.x); } HSSFRow headerRow = sheet.createRow(0); if (_ExportColumnHeaders) { for (int i = 0; i < cols.size(); i++) { String name = table.getColumn(cols.get(i)).getName(); HSSFCell cell = headerRow.createCell((short) i); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(new HSSFRichTextString(name)); } } // our zero-based row map Map<Integer, HSSFRow> rowMap = new HashMap<Integer, HSSFRow>(); int rowCnt = 1; for (int i = 0; i < points.length; i++) { if (monitor.isCanceled()) { return; } Point p = points[i]; HSSFRow rowEntry = rowMap.get(p.y); if (rowEntry == null) { // note that we create the row zero based but store it point y-location based rowEntry = sheet.createRow(rowCnt); rowMap.put(p.y, rowEntry); // it's a new row next time around rowCnt++; } IDataObject gi = table.getItem(p.y); String value = gi.getColumnText(p.x); // again with zero based vs. col location. here we use the columns array to determine zero-based location HSSFCell cell = rowEntry.createCell((short) cols.indexOf(p.x)); cell.setCellValue(new HSSFRichTextString(value)); } } else { int row = 0; HSSFRow headerRow = sheet.createRow(row); List<Integer> cols = table.getVisibleColumnOrder(); int colLoc = 0; for (int i : cols) { HSSFCell cell = headerRow.createCell((short) colLoc); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(new HSSFRichTextString(table.getColumn(i).getName())); colLoc++; } row++; List<IDataObject> allObjs = table.getAllItems(); for (int i = 0; i < allObjs.size(); i++) { if (monitor.isCanceled()) return; HSSFRow rowEntry = sheet.createRow(row); colLoc = 0; for (int x : cols) { HSSFCell cell = rowEntry.createCell((short) colLoc); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(new HSSFRichTextString(allObjs.get(i).getColumnText(x))); colLoc++; } row++; } } if (monitor.isCanceled()) { return; } job.done(Status.OK_STATUS); localSaveTableAsExcel(wb, fileName); } catch (Exception err) { err.printStackTrace(); } } private static void localSaveTableAsExcel(final HSSFWorkbook wb, final String fileName) throws IOException { Display.getDefault().asyncExec(new Runnable() { @Override public void run() { FileDialog fd = new FileDialog(Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.SAVE); fd.setFileName(fileName + ".xls"); fd.setFilterNames(new String[] { "Microsoft Excel Files", "All Files" }); fd.setFilterExtensions(new String[] { "*.xls", "*.*" }); String pick = fd.open(); if (pick == null) { return; } if (!pick.toLowerCase(Locale.getDefault()).endsWith(".xls")) { pick = pick + ".xls"; } try { final File f = new File(pick); if (f.exists()) { boolean overwrite = MessageDialog.openQuestion(Display.getDefault().getActiveShell(), "File Exists", "Would you like to overwrite the existing file?"); if (!overwrite) { return; } } Job job = new Job("Writing Excel File") { @Override protected IStatus run(IProgressMonitor monitor) { monitor.beginTask("Writing Excel File", IProgressMonitor.UNKNOWN); try { FileOutputStream fos = new FileOutputStream(f); wb.write(fos); fos.close(); } catch (Exception err) { GuiLogManager.error(err); } return Status.OK_STATUS; } }; job.setUser(true); job.schedule(); } catch (Exception err) { err.printStackTrace(); MessageDialog.openError(Display.getDefault().getActiveShell(), "Error", "There was an error writing to the file. Please ensure the target destination is writable."); } } }); }