public void processExcelExport(ExcelExportActionNotification object) {


Display.getDefault().syncExec(new Runnable() {

public void run() {

try {

FileDialog fd = new FileDialog(object.getTable().getControl().getShell(), SWT.SAVE);

fd.setText("Save");

fd.setFilterPath("C:/");

String[] filterExt = {"*.xls", "*.*" };

fd.setFilterExtensions(filterExt);

String path = fd.open();


if (path == null || path == "" || fd.getFileName() == null || fd.getFileName() == "") {

exceptionHandler.handleException(Message.ExportcancelMsg);

return;

}

FileContainer.getInstance().setFilePath(path);

new ProgressMonitorDialog(object.getTable().getControl().getShell()).run(false, false,

new LongRunningOperation(object, path));

} catch (Exception e) {

LOGGER.error("Export Error", e);

exceptionHandler.handleException(Message.errExportContent);

}

}

});

}


public class LongRunningOperation implements

IRunnableWithProgress {

ExcelExportActionNotification notification;

String path;


public LongRunningOperation(ExcelExportActionNotification object, String path) {

this.notification = object;

this.path = path;

}


@Override

public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {

try (FileOutputStream out = new FileOutputStream(FileContainer.getInstance().getCurrentFile())) {

//Create Data, 작업량 설정

monitor.beginTask("Export Data",

notification.getTable().getTable().getItemCount() * notification.getColumn_header().length);



//Create workbook and sheet

workbook = new HSSFWorkbook();

HSSFSheet sheet = workbook.createSheet();

sheet.setGridsPrinted(true);

sheet.setFitToPage(true);

sheet.setDisplayGuts(true);


//Header 설정

row = sheet.createRow(0);

for (int colIndex = 0; colIndex < notification.getColumn_header().length; colIndex++) {

cell = row.createCell(colIndex);

cell.setCellValue(notification.getColumn_header()[colIndex]);

}


//Data Input

for (int rowIndex = 0; rowIndex < notification.getTable().getTable().getItemCount(); rowIndex++) {

row = sheet.createRow(rowIndex + 1);

for (int colIndex = 0; colIndex < notification.getColumn_header().length; colIndex++) {

cell = row.createCell(colIndex);

cell.setCellValue(notification.getTable().getTable().getItem(rowIndex).getText(colIndex));

monitor.worked(1);

}

}


monitor.done();

workbook.write(out);


//내보내기가 완료된 엑셀파일 실행

if (MessageDialog.openConfirm(null, Message.successTitle, Message.msgExportSuccess)) {

Program program = Program.findProgram("xls");

program.execute(path);

}

} catch (IOException e) {

LOGGER.error("Export Error", e);

exceptionHandler.handleException(Message.errExportContent);

} finally {

workbook = null;

row = null;

cell = null;

}

}

}


결과 화면

 

+ Recent posts