Commit ae4f4a7e authored by Pratik Shrivastava's avatar Pratik Shrivastava

feat: add 'download-master-interaction-excel' feature

parent cb00966a
...@@ -153,6 +153,11 @@ ...@@ -153,6 +153,11 @@
<groupId>javax.mail</groupId> <groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId> <artifactId>javax.mail-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
......
...@@ -17,11 +17,14 @@ import org.springframework.http.MediaType; ...@@ -17,11 +17,14 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid; import javax.validation.Valid;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -316,6 +319,38 @@ public class AdmissionFormController { ...@@ -316,6 +319,38 @@ public class AdmissionFormController {
response.setHeader("Content-Disposition", "attachment; filename=master-interaction-template.pdf"); response.setHeader("Content-Disposition", "attachment; filename=master-interaction-template.pdf");
IOUtils.copy(pdfStream, response.getOutputStream()); IOUtils.copy(pdfStream, response.getOutputStream());
} }
public byte[] convertAdmissionFormToExcel(List<AdmissionFormData> admissionFormDataList) throws Exception {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Admission Form Data");
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("SL. NO.");
headerRow.createCell(1).setCellValue("Reference Id");
headerRow.createCell(2).setCellValue("Name");
headerRow.createCell(3).setCellValue("Father's Name");
headerRow.createCell(4).setCellValue("Interaction Time");
// Start data rows from second row
int rowIndex = 1;
for (AdmissionFormData admissionFormData : admissionFormDataList) {
Row dataRow = sheet.createRow(rowIndex++);
// Set data for each column based on AdmissionFormData fields
dataRow.createCell(0).setCellValue(admissionFormData.getId());
dataRow.createCell(1).setCellValue(admissionFormData.getFatherEmail());
dataRow.createCell(2).setCellValue(admissionFormData.getFullName());
dataRow.createCell(3).setCellValue(admissionFormData.getFatherName());
dataRow.createCell(4).setCellValue(admissionFormData.getInteractionTime());
}
// Write the workbook to a byte array
ByteArrayOutputStream out = new ByteArrayOutputStream();
workbook.write(out);
return out.toByteArray();
}
@GetMapping( @GetMapping(
...@@ -327,7 +362,7 @@ public class AdmissionFormController { ...@@ -327,7 +362,7 @@ public class AdmissionFormController {
@ApiResponse(code = 500, message = "Exception Occurred") @ApiResponse(code = 500, message = "Exception Occurred")
}) })
@ApiOperation(value = "") @ApiOperation(value = "")
public Object downloadMasterInteractionExcel( public void downloadMasterInteractionExcel(
HttpServletRequest request, HttpServletRequest request,
HttpServletResponse response, HttpServletResponse response,
@RequestParam("apikey") String apiKey, @RequestParam("apikey") String apiKey,
...@@ -348,24 +383,18 @@ public class AdmissionFormController { ...@@ -348,24 +383,18 @@ public class AdmissionFormController {
throw new Exception("No Admission Data Found"); throw new Exception("No Admission Data Found");
} }
//TODO: write logic to download excel // Call the convertAdmissionFormToExcel method to get the Excel data as byte array
return new ResponseEntity<>( byte[] excelData = convertAdmissionFormToExcel(admissionFormDataList);
new GeneralResponse(
true, // Set response content type as application/vnd.ms-excel
AppConstants.RESPONSE_SUCCESS_STATUS_CODE, response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
"Excel should be downloaded",
null // Set a downloadable filename
), response.setHeader("Content-Disposition", "attachment; filename=\"master-interaction-excel.xls\"");
HttpStatus.OK);
// Write the Excel data to the response output stream
response.getOutputStream().write(excelData);
// String output = templateService
// .generateMasterInteractionTemplate(admissionFormDataList);
//
// ByteArrayInputStream pdfStream = pdfService.convertHtmlToPdf(output);
//
// response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
// response.setHeader("Content-Disposition", "attachment; filename=master-interaction-template.pdf");
// IOUtils.copy(pdfStream, response.getOutputStream());
} }
@GetMapping( @GetMapping(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment