Search in sources :

Example 1 with DataExporterXLSX

use of org.jkiss.dbeaver.data.office.export.DataExporterXLSX in project dbeaver by dbeaver.

the class OpenSpreadsheetHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IResultSetController resultSet = ResultSetCommandHandler.getActiveResultSet(HandlerUtil.getActivePart(event));
    if (resultSet == null) {
        DBeaverUI.getInstance().showError("Open Excel", "No active results viewer");
        return null;
    }
    DBSDataContainer dataContainer = resultSet.getDataContainer();
    if (dataContainer == null || dataContainer.getDataSource() == null) {
        DBeaverUI.getInstance().showError("Open Excel", "Not connected to a database");
        return null;
    }
    AbstractJob exportJob = new AbstractJob("Open Excel") {

        {
            setUser(true);
            setSystem(false);
        }

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            try {
                File tempDir = DBeaverCore.getInstance().getTempFolder(monitor, "office-files");
                File tempFile = new File(tempDir, CommonUtils.escapeFileName(CommonUtils.truncateString(dataContainer.getName(), 32)) + "." + new SimpleDateFormat("yyyyMMdd-HHmmss").format(System.currentTimeMillis()) + ".xlsx");
                DataExporterXLSX exporter = new DataExporterXLSX();
                StreamPOITransferConsumer consumer = new StreamPOITransferConsumer();
                StreamPOIConsumerSettings settings = new StreamPOIConsumerSettings();
                settings.setOutputEncodingBOM(false);
                settings.setOpenFolderOnFinish(false);
                settings.setOutputFolder(tempDir.getAbsolutePath());
                settings.setOutputFilePattern(tempFile.getName());
                Map<Object, Object> properties = DataExporterXLSX.getDefaultProperties();
                consumer.initTransfer(dataContainer, settings, exporter, properties);
                DatabaseTransferProducer producer = new DatabaseTransferProducer(dataContainer);
                DatabaseProducerSettings producerSettings = new DatabaseProducerSettings();
                producerSettings.setExtractType(DatabaseProducerSettings.ExtractType.SINGLE_QUERY);
                producerSettings.setQueryRowCount(false);
                producer.transferData(monitor, consumer, producerSettings);
                consumer.finishTransfer(monitor, false);
                DBeaverUI.asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        if (!UIUtils.launchProgram(tempFile.getAbsolutePath())) {
                            DBeaverUI.getInstance().showError("Open XLSX", "Can't open XLSX file '" + tempFile.getAbsolutePath() + "'");
                        }
                    }
                });
            } catch (Exception e) {
                DBeaverUI.getInstance().showError("Error opening in Excel", null, e);
            }
            return Status.OK_STATUS;
        }
    };
    exportJob.schedule();
    return null;
}
Also used : StreamPOIConsumerSettings(org.jkiss.dbeaver.data.office.export.StreamPOIConsumerSettings) IResultSetController(org.jkiss.dbeaver.ui.controls.resultset.IResultSetController) ExecutionException(org.eclipse.core.commands.ExecutionException) DataExporterXLSX(org.jkiss.dbeaver.data.office.export.DataExporterXLSX) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DatabaseTransferProducer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer) DatabaseProducerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings) StreamPOITransferConsumer(org.jkiss.dbeaver.data.office.export.StreamPOITransferConsumer) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBSDataContainer(org.jkiss.dbeaver.model.struct.DBSDataContainer) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with DataExporterXLSX

use of org.jkiss.dbeaver.data.office.export.DataExporterXLSX in project dbeaver by dbeaver.

