Commit ae4f4a7e authored by Pratik Shrivastava's avatar Pratik Shrivastava

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

parent cb00966a
......@@ -153,6 +153,11 @@
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -17,11 +17,14 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
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.HttpServletResponse;
import javax.validation.Valid;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
......@@ -316,6 +319,38 @@ public class AdmissionFormController {
response.setHeader("Content-Disposition", "attachment; filename=master-interaction-template.pdf");
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(
......@@ -327,7 +362,7 @@ public class AdmissionFormController {
@ApiResponse(code = 500, message = "Exception Occurred")
})
@ApiOperation(value = "")
public Object downloadMasterInteractionExcel(
public void downloadMasterInteractionExcel(
HttpServletRequest request,
HttpServletResponse response,
@RequestParam("apikey") String apiKey,
......@@ -347,25 +382,19 @@ public class AdmissionFormController {
if (admissionFormDataList.isEmpty()) {
throw new Exception("No Admission Data Found");
}
//TODO: write logic to download excel
return new ResponseEntity<>(
new GeneralResponse(
true,
AppConstants.RESPONSE_SUCCESS_STATUS_CODE,
"Excel should be downloaded",
null
),
HttpStatus.OK);
// 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());
// Call the convertAdmissionFormToExcel method to get the Excel data as byte array
byte[] excelData = convertAdmissionFormToExcel(admissionFormDataList);
// Set response content type as application/vnd.ms-excel
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
// Set a downloadable filename
response.setHeader("Content-Disposition", "attachment; filename=\"master-interaction-excel.xls\"");
// Write the Excel data to the response output stream
response.getOutputStream().write(excelData);
}
@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