use of org.jkiss.dbeaver.data.office.export.StreamPOITransferConsumer 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;
}
Aggregations