From ae3dabbd4f955980f6b00ee3eb840de27310edb4 Mon Sep 17 00:00:00 2001 From: DamithDeshan Date: Tue, 24 Dec 2024 23:46:25 +0530 Subject: [PATCH 1/2] closes #9851 Signed-off-by: DamithDeshan --- .../lab/LabReportExportImportController.java | 75 ++++++++++++------- src/main/webapp/admin/lims/export.xhtml | 10 ++- .../webapp/admin/lims/investigation.xhtml | 1 + 3 files changed, 58 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/divudi/bean/lab/LabReportExportImportController.java b/src/main/java/com/divudi/bean/lab/LabReportExportImportController.java index 573499f409..6a4801309e 100644 --- a/src/main/java/com/divudi/bean/lab/LabReportExportImportController.java +++ b/src/main/java/com/divudi/bean/lab/LabReportExportImportController.java @@ -41,10 +41,14 @@ import com.divudi.data.CssVerticalAlign; import com.divudi.facade.ReportItemFacade; import com.divudi.java.CommonFunctions; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.InputStream; import static org.apache.poi.ss.usermodel.CellType.FORMULA; import static org.apache.poi.ss.usermodel.CellType.NUMERIC; import static org.apache.poi.ss.usermodel.CellType.STRING; +import org.primefaces.model.DefaultStreamedContent; +import org.primefaces.model.StreamedContent; /** * @@ -78,6 +82,7 @@ public class LabReportExportImportController implements Serializable { private List uploadedSuccessItems; private List uploadedRejectedItems; private UploadedFile file; + private StreamedContent downloadingExcel; // @@ -133,20 +138,26 @@ public List listInvestigationItemsFilteredByItemTypes(Investi return tis; } - public void export() throws IOException { + public StreamedContent export(){ if (getCurrent() == null) { JsfUtil.addErrorMessage("Pleace select Investigations "); - return; + return null; } List items = investigationItemController.getUserChangableItems(); + if (items == null) { JsfUtil.addErrorMessage("This Investigation has no InvestigationItem"); - return; + return null; } - exportInvestigationItemsToExcel(items); - JsfUtil.addSuccessMessage("Successfuly Uploaed !"); - + try { + downloadingExcel = exportInvestigationItemsToExcel(items); + } catch (IOException e) { + // Handle IOException + } + JsfUtil.addSuccessMessage("Successfuly Download !"); + return downloadingExcel; + } public void importFormat() { @@ -193,19 +204,21 @@ private String defaultIfNullOrEmpty(String value, String defaultValue) { return (value == null || value.trim().isEmpty()) ? defaultValue : value; } - public void exportInvestigationItemsToExcel(List investigationItems) throws IOException { + public StreamedContent exportInvestigationItemsToExcel(List investigationItems) throws IOException { // Define a predefined directory (e.g., user's desktop) System.out.println("Investigation Items = " + investigationItems.size()); - String userHome = System.getProperty("user.home"); - String defaultDirectory = userHome + File.separator + "Desktop"; // Change to any other directory if needed - String fileName = getCurrent().getName() + ".xlsx"; - String fullFilePath = defaultDirectory + File.separator + fileName; +// String userHome = System.getProperty("user.home"); +// String defaultDirectory = userHome + File.separator + "Desktop"; // Change to any other directory if needed +// String fullFilePath = defaultDirectory + File.separator + fileName; +// +// // Ensure the directory exists +// File file = new File(fullFilePath); +// file.getParentFile().mkdirs(); // Create parent directories if they don't exist - // Ensure the directory exists - File file = new File(fullFilePath); - file.getParentFile().mkdirs(); // Create parent directories if they don't exist + StreamedContent excelSc; // Create a new workbook and a sheet + XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("InvestigationItems"); @@ -262,17 +275,21 @@ public void exportInvestigationItemsToExcel(List investigatio sheet.autoSizeColumn(i); } - // Write the workbook to the default file path - System.out.println("Attempting to write to: " + fullFilePath); - try (FileOutputStream outputStream = new FileOutputStream(file)) { - workbook.write(outputStream); - System.out.println("Excel file created successfully at " + fullFilePath); - } catch (IOException e) { - System.out.println("Error writing Excel file: " + e.getMessage()); - e.printStackTrace(); - } finally { - workbook.close(); - } + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + workbook.write(outputStream); + workbook.close(); + + InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); + + String fileName = getCurrent().getName() + ".xlsx"; + // Set the downloading file + excelSc = DefaultStreamedContent.builder() + .name(fileName) + .contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") + .stream(() -> inputStream) + .build(); + + return excelSc; } // @@ -762,4 +779,12 @@ public void setDataUploadController(DataUploadController dataUploadController) { } // + public StreamedContent getDownloadingExcel() { + return downloadingExcel; + } + + public void setDownloadingExcel(StreamedContent downloadingExcel) { + this.downloadingExcel = downloadingExcel; + } + } diff --git a/src/main/webapp/admin/lims/export.xhtml b/src/main/webapp/admin/lims/export.xhtml index 9ffc3e46c5..1392dfae56 100644 --- a/src/main/webapp/admin/lims/export.xhtml +++ b/src/main/webapp/admin/lims/export.xhtml @@ -11,6 +11,8 @@ + +
@@ -30,15 +32,17 @@
- +
+ value="Export" + onclick="PrimeFaces.monitorDownload(start, stop);" > + +
diff --git a/src/main/webapp/admin/lims/investigation.xhtml b/src/main/webapp/admin/lims/investigation.xhtml index c01eb3ddef..2b814f61d4 100644 --- a/src/main/webapp/admin/lims/investigation.xhtml +++ b/src/main/webapp/admin/lims/investigation.xhtml @@ -555,6 +555,7 @@ icon="fas fa-file-export export-icon" action="#{investigationController.navigateExportReoirtFormat()}" value="Export Format" > + Date: Tue, 24 Dec 2024 23:52:08 +0530 Subject: [PATCH 2/2] Signed-off-by: DamithDeshan --- .../admin/lims/import_report_format.xhtml | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/webapp/admin/lims/import_report_format.xhtml b/src/main/webapp/admin/lims/import_report_format.xhtml index a0ebfd4425..504d05581c 100644 --- a/src/main/webapp/admin/lims/import_report_format.xhtml +++ b/src/main/webapp/admin/lims/import_report_format.xhtml @@ -12,7 +12,21 @@ - + + + + + + + + +
@@ -70,7 +84,7 @@ - +