0

tengo un formulario con un jtable el cual carga un archivo de excel dentro de el, el problema es que no me carga las ñs o caracteres especiales introducir la descripción de la imagen aquí

Me salen esos símbolos de pregunta. este el el codigo del boton agregar

private void btn_importarActionPerformed(java.awt.event.ActionEvent evt) {                                             
    try {
        for (int i = 0; i < jtabla.getRowCount(); i++) {
            modelo.removeRow(i);
            i -= 1;
        }
    } catch (Exception e) {
    }

    JFileChooser fileChooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Archivos Excel (*.xls)", "xls");
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.setFileFilter(filter);
    fileChooser.setDialogTitle("BUSCAR ARCHIVO");
    if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        String ruta = fileChooser.getSelectedFile().getAbsolutePath();
        File f = new File(ruta);
        try {
            Workbook wb = Workbook.getWorkbook(f);
            Sheet s = wb.getSheet(0);
            int row = s.getRows();
            int col = s.getColumns();
            String[] datos;
            for (int i = 0; i < row; i++) {
                datos = new String[col];
                for (int j = 0; j < col; j++) {
                    Cell c = s.getCell(j, i);
                    datos[j] = c.getContents();
                    System.out.print(c.getContents());
                }
                System.out.println("");
                if (i == 0) {
                    modelo.setColumnIdentifiers(datos);
                } else {
                    modelo.addRow(datos);
                }

            }
            jtabla.setModel(modelo);
        } catch (Exception e) {
        }
    }
}      

Ya estuve investigando sobre convertir a UTF-8 pero no se en que momento deberia hacerlo y como. intente convertir el archivo FILE o al momento del getContents pero no me da resultado ninguna forma.

Gracias de antemano

2
  • puedes hacer String content = new String(c.getContents.getBytes("UTF-8"));
    – Bryro
    el 6 may. 2019 a las 0:19
  • Ya lo intente y no me funciona tampoco el 6 may. 2019 a las 3:43

1 respuesta 1

1

Encontré la Solución

tuve que cambiar las librerías a usar, en la pregunta estuve usando la jxl pero después use poi-3.7-20101029.jar

este es el código

Librerías

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Row;

Boton

DecimalFormat formatDecimal = new DecimalFormat("#");

private void btn_importarActionPerformed(java.awt.event.ActionEvent evt) {                                             

    try {
        for (int i = 0; i < jtabla.getRowCount(); i++) {
            modelo.removeRow(i);
            i -= 1;
        }
    } catch (Exception e) {
    }

    JFileChooser fileChooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Archivos Excel (*.xls)", "xls");
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.setFileFilter(filter);
    fileChooser.setDialogTitle("BUSCAR ARCHIVO");
    if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        String ruta = fileChooser.getSelectedFile().getAbsolutePath();
        File f = new File(ruta);

        try {
            FileInputStream input = new FileInputStream(ruta);
            POIFSFileSystem fs = new POIFSFileSystem(input);
            HSSFWorkbook wb = new HSSFWorkbook(fs);

            HSSFSheet sheet = wb.getSheetAt(0);
            Row row;
            String[] datos;

            for (int i = 0; i <= sheet.getLastRowNum(); i++) {
                row = sheet.getRow(i);
                int cols = row.getPhysicalNumberOfCells();
                datos = new String[cols];

                for (int j = 0; j < cols; j++) {
                    try {
                        datos[j] = row.getCell(j).getStringCellValue();
                    } catch (Exception e) {
                        datos[j] = formatDecimal.format(row.getCell(j).getNumericCellValue());
                    }
                }
                if (i == 0) {
                    modelo.setColumnIdentifiers(datos);
                } else {
                    modelo.addRow(datos);
                }
            }
            jtabla.setModel(modelo);
        } catch (Exception e) {
            System.out.println("general " + e);
        }
    }
}                                            

tuve que usar el decimalformat para los datos numéricos enteros porque me daba error

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.