the class OpenSpreadsheetHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IResultSetController resultSet = ResultSetHandlerMain.getActiveResultSet(HandlerUtil.getActivePart(event));
    if (resultSet == null) {
        DBWorkbench.getPlatformUI().showError("Open Excel", "No active results viewer");
        return null;
    }
    ResultSetDataContainerOptions options = new ResultSetDataContainerOptions();
    IResultSetSelection rsSelection = resultSet.getSelection();
    List<ResultSetRow> rsSelectedRows = rsSelection.getSelectedRows();
    if (rsSelectedRows.size() > 1) {
        List<Long> selectedRows = new ArrayList<>();
        for (ResultSetRow selectedRow : rsSelectedRows) {
            selectedRows.add((long) selectedRow.getRowNumber());
        }
        List<String> selectedAttributes = new ArrayList<>();
        for (DBDAttributeBinding attributeBinding : rsSelection.getSelectedAttributes()) {
            selectedAttributes.add(attributeBinding.getName());
        }
        options.setSelectedRows(selectedRows);
        options.setSelectedColumns(selectedAttributes);
    }
    ResultSetDataContainer dataContainer = new ResultSetDataContainer(resultSet, options);
    if (dataContainer.getDataSource() == null) {
        DBWorkbench.getPlatformUI().showError("Open Excel", ModelMessages.error_not_connected_to_database);
        return null;
    }
    AbstractJob exportJob = new AbstractJob("Open Excel") {

        {
            setUser(true);
            setSystem(false);
        }

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            try {
                File tempDir = DBWorkbench.getPlatform().getTempFolder(monitor, "office-files");
                File tempFile = new File(tempDir, CommonUtils.escapeFileName(CommonUtils.truncateString(dataContainer.getName(), 32)) + "." + new SimpleDateFormat("yyyyMMdd-HHmmss").format(System.currentTimeMillis()) + ".xlsx");
                tempFile.deleteOnExit();
                StreamExporterAbstract exporter = new DataExporterXLSX();
                StreamTransferConsumer consumer = new StreamTransferConsumer();
                StreamConsumerSettings settings = new StreamConsumerSettings();
                settings.setOutputEncodingBOM(false);
                settings.setOpenFolderOnFinish(false);
                settings.setOutputFolder(tempDir.getAbsolutePath());
                settings.setOutputFilePattern(tempFile.getName());
                Map<String, Object> properties = DataExporterXLSX.getDefaultProperties();
                consumer.initTransfer(dataContainer, settings, new IDataTransferConsumer.TransferParameters(true, false), exporter, properties);
                DBDDataFilter dataFilter = resultSet.getModel().getDataFilter();
                DatabaseTransferProducer producer = new DatabaseTransferProducer(dataContainer, dataFilter);
                DatabaseProducerSettings producerSettings = new DatabaseProducerSettings();
                producerSettings.setExtractType(DatabaseProducerSettings.ExtractType.SINGLE_QUERY);
                producerSettings.setQueryRowCount(false);
                producerSettings.setSelectedRowsOnly(true);
                producerSettings.setSelectedColumnsOnly(true);
                producer.transferData(monitor, consumer, null, producerSettings, null);
                consumer.finishTransfer(monitor, false);
                UIUtils.asyncExec(() -> {
                    if (!UIUtils.launchProgram(tempFile.getAbsolutePath())) {
                        DBWorkbench.getPlatformUI().showError("Open XLSX", "Can't open XLSX file '" + tempFile.getAbsolutePath() + "'");
                    }
                });
            } catch (Exception e) {
                DBWorkbench.getPlatformUI().showError("Error opening in Excel", null, e);
            }
            return Status.OK_STATUS;
        }
    };
    exportJob.schedule();
    return null;
}
Also used : StreamExporterAbstract(org.jkiss.dbeaver.tools.transfer.stream.exporter.StreamExporterAbstract) ArrayList(java.util.ArrayList) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) StreamConsumerSettings(org.jkiss.dbeaver.tools.transfer.stream.StreamConsumerSettings) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) StreamTransferConsumer(org.jkiss.dbeaver.tools.transfer.stream.StreamTransferConsumer) IDataTransferConsumer(org.jkiss.dbeaver.tools.transfer.IDataTransferConsumer) ExecutionException(org.eclipse.core.commands.ExecutionException) DataExporterXLSX(org.jkiss.dbeaver.data.office.export.DataExporterXLSX) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DatabaseTransferProducer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer) DatabaseProducerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with DataExporterXLSX

use of org.jkiss.dbeaver.data.office.export.DataExporterXLSX in project dbeaver by serge-rider.

