Search in sources :

Example 1 with XlsExportOptionsDlg

use of net.sourceforge.sqlexplorer.dialogs.XlsExportOptionsDlg in project tdq-studio-se by Talend.

the class ExportXLSAction method run.

/**
 * Copy all table data to clipboard
 * @see org.eclipse.jface.action.IAction#run()
 */
public void run() {
    final XlsExportOptionsDlg dlg = new XlsExportOptionsDlg(_table.getShell());
    if (dlg.open() != Window.OK)
        return;
    BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {

        public void run() {
            try {
                File file = new File(dlg.getFilename());
                if (file.exists()) {
                    // overwrite existing files
                    file.delete();
                }
                file.createNewFile();
                PrintStream writer = new PrintStream(file, dlg.getCharacterSet());
                // $NON-NLS-1$
                StringBuffer buffer = new StringBuffer("");
                // get preferences
                boolean includeColumnNames = dlg.includeHeaders();
                boolean rtrim = dlg.trimSpaces();
                boolean quote = dlg.quoteText();
                String nullValue = dlg.getNullValue();
                DataSet dataSet = (DataSet) _table.getData();
                if (dataSet == null) {
                    return;
                }
                // $NON-NLS-1$
                writer.println("<table>");
                // export column names
                if (includeColumnNames) {
                    // $NON-NLS-1$
                    buffer.append("<tr>");
                    DataSet.Column[] columns = dataSet.getColumns();
                    for (int i = 0; i < columns.length; i++) {
                        // $NON-NLS-1$
                        buffer.append("<th>");
                        buffer.append(TextUtil.htmlEscape(columns[i].getCaption()));
                        // $NON-NLS-1$
                        buffer.append("</th>");
                    }
                    // $NON-NLS-1$
                    buffer.append("</tr>");
                    writer.println(buffer.toString());
                }
                // export column data
                int columnCount = _table.getColumnCount();
                for (int i = 0; i < dataSet.getRowCount(); i++) {
                    // $NON-NLS-1$
                    buffer = new StringBuffer("<tr>");
                    DataSetRow row = dataSet.getRow(i);
                    for (int j = 0; j < columnCount; j++) {
                        // $NON-NLS-1$
                        buffer.append("<td>");
                        Object o = row.getRawObjectValue(j);
                        String t = o == null ? nullValue : o.toString();
                        if (rtrim)
                            t = TextUtil.rtrim(t);
                        if (quote && o instanceof String) {
                            // $NON-NLS-1$
                            buffer.append("\"");
                            buffer.append(TextUtil.htmlEscape(t));
                            // $NON-NLS-1$
                            buffer.append("\"");
                        } else
                            buffer.append(TextUtil.htmlEscape(t));
                        // $NON-NLS-1$
                        buffer.append("</td>");
                    }
                    // $NON-NLS-1$
                    buffer.append("</tr>");
                    writer.println(buffer.toString());
                }
                // $NON-NLS-1$
                writer.println("</table>");
                writer.close();
            } catch (final Exception e) {
                _table.getShell().getDisplay().asyncExec(new Runnable() {

                    public void run() {
                        MessageDialog.openError(_table.getShell(), Messages.getString("SQLResultsView.Error.Export.Title"), e.getMessage());
                        SQLExplorerPlugin.error(Messages.getString("SQLResultsView.Error.Export.Title"), e);
                    }
                });
            }
        }
    });
}
Also used : PrintStream(java.io.PrintStream) DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) XlsExportOptionsDlg(net.sourceforge.sqlexplorer.dialogs.XlsExportOptionsDlg) File(java.io.File) DataSetRow(net.sourceforge.sqlexplorer.dataset.DataSetRow)

Aggregations

File (java.io.File)1 PrintStream (java.io.PrintStream)1 DataSet (net.sourceforge.sqlexplorer.dataset.DataSet)1 DataSetRow (net.sourceforge.sqlexplorer.dataset.DataSetRow)1 XlsExportOptionsDlg (net.sourceforge.sqlexplorer.dialogs.XlsExportOptionsDlg)1