Browse Source

Se agrego el componente de upload a la vista y tambien la funcion para que inserte el archivo en la tabla desde el upload

master
mramirezg 9 months ago
parent
commit
151c247ab3
4 changed files with 35 additions and 9 deletions
  1. +5
    -0
      pom.xml
  2. +0
    -5
      src/main/java/mx/gob/jumapacelaya/Application.java
  3. +4
    -3
      src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java
  4. +26
    -1
      src/main/java/mx/gob/jumapacelaya/ui/PlanAnualView.java

+ 5
- 0
pom.xml View File

@ -133,6 +133,11 @@
<artifactId>poi-ooxml</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.16.1</version>
</dependency>
</dependencies>
<build>


+ 0
- 5
src/main/java/mx/gob/jumapacelaya/Application.java View File

@ -19,11 +19,6 @@ public class Application implements AppShellConfigurator {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
DatabaseService databaseService = new DatabaseService();
String archivo = "C:/Users/mramirezg/Documents/MRAMIREZG/Libro1 PRUEBA.xlsx";
databaseService.insertarDesdeExcel(archivo);
}
}

+ 4
- 3
src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java View File

@ -7,6 +7,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.swing.plaf.PanelUI;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
@ -230,14 +231,14 @@ public class DatabaseService {
}
public void insertarDesdeExcel(String rutaArchivoExcel) {
public void insertarDesdeExcel(InputStream inputStream) {
String query = "INSERT INTO MTTOPROGRAMADOS (NOMEQUIPO, DEPARTAMENTO, MONITOR, TECLADO, MOUSE, " +
"REGULADOR, CPU, IMPRESORA, MINIPRINT, LAPTOP, ESCANER, FECHAPROG, TECNICOSMT, ESTADO) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (Connection connection = getConnection();
FileInputStream file = new FileInputStream(rutaArchivoExcel);
Workbook workbook = new XSSFWorkbook(file)) {
//FileInputStream file = new FileInputStream(rutaArchivoExcel);
Workbook workbook = new XSSFWorkbook(inputStream)) {
Sheet sheet = workbook.getSheetAt(0); // Primera hoja del archivo Excel


+ 26
- 1
src/main/java/mx/gob/jumapacelaya/ui/PlanAnualView.java View File

@ -8,14 +8,19 @@ import com.vaadin.flow.component.html.H5;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.notification.NotificationVariant;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.component.upload.Upload;
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import jakarta.annotation.security.PermitAll;
import mx.gob.jumapacelaya.models.PlanAnual;
import mx.gob.jumapacelaya.services.DatabaseService;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.time.LocalDate;
import java.util.Arrays;
@ -30,6 +35,7 @@ public class PlanAnualView extends VerticalLayout {
HorizontalLayout header = new HorizontalLayout();
H4 titulo = new H4();
H5 titulo1 = new H5();
DatabaseService databaseService = new DatabaseService();
public PlanAnualView() {
VerticalLayout headerLayout = new VerticalLayout();
@ -50,6 +56,25 @@ public class PlanAnualView extends VerticalLayout {
headerLayout.add(titulo, titulo1);
// Componente UPLOAD para subir archivos
MemoryBuffer buffer = new MemoryBuffer();
Upload upload = new Upload(buffer);
upload.setAcceptedFileTypes(".xls", ".xlsx");
upload.setMaxFiles(1);
upload.setDropLabel(new com.vaadin.flow.component.html.Span("Arrastra un archivo Excel aquí o selecciona uno"));
upload.addSucceededListener(event -> {
try {
InputStream inputStream = buffer.getInputStream();
databaseService.insertarDesdeExcel(inputStream);
Notification.show("Archivo insertado correctamente").addThemeVariants(NotificationVariant.LUMO_SUCCESS);
} catch (Exception e) {
Notification.show("Error al insertar el archivo: " + e.getMessage()).addThemeVariants(NotificationVariant.LUMO_ERROR);
}
});
Grid<PlanAnual> planAnualGrid = new Grid<>();
planAnualGrid.addColumn(PlanAnual::getNumero).setHeader("No.");
planAnualGrid.addColumn(PlanAnual::getNomEquipo).setHeader("Equipo").setAutoWidth(true);
@ -100,7 +125,7 @@ public class PlanAnualView extends VerticalLayout {
this.setPadding(false);
this.setMargin(false);
this.setSpacing(false);
add(headerLayout, gridLayout);
add(headerLayout, gridLayout, upload);
}
private Icon getIcon(String value) {


Loading…
Cancel
Save