From deb7acc4a407f8e976a72c149fe0daf46418f874 Mon Sep 17 00:00:00 2001 From: Melina Gutierrez Date: Mon, 25 Nov 2024 23:30:27 -0400 Subject: [PATCH] =?UTF-8?q?A=C3=B1adir=20generar=20reporte=20excel=20de=20?= =?UTF-8?q?los=20empleados?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../views/EmployeesListView.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java index d910b87..dfffa09 100644 --- a/src/main/java/com/primefactorsolutions/views/EmployeesListView.java +++ b/src/main/java/com/primefactorsolutions/views/EmployeesListView.java @@ -3,15 +3,22 @@ package com.primefactorsolutions.views; import com.primefactorsolutions.model.Employee; import com.primefactorsolutions.service.EmployeeService; import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.html.Anchor; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; +import com.vaadin.flow.server.StreamResource; import com.vaadin.flow.spring.annotation.SpringComponent; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.vaadin.firitin.components.grid.PagingGrid; import com.vaadin.flow.component.grid.GridSortOrder; import com.vaadin.flow.data.provider.SortDirection; import jakarta.annotation.security.PermitAll; import org.springframework.context.annotation.Scope; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.util.List; @SpringComponent @@ -33,9 +40,49 @@ public class EmployeesListView extends BaseView { private void setupView() { configureTable(); getCurrentPageLayout().add(createAddEmployeeButton()); + getCurrentPageLayout().add(createExportButton()); getCurrentPageLayout().add(table); } + private Button createExportButton() { + StreamResource excelResource = new StreamResource("employees.xlsx", this::generateExcel); + Anchor downloadLink = new Anchor(excelResource, "Export Employees"); + downloadLink.getElement().setAttribute("download", true); // Forzar descarga + return new Button("Export to Excel", e -> getCurrentPageLayout().add(downloadLink)); + } + + private ByteArrayInputStream generateExcel() { + List employees = employeeService.findAllEmployees(); + try (Workbook workbook = new XSSFWorkbook(); ByteArrayOutputStream out = new ByteArrayOutputStream()) { + Sheet sheet = workbook.createSheet("Employees"); + Row headerRow = sheet.createRow(0); + String[] headers = {"ID", "First Name", "Last Name", "Status", "Team"}; + for (int i = 0; i < headers.length; i++) { + Cell cell = headerRow.createCell(i); + cell.setCellValue(headers[i]); + CellStyle style = workbook.createCellStyle(); + Font font = workbook.createFont(); + font.setBold(true); + style.setFont(font); + cell.setCellStyle(style); + } + int rowIndex = 1; + for (Employee employee : employees) { + Row row = sheet.createRow(rowIndex++); + row.createCell(0).setCellValue(employee.getId().toString()); + row.createCell(1).setCellValue(employee.getFirstName()); + row.createCell(2).setCellValue(employee.getLastName()); + row.createCell(3).setCellValue(employee.getStatus().toString()); + row.createCell(4).setCellValue(employee.getTeam().toString()); + } + workbook.write(out); + return new ByteArrayInputStream(out.toByteArray()); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + private void configureTable() { table.setColumns("firstName", "lastName", "status"); addEditButtonColumn("View", this::navigateToEmployeeView);