Search in sources :

Example 1 with JxlWriteException

use of jxl.write.biff.JxlWriteException in project janrufmonitor by tbrandt77.

the class XlsFilter method doExport.

public boolean doExport() {
    if (m_filename == null || m_filename.length() == 0)
        return false;
    WritableWorkbook wb = null;
    try {
        wb = Workbook.createWorkbook(new File(m_filename));
        WritableSheet sheet = wb.createSheet("journal", 0);
        // get renderers
        List renderer = new ArrayList();
        String renderer_config = this.getRuntime().getConfigManagerFactory().getConfigManager().getProperty("ui.jface.application.journal.Journal", "renderer");
        if (renderer_config != null && renderer_config.length() > 0) {
            StringTokenizer s = new StringTokenizer(renderer_config, ",");
            while (s.hasMoreTokens()) {
                renderer.add(RendererRegistry.getInstance().getRenderer(s.nextToken()));
            }
        }
        ITableCellRenderer t = null;
        Label cell = null;
        for (int i = 0, j = renderer.size(); i < j; i++) {
            t = (ITableCellRenderer) renderer.get(i);
            if (t == null) {
                this.m_logger.severe("No renderer found for ID: " + (String) renderer.get(i));
                this.m_logger.severe("Export to XLS format canceled...");
                return false;
            }
            cell = new Label(i, 0, t.getHeader());
            sheet.addCell(cell);
        }
        int[] widths = new int[renderer.size()];
        ICall c = null;
        String cellContent = null;
        for (int k = 0, i = 0, j = this.m_callList.size(); i < j; i++) {
            try {
                c = this.m_callList.get(i);
                k = 0;
                for (int m = renderer.size(); k < m; k++) {
                    t = (ITableCellRenderer) renderer.get(k);
                    t.updateData(c);
                    cellContent = t.renderAsText();
                    if (cellContent != null && cellContent.length() > 0) {
                        widths[k] = Math.max(widths[k], cellContent.length());
                        cell = new Label(k, i + 1, cellContent);
                        sheet.addCell(cell);
                    } else {
                        cellContent = t.renderAsImageID();
                        if (cellContent != null && cellContent.length() > 0) {
                            if (cellContent.indexOf(".") > -1)
                                cellContent = cellContent.substring(0, cellContent.indexOf("."));
                            widths[k] = Math.max(widths[k], cellContent.length());
                            cell = new Label(k, i + 1, cellContent);
                        } else
                            cell = new Label(k, i + 1, " ");
                        sheet.addCell(cell);
                    }
                    cell = null;
                }
            } catch (JxlWriteException e) {
                this.m_logger.severe(e.getMessage());
                this.m_logger.severe("Ignored data [" + (k + 1) + ", " + (i + 1) + "] for Excel export: " + cellContent);
            }
        }
        // calculate width
        for (int i = 0, j = renderer.size(); i < j; i++) {
            sheet.setColumnView(i, widths[i]);
        }
        wb.write();
    } catch (Exception e) {
        this.m_logger.severe(e.getMessage());
        return false;
    } finally {
        try {
            if (wb != null)
                wb.close();
        } catch (WriteException ex) {
            this.m_logger.severe(ex.getMessage());
            return false;
        } catch (IOException ex) {
            this.m_logger.severe(ex.getMessage());
            return false;
        }
    }
    return true;
}
Also used : ITableCellRenderer(de.janrufmonitor.ui.jface.application.rendering.ITableCellRenderer) WriteException(jxl.write.WriteException) JxlWriteException(jxl.write.biff.JxlWriteException) ICall(de.janrufmonitor.framework.ICall) ArrayList(java.util.ArrayList) Label(jxl.write.Label) WritableSheet(jxl.write.WritableSheet) IOException(java.io.IOException) IOException(java.io.IOException) WriteException(jxl.write.WriteException) JxlWriteException(jxl.write.biff.JxlWriteException) WritableWorkbook(jxl.write.WritableWorkbook) StringTokenizer(java.util.StringTokenizer) JxlWriteException(jxl.write.biff.JxlWriteException) ICallList(de.janrufmonitor.framework.ICallList) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Aggregations

ICall (de.janrufmonitor.framework.ICall)1 ICallList (de.janrufmonitor.framework.ICallList)1 ITableCellRenderer (de.janrufmonitor.ui.jface.application.rendering.ITableCellRenderer)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 StringTokenizer (java.util.StringTokenizer)1 Label (jxl.write.Label)1 WritableSheet (jxl.write.WritableSheet)1 WritableWorkbook (jxl.write.WritableWorkbook)1 WriteException (jxl.write.WriteException)1 JxlWriteException (jxl.write.biff.JxlWriteException)1