How to read empty columns of an excel file? [closed]

0

I need help with a problem, I'm doing an application in java (netBeans) which reads an excel xlxs file and inserts it into a table in MySQL the excel file brings 12 columns and of those 12 columns 1 comes empty no data. The problem is that when inserted in the MySQL table in the column that comes empty inserts the data of the next column that is empty is the last column and not the one that comes empty in excel

    
asked by Daniel Vasquez 21.04.2017 в 17:37
source

2 answers

2

Hello, you need to specify which library you are using, if it is Apache Poi, try this:

for(Row row : sheet) {
   for(int cn=0; cn<row.getLastCellNum(); cn++) {

     // Si falta la celda del archivo, genera una casilla en blanco
     // (Funciona especificando un MissingCellPolicy)
       Cell cell = row.getCell(cn, Row.CREATE_NULL_AS_BLANK);


       // Imprimir la celda para depurar
       System.out.println("CELL: " + cn + " --> " + cell.toString());
   }
}

Rerefencia : Iterate over cells, with control of missing / blank cells in Busy Developers' Guide to HSSF and XSSF Features .

    
answered by 21.04.2017 в 17:52
0

Assuming that you already have the sheet, you attach detailed information.

1.- You need the Java library first, you can download it from here.

http://www.java2s.com/Code/Jar/j/Downloadjxljar.htm

2.- You incorporate the Excel file

You select the right button on the project folder \ Properties; "Java Build Path" option; Libraries tab; finally click on "Add External JAR" and select the file.

This is an example, let's see what you think.

Go through each sheet of the excel file. Within each sheet, scroll through all the columns of each row of the excel file and display its contents on the screen.

importjxl. *;
importjava.io. *; 



public class LeerExcel {

    private void leerArchivoExcel(String archivoDestino) {

        try {
            Workbook archivoExcel = Workbook.getWorkbook(new File(
                    archivoDestino));
            System.out.println("Número de Hojas\t"
                    + archivoExcel.getNumberOfSheets());
            for (int sheetNo = 0; sheetNo < archivoExcel.getNumberOfSheets(); sheetNo++) // Recorre las hojas                                                                                                                                                       
            {
                Sheet hoja = archivoExcel.getSheet(sheetNo);
                int numColumnas = hoja.getColumns();
                int numFilas = hoja.getRows();
                String data;
                System.out.println("Nombre de la Hoja\t"
                        + archivoExcel.getSheet(sheetNo).getName());
                for (int fila = 0; fila < numFilas; fila++) { // Recorre cada 
                    // fila de la 
                    // hoja 
                    for (int columna = 0; columna < numColumnas; columna++) {
                        // Recorre                                                                                
                        // cada                                                                                
                        // columna                                                                            
                        // de                                                                                
                        // la                                                                                
                        // fila 
                        data = hoja.getCell(columna, fila).getContents();
                        System.out.print(data + " ");

                    }
                    System.out.println("\n");
                }
            }
        } catch (Exception ioe) {
            ioe.printStackTrace();
        }

    }

    public static void main(String arg[]) {
        ReadExcel excel = new ReadExcel();
        excel.leerArchivoExcel("archivoPrueba.xls");
    }
}
    
answered by 21.04.2017 в 17:53