Guest User

Untitled

a guest
Feb 14th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.util.Iterator;
  7. import org.apache.poi.ss.usermodel.Cell;
  8. import org.apache.poi.ss.usermodel.CellStyle;
  9. import org.apache.poi.ss.usermodel.Font;
  10. import org.apache.poi.ss.usermodel.Row;
  11. import org.apache.poi.xssf.usermodel.XSSFCell;
  12. import org.apache.poi.xssf.usermodel.XSSFRow;
  13. import org.apache.poi.xssf.usermodel.XSSFSheet;
  14. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  15.  
  16. public class Leer {
  17.  
  18. public static void main(String[] args) {
  19.  
  20. //EscribirEXCEL();
  21.  
  22. LeerEXCEL();
  23.  
  24. }
  25.  
  26. private static void LeerEXCEL() {
  27.  
  28. String nombreArchivo = "ListaUsuarios.xlsx";
  29. String hoja = "Usuarios";
  30.  
  31. try(FileInputStream file = new FileInputStream(new File(nombreArchivo))){
  32. //Leer archivo de Excel
  33. XSSFWorkbook libro = new XSSFWorkbook(file);
  34. // Obtener la hoja que se va a leer
  35. XSSFSheet sheet = libro.getSheetAt(0);
  36. // Obtener todas las filas de la hoja de Excel
  37. Iterator<Row> rowIterator = sheet.iterator();
  38.  
  39. Row row;
  40. // Se recorre cada fila hasta el final
  41. while(rowIterator.hasNext()) {
  42. row = rowIterator.next();
  43. // Se obtienen las celdas por fila
  44. Iterator<Cell> cellIterator = row.cellIterator();
  45. Cell cell;
  46. // Se recorre cada celda
  47. while(cellIterator.hasNext()) {
  48. // Se obtiene la celda en especifico y se imprime
  49. cell = cellIterator.next();
  50. System.out.print(cell.getStringCellValue()+ " - ");
  51. }
  52. System.out.println("");
  53. }
  54.  
  55. } catch(Exception e) {
  56. e.getMessage();
  57. }
  58. }
  59.  
  60. private static void EscribirEXCEL() {
  61. String nombreArchivo = "ListaUsuarios.xlsx";
  62.  
  63. String hoja = "Usuarios";
  64.  
  65. XSSFWorkbook libro = new XSSFWorkbook();
  66. XSSFSheet hoja1 = libro.createSheet(hoja);
  67.  
  68. // Cabecera de la hoja de excel
  69. String[] header = new String[] {"NOMBRE", "TELEFONO", "EMAIL"};
  70.  
  71. // Contenido de la hoja de excel
  72. String[][] document = new String[][] {
  73. {"Sergio P", "1234567", "sergiop@prueba.es"},
  74. {"Laura L", "4324251", "laural@prueba.es"},
  75. {"Juan H", "7363153", "juanh@prueba.es"}
  76. };
  77.  
  78. // Poner en negrita la cabecera
  79. CellStyle style = libro.createCellStyle();
  80. Font font = libro.createFont();
  81. font.setBold(true);
  82. style.setFont(font);
  83.  
  84. // Generar los datos para el documento
  85. for(int i = 0 ; i <= document.length ; i++) {
  86. XSSFRow row = hoja1.createRow(i); // Se crea la fila
  87. for(int j = 0 ; j < header.length ; j++) {
  88. if(i == 0) { // Para la cabecera
  89. XSSFCell cell = row.createCell(j); // Se crean las celdas pra la cabecera
  90. cell.setCellValue(header[j]); // Se añade el contenido
  91. } else {
  92. XSSFCell cell = row.createCell(j); // Se crean las celdas para el contenido
  93. cell.setCellValue(document[i - 1][j]); // Se añade el contenido
  94. }
  95. }
  96. }
  97.  
  98. // Crear el archivo
  99. try (OutputStream fileOut = new FileOutputStream(nombreArchivo)){
  100. System.out.println("SE CREO EL EXCEL");
  101. libro.write(fileOut);
  102. } catch(IOException e) {
  103. e.printStackTrace();
  104. }
  105. }
  106. }
  107.  
  108. NOMBRE TELEFONO EMAIL
  109. Sergio P 1234567 sergiop@prueba.es
  110. Laura L 4324251 laural@prueba.es
  111. Juan H 7363153 juanh@prueba.es
  112. Luis 12345 luis@prueba.es
  113.  
  114. NOMBRE - TELEFONO - EMAIL -
  115. Sergio P - 1234567 - sergiop@prueba.es -
  116. Laura L - 4324251 - laural@prueba.es -
  117. Juan H - 7363153 - juanh@prueba.es -
  118. Luis -
Add Comment
Please, Sign In to add comment