Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created March 23, 2022 23:05
Show Gist options
  • Save parzibyte/173a6fa9b6556dabacca9bfa7da8bdfc to your computer and use it in GitHub Desktop.
Save parzibyte/173a6fa9b6556dabacca9bfa7da8bdfc to your computer and use it in GitHub Desktop.
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template
*/
package me.parzibyte.escribirexcel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
*
* @author parzibyte
*/
public class EscribirExcel {
public static void main(String[] args) {
Workbook libro = new XSSFWorkbook();
final String nombreArchivo = "Mi archivo creado con Java.xlsx";
Sheet hoja = libro.createSheet("Hoja 1");
Row primeraFila = hoja.createRow(0);
Cell primeraCelda = primeraFila.createCell(0);
primeraCelda.setCellValue("Yo voy en la primera celda y primera fila");
File directorioActual = new File(".");
String ubicacion = directorioActual.getAbsolutePath();
String ubicacionArchivoSalida = ubicacion.substring(0, ubicacion.length() - 1) + nombreArchivo;
FileOutputStream outputStream;
try {
outputStream = new FileOutputStream(ubicacionArchivoSalida);
libro.write(outputStream);
libro.close();
System.out.println("Libro guardado correctamente");
} catch (FileNotFoundException ex) {
System.out.println("Error de filenotfound");
} catch (IOException ex) {
System.out.println("Error de IOException");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment