Generacion de excel con lista de postulantes

parent 909d4b96
......@@ -111,6 +111,12 @@
<!-- <version>2.6.0</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
......
package com.roshka.controller;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
......@@ -24,6 +29,7 @@ import com.roshka.repositorio.TecnologiaRepository;
import com.roshka.utils.Helper;
import com.roshka.utils.PostulantesExcelExporter;
import org.hibernate.jpa.TypedParameterValue;
import org.hibernate.type.StringType;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -80,7 +86,7 @@ public class PostulanteRRHHController {
}
@RequestMapping("/postulantes")
public String postulantes(Model model,
public String postulantes(HttpServletRequest request, Model model,
@RequestParam(required = false)Long tecId,
@RequestParam(required = false)String nombre,
@RequestParam(required = false)EstadoPostulante estado,
......@@ -92,7 +98,7 @@ public class PostulanteRRHHController {
@RequestParam(required = false)Long cargoId,
@RequestParam(required = false)Long convId,
@RequestParam(defaultValue = "0")Integer nroPagina
) {
) throws IOException {
final Integer CANTIDAD_POR_PAGINA = 5;
Pageable page = PageRequest.of(nroPagina,CANTIDAD_POR_PAGINA,Sort.by("id"));
model.addAttribute("tecnologias", tecRepo.findAll());
......@@ -119,12 +125,73 @@ public class PostulanteRRHHController {
if(expInMonths != null && expInMonths > expTotal) continue;
postulantesDTO.add(new PostulanteListaDTO(postulante.getId(), postulante.getNombre(), postulante.getApellido(), postulante.getDisponibilidad(), postulante.getNivelIngles(), expTotal, postulante.getTecnologias(),postulante.getEstadoPostulante(),postulante.getPostulaciones()));
}
model.addAttribute("pages", postulantesPag.getTotalPages());
model.addAttribute("postulantes", postulantesDTO);
String query = request.getQueryString();
model.addAttribute("query", query);
return "postulantes";
}
@RequestMapping("/postulantesExcel")
public void exportPostulantesExcel(HttpServletResponse response, Model model,
@RequestParam(required = false)Long tecId,
@RequestParam(required = false)String nombre,
@RequestParam(required = false)EstadoPostulante estado,
@RequestParam(required = false)Disponibilidad dispo,
@RequestParam(required = false)Long lvlEng,
@RequestParam(required = false)Long lvlTec,
@RequestParam(required = false)Long instId,
@RequestParam(required = false)Long expInMonths,
@RequestParam(required = false)Long cargoId,
@RequestParam(required = false)Long convId,
@RequestParam(defaultValue = "0")Integer nroPagina
) throws IOException {
Pageable page = PageRequest.of(0,Integer.MAX_VALUE,Sort.by("id"));
Page<Postulante> postulantesPag = post.postulantesMultiFiltro(
nombre == null || nombre.trim().isEmpty() ?
new TypedParameterValue(StringType.INSTANCE,null) :
new TypedParameterValue(StringType.INSTANCE,"%"+nombre+"%"),
dispo, lvlEng, lvlTec, tecId, instId,cargoId,page,estado,convId);
List<Postulante> postulantes = postulantesPag.getContent();
List<PostulanteListaDTO> postulantesDTO = new ArrayList<>();
for (Postulante postulante : postulantes) {
long expTotal = 0;
//Sumamos el tiempo de experiencia total en meses de cada postulante
//expTotal = postulante.getExperiencias().stream().mapToLong(e -> Helper.getMonthsDifference(e.getFechaDesde(), e.getFechaHasta())).sum();
for (Experiencia experiencia : postulante.getExperiencias()) {
expTotal += Helper.getMonthsDifference(experiencia.getFechaDesde(), experiencia.getFechaHasta());
}
if(expInMonths != null && expInMonths > expTotal) continue;
postulantesDTO.add(new PostulanteListaDTO(postulante.getId(), postulante.getNombre(), postulante.getApellido(), postulante.getDisponibilidad(), postulante.getNivelIngles(), expTotal, postulante.getTecnologias(),postulante.getEstadoPostulante(),postulante.getPostulaciones()));
}
response.setContentType("application/octet-stream");
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
String currentDateTime = dateFormatter.format(new Date());
String headerKey = "Content-Disposition";
String headerValue = "attachment; filename=postulantes_" + currentDateTime + ".xlsx";
response.setHeader(headerKey, headerValue);
HashMap<String, String> filtros = new HashMap<String, String>();
filtros.put("nombre", nombre.equals("") ? "-":nombre);
filtros.put("nivelIngles", lvlEng == null ? "-" : lvlEng.toString());
filtros.put("tecnologia", tecId == null ? "-" : tecRepo.findById(tecId).get().getNombre());
filtros.put("nivelTecnologia", lvlTec == null ? "-" : lvlTec.toString());
filtros.put("institucion", instId == null ? "-" : institucionRepository.findById(instId).get().getNombre());
filtros.put("estado", estado == null ? "-" : estado.getEstado());
filtros.put("experienciaEnMeses", expInMonths == null ? "-" : expInMonths.toString());
filtros.put("convocatoria", convId == null ? "-" : cargoRepo.findById(convId).get().getCargo().getNombre());
PostulantesExcelExporter excelExporter = new PostulantesExcelExporter(postulantesDTO, filtros);
excelExporter.export(response);
}
@GetMapping({"/postulantes/{postulanteId}"})
......
package com.roshka.utils;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import com.roshka.DTO.PostulanteListaDTO;
import com.roshka.modelo.PostulanteTecnologia;
import com.roshka.modelo.Tecnologia;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class PostulantesExcelExporter {
private XSSFWorkbook workbook;
private XSSFSheet sheet;
private List<PostulanteListaDTO> listUsers;
private HashMap<String, String> filtros;
public PostulantesExcelExporter(List<PostulanteListaDTO> listUsers, HashMap<String, String> filtros) {
this.listUsers = listUsers;
workbook = new XSSFWorkbook();
this.filtros = filtros;
}
private void writeHeaderLine() {
sheet = workbook.createSheet("Postulantes");
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
String currentDateTime = dateFormatter.format(new Date());
Row row = sheet.createRow(0);
CellStyle style = workbook.createCellStyle();
XSSFFont font = workbook.createFont();
font.setBold(true);
font.setFontHeight(14);
style.setFont(font);
createCell(row, 0, "Postulantes "+currentDateTime, style);
row = sheet.createRow(2);
createCell(row, 0, "Filtros", style);
row = sheet.createRow(3);
createCell(row, 0, "Nombre", style);
createCell(row, 1, "Nivel de Ingles", style);
createCell(row, 2, "Experiencia (Meses)", style);
createCell(row, 3, "Tecnologias", style);
createCell(row, 4, "Nivel Tecnologia", style);
createCell(row, 5, "Institucion", style);
createCell(row, 6, "Estado", style);
createCell(row, 7, "Convocatoria", style);
row = sheet.createRow(6);
style = workbook.createCellStyle();
font = workbook.createFont();
font.setBold(true);
font.setFontHeight(14);
style.setFont(font);
createCell(row, 0, "Nombre", style);
createCell(row, 1, "Nivel de Ingles", style);
createCell(row, 2, "Experiencia", style);
createCell(row, 3, "Tecnologias", style);
createCell(row, 4, "Estado", style);
row = sheet.createRow(4);
font.setBold(false);
font.setFontHeight(12);
style.setFont(font);
createCell(row, 0, filtros.get("nombre"), style);
createCell(row, 1, filtros.get("nivelIngles"), style);
createCell(row, 2, filtros.get("experienciaEnMeses"), style);
createCell(row, 3, filtros.get("tecnologia"), style);
createCell(row, 4, filtros.get("nivelTecnologia"), style);
createCell(row, 5, filtros.get("institucion"), style);
createCell(row, 6, filtros.get("estado"), style);
createCell(row, 7, filtros.get("convocatoria"), style);
}
private void createCell(Row row, int columnCount, Object value, CellStyle style) {
sheet.autoSizeColumn(columnCount);
Cell cell = row.createCell(columnCount);
if (value instanceof Integer) {
cell.setCellValue((Integer) value);
} else if (value instanceof Boolean) {
cell.setCellValue((Boolean) value);
}else {
cell.setCellValue((String) value);
}
cell.setCellStyle(style);
}
private void writeDataLines() {
int rowCount = 7;
CellStyle style = workbook.createCellStyle();
XSSFFont font = workbook.createFont();
font.setFontHeight(12);
style.setFont(font);
for (PostulanteListaDTO user : listUsers) {
Row row = sheet.createRow(rowCount++);
int columnCount = 0;
StringBuilder tecno = new StringBuilder();
for(PostulanteTecnologia tecnologia: user.getTecnologias()){
tecno.append(tecnologia.getTecnologia().getNombre()).append(" ");
}
createCell(row, columnCount++, user.getNombre() + " " + user.getApellido(), style);
createCell(row, columnCount++, user.getNivelIngles().intValue(), style);
createCell(row, columnCount++, user.getExperienciaMeses().intValue(), style);
createCell(row, columnCount++, tecno.toString(), style);
createCell(row, columnCount++, user.getEstado().getEstado(), style);
tecno.delete(0, tecno.length()-1);
}
}
public void export(HttpServletResponse response) throws IOException {
writeHeaderLine();
writeDataLines();
ServletOutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
workbook.close();
outputStream.close();
}
}
\ No newline at end of file
......@@ -165,6 +165,16 @@
</div>
</div>
</form>
<div class="row">
<div class="col-md-12">
<a href="/postulantesExcel?${query}" type="button" class="btn btn-light float-end">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-file-earmark-excel-fill" viewBox="0 0 16 16">
<path d="M9.293 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V4.707A1 1 0 0 0 13.707 4L10 .293A1 1 0 0 0 9.293 0zM9.5 3.5v-2l3 3h-2a1 1 0 0 1-1-1zM5.884 6.68 8 9.219l2.116-2.54a.5.5 0 1 1 .768.641L8.651 10l2.233 2.68a.5.5 0 0 1-.768.64L8 10.781l-2.116 2.54a.5.5 0 0 1-.768-.641L7.349 10 5.116 7.32a.5.5 0 1 1 .768-.64z"></path>
</svg>
Excel
</a>
</div>
</div>
</div>
<div class="card text-dark bg-light mt-3">
......
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