the class OpenSpreadsheetHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IResultSetController resultSet = ResultSetHandlerMain.getActiveResultSet(HandlerUtil.getActivePart(event));
    if (resultSet == null) {
        DBWorkbench.getPlatformUI().showError("Open Excel", "No active results viewer");
        return null;
    }
    ResultSetDataContainerOptions options = new ResultSetDataContainerOptions();
    IResultSetSelection rsSelection = resultSet.getSelection();
    List<ResultSetRow> rsSelectedRows = rsSelection.getSelectedRows();
    if (rsSelectedRows.size() > 1) {
        List<Long> selectedRows = new ArrayList<>();
        for (ResultSetRow selectedRow : rsSelectedRows) {
            selectedRows.add((long) selectedRow.getRowNumber());
        }
        List<String> selectedAttributes = new ArrayList<>();
        for (DBDAttributeBinding attributeBinding : rsSelection.getSelectedAttributes()) {
            selectedAttributes.add(attributeBinding.getName());
        }
        options.setSelectedRows(selectedRows);
        options.setSelectedColumns(selectedAttributes);
    }
    ResultSetDataContainer dataContainer = new ResultSetDataContainer(resultSet, options);
    if (dataContainer.getDataSource() == null) {
        DBWorkbench.getPlatformUI().showError("Open Excel", ModelMessages.error_not_connected_to_database);
        return null;
    }
    AbstractJob exportJob = new AbstractJob("Open Excel") {

        {
            setUser(true);
            setSystem(false);
        }

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            try {
                File tempDir = DBWorkbench.getPlatform().getTempFolder(monitor, "office-files");
                File tempFile = new File(tempDir, CommonUtils.escapeFileName(CommonUtils.truncateString(dataContainer.getName(), 32)) + "." + new SimpleDateFormat("yyyyMMdd-HHmmss").format(System.currentTimeMillis()) + ".xlsx");
                tempFile.deleteOnExit();
                StreamExporterAbstract exporter = new DataExporterXLSX();
                StreamTransferConsumer consumer = new StreamTransferConsumer();
                StreamConsumerSettings settings = new StreamConsumerSettings();
                settings.setOutputEncodingBOM(false);
                settings.setOpenFolderOnFinish(false);
                settings.setOutputFolder(tempDir.getAbsolutePath());
                settings.setOutputFilePattern(tempFile.getName());
                Map<String, Object> properties = DataExporterXLSX.getDefaultProperties();
                consumer.initTransfer(dataContainer, settings, new IDataTransferConsumer.TransferParameters(true, false), exporter, properties);
                DBDDataFilter dataFilter = resultSet.getModel().getDataFilter();
                DatabaseTransferProducer producer = new DatabaseTransferProducer(dataContainer, dataFilter);
                DatabaseProducerSettings producerSettings = new DatabaseProducerSettings();
                producerSettings.setExtractType(DatabaseProducerSettings.ExtractType.SINGLE_QUERY);
                producerSettings.setQueryRowCount(false);
                producerSettings.setSelectedRowsOnly(true);
                producerSettings.setSelectedColumnsOnly(true);
                producer.transferData(monitor, consumer, null, producerSettings, null);
                consumer.finishTransfer(monitor, false);
                UIUtils.asyncExec(() -> {
                    if (!UIUtils.launchProgram(tempFile.getAbsolutePath())) {
                        DBWorkbench.getPlatformUI().showError("Open XLSX", "Can't open XLSX file '" + tempFile.getAbsolutePath() + "'");
                    }
                });
            } catch (Exception e) {
                DBWorkbench.getPlatformUI().showError("Error opening in Excel", null, e);
            }
            return Status.OK_STATUS;
        }
    };
    exportJob.schedule();
    return null;
}
Also used : StreamExporterAbstract(org.jkiss.dbeaver.tools.transfer.stream.exporter.StreamExporterAbstract) ArrayList(java.util.ArrayList) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) StreamConsumerSettings(org.jkiss.dbeaver.tools.transfer.stream.StreamConsumerSettings) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) StreamTransferConsumer(org.jkiss.dbeaver.tools.transfer.stream.StreamTransferConsumer) IDataTransferConsumer(org.jkiss.dbeaver.tools.transfer.IDataTransferConsumer) ExecutionException(org.eclipse.core.commands.ExecutionException) DataExporterXLSX(org.jkiss.dbeaver.data.office.export.DataExporterXLSX) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DatabaseTransferProducer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer) DatabaseProducerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

File (java.io.File)3 SimpleDateFormat (java.text.SimpleDateFormat)3 ExecutionException (org.eclipse.core.commands.ExecutionException)3 DataExporterXLSX (org.jkiss.dbeaver.data.office.export.DataExporterXLSX)3 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)3 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)3 DatabaseProducerSettings (org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings)3 DatabaseTransferProducer (org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer)3 ArrayList (java.util.ArrayList)2 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)2 DBDDataFilter (org.jkiss.dbeaver.model.data.DBDDataFilter)2 IDataTransferConsumer (org.jkiss.dbeaver.tools.transfer.IDataTransferConsumer)2 StreamConsumerSettings (org.jkiss.dbeaver.tools.transfer.stream.StreamConsumerSettings)2 StreamTransferConsumer (org.jkiss.dbeaver.tools.transfer.stream.StreamTransferConsumer)2 StreamExporterAbstract (org.jkiss.dbeaver.tools.transfer.stream.exporter.StreamExporterAbstract)2 StreamPOIConsumerSettings (org.jkiss.dbeaver.data.office.export.StreamPOIConsumerSettings)1 StreamPOITransferConsumer (org.jkiss.dbeaver.data.office.export.StreamPOITransferConsumer)1 DBSDataContainer (org.jkiss.dbeaver.model.struct.DBSDataContainer)1 IResultSetController (org.jkiss.dbeaver.ui.controls.resultset.IResultSetController)1