Created
March 30, 2025 17:07
-
-
Save HiroNakamura/dee670131e3a180e699115ff3f77c87a to your computer and use it in GitHub Desktop.
Maven + Apache POI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.inforhomex.generador; | |
/** | |
* Generador de Excel | |
* | |
*/ | |
public class App | |
{ | |
public static void main( String[] args ) | |
{ | |
new ExcelGenerator().generarExcelRedes(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.inforhomex.generador; | |
import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
import org.apache.poi.ss.usermodel.*; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.List; | |
public class ExcelGenerator { | |
public void generarExcelRedes() { | |
// Crear un nuevo libro de trabajo | |
try (HSSFWorkbook workbook = new HSSFWorkbook()) { | |
// Crear una hoja | |
Sheet sheet = workbook.createSheet("Redes"); | |
// Crear fila de encabezados | |
Row headerRow = sheet.createRow(0); | |
// Definir los encabezados | |
String[] headers = {"IP", "Hostname", "DNS"}; | |
// Estilo para los encabezados | |
CellStyle headerStyle = workbook.createCellStyle(); | |
Font font = workbook.createFont(); | |
font.setBold(true); | |
headerStyle.setFont(font); | |
// Crear las celdas de encabezado | |
for (int i = 0; i < headers.length; i++) { | |
Cell cell = headerRow.createCell(i); | |
cell.setCellValue(headers[i]); | |
cell.setCellStyle(headerStyle); | |
} | |
// Obtener la lista de redes | |
List<Redes> listaRedes = getRedes(); | |
// Llenar datos | |
int rowNum = 1; | |
for (Redes red : listaRedes) { | |
Row row = sheet.createRow(rowNum++); | |
// Crear celdas con los datos | |
row.createCell(0).setCellValue(red.getIp()); | |
row.createCell(1).setCellValue(red.getHostname()); | |
row.createCell(2).setCellValue(red.getDns()); | |
} | |
// Ajustar tamaño de columnas automáticamente | |
for (int i = 0; i < headers.length; i++) { | |
sheet.autoSizeColumn(i); | |
} | |
// Escribir el archivo | |
try (FileOutputStream fileOut = new FileOutputStream("redes.xls")) { | |
workbook.write(fileOut); | |
} | |
System.out.println("Archivo Excel creado exitosamente!"); | |
} catch (IOException e) { | |
System.out.println("Error al crear el archivo Excel: " + e.getMessage()); | |
e.printStackTrace(); | |
} | |
} | |
// Clase Redes de ejemplo (ajusta según tu implementación real) | |
private static class Redes { | |
private String ip; | |
private String hostname; | |
private String dns; | |
// Constructor | |
public Redes(String ip, String hostname, String dns) { | |
this.ip = ip; | |
this.hostname = hostname; | |
this.dns = dns; | |
} | |
// Getters | |
public String getIp() { return ip; } | |
public String getHostname() { return hostname; } | |
public String getDns() { return dns; } | |
} | |
// Método de ejemplo para obtener datos | |
private List<Redes> getRedes() { | |
// Aquí iría tu lógica real para obtener los datos | |
// Este es un ejemplo con datos dummy | |
return List.of( | |
new Redes("192.168.1.1", "host1", "dns1"), | |
new Redes("192.168.1.2", "host2", "dns2"), | |
new Redes("192.168.1.3", "host3", "dns3") | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mvn archetype:generate -DgroupId=com.inforhomex.generador -DartifactId=generador -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.inforhomex.generador</groupId> | |
<artifactId>generador</artifactId> | |
<packaging>jar</packaging> | |
<version>1.0-SNAPSHOT</version> | |
<name>generador</name> | |
<url>http://maven.apache.org</url> | |
<properties> | |
<maven.compiler.source>1.8</maven.compiler.source> | |
<maven.compiler.target>1.8</maven.compiler.target> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>3.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.poi</groupId> | |
<artifactId>poi</artifactId> | |
<version>5.2.3</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>2.3.2</version> | |
<configuration> | |
<source>1.6</source> | |
<target>1.6</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.codehaus.mojo</groupId> | |
<artifactId>exec-maven-plugin</artifactId> | |
<version>1.2.1</version> | |
<executions> | |
<execution> | |
<goals> | |
<goal>java</goal> | |
</goals> | |
</execution> | |
</executions> | |
<configuration> | |
<executable>maven</executable> | |
<mainClass>com.inforhomex.generador.App</mainClass> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment