arreglar-funcionalidad #84
@ -3,15 +3,22 @@ package com.primefactorsolutions.views;
|
|||||||
import com.primefactorsolutions.model.Employee;
|
import com.primefactorsolutions.model.Employee;
|
||||||
import com.primefactorsolutions.service.EmployeeService;
|
import com.primefactorsolutions.service.EmployeeService;
|
||||||
import com.vaadin.flow.component.button.Button;
|
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.PageTitle;
|
||||||
import com.vaadin.flow.router.Route;
|
import com.vaadin.flow.router.Route;
|
||||||
|
import com.vaadin.flow.server.StreamResource;
|
||||||
import com.vaadin.flow.spring.annotation.SpringComponent;
|
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 org.vaadin.firitin.components.grid.PagingGrid;
|
||||||
import com.vaadin.flow.component.grid.GridSortOrder;
|
import com.vaadin.flow.component.grid.GridSortOrder;
|
||||||
import com.vaadin.flow.data.provider.SortDirection;
|
import com.vaadin.flow.data.provider.SortDirection;
|
||||||
import jakarta.annotation.security.PermitAll;
|
import jakarta.annotation.security.PermitAll;
|
||||||
import org.springframework.context.annotation.Scope;
|
import org.springframework.context.annotation.Scope;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@SpringComponent
|
@SpringComponent
|
||||||
@ -33,9 +40,49 @@ public class EmployeesListView extends BaseView {
|
|||||||
private void setupView() {
|
private void setupView() {
|
||||||
configureTable();
|
configureTable();
|
||||||
getCurrentPageLayout().add(createAddEmployeeButton());
|
getCurrentPageLayout().add(createAddEmployeeButton());
|
||||||
|
getCurrentPageLayout().add(createExportButton());
|
||||||
getCurrentPageLayout().add(table);
|
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<Employee> 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() {
|
private void configureTable() {
|
||||||
table.setColumns("firstName", "lastName", "status");
|
table.setColumns("firstName", "lastName", "status");
|
||||||
addEditButtonColumn("View", this::navigateToEmployeeView);
|
addEditButtonColumn("View", this::navigateToEmployeeView);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user