Help with my converter to csv

1

I am doing a converter of xsl and xlsx to csv but I am having problems with the dates. When a date comes to me in one of the cells, does not it remain in dd/mm/yyyy , if not change it to mm/dd/yyy , any solution?

I am using the library of APACHE POI and Java 7

//Esto es simplemente una clase que se utiliza para transformar xls y xlsx a csv
public class Conversor_a_csv {

public static void echoAsCSV(Sheet sheet, Objeto_Ditto objeto) throws IOException {

    objeto.setMicrodatos(objeto.getExcel() + ".csv");
    File csv = new File(objeto.getMicrodatos());
    BufferedWriter bw;
    bw = new BufferedWriter(new FileWriter(csv));

    String v_Fila = "";

    //https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html
    Row row = null;
    Cell cell = null;
    DataFormatter formatter = new DataFormatter(Locale.FRENCH);
    DateFormat formatterDate = new SimpleDateFormat("dd/MM/yyyy");
    Date v_Fecha;
    for (int i = 0; i <= sheet.getLastRowNum(); i++) {
        v_Fila = "";
        row = sheet.getRow(i);
        if (row != null) {
            for (int x = 0; x < row.getLastCellNum(); x++) {
                cell = row.getCell(x);
                //Comprovamos que la celda es tipo fecha
                if(cell != null && cell.getCellTypeEnum() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell)){
                    //Aqui tocaria tratrar la celda para que nos la copie de forma correcta
                    v_Fecha = cell.getDateCellValue();
                    v_Fila = v_Fila + formatterDate.format(v_Fecha) + ";";
                }else{
                    v_Fila = v_Fila + formatter.formatCellValue(cell).trim() + ";";
                }
            }
            bw.write(v_Fila);
            bw.newLine();
        }
    }
    bw.close();
}

/**
 * @param objeto
 * @throws org.apache.poi.openxml4j.exceptions.InvalidFormatException
 */
public static void conversor(Objeto_Ditto objeto) throws org.apache.poi.openxml4j.exceptions.InvalidFormatException {
    InputStream inp = null;
    try {
        inp = new FileInputStream(objeto.getExcel());
        Workbook wb = WorkbookFactory.create(inp);
        echoAsCSV(wb.getSheetAt(0), objeto);
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    } finally {
        try {
            inp.close();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }
}
}

It is already solved in the code above. Gradcias.

    
asked by Iván Montero 30.05.2018 в 09:45
source

1 answer

-1

In these lines:

//String excelFormatPattern = DateFormatConverter.convert(Locale.FRENCH, "dd MMMM, yyyy");
//DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, new Locale("es_ES"));

I see that you are missing a comma in the first comment. I do not know if that solves anything. Another thing that I was going to tell you is that instead of DateFormat you can use

 new SimpleDateFormat(dd-MM-yyy);

I have not tried it, it's something I found on the Internet. This link at better help. If you used Java 8 it would be much easier, I think.

    
answered by 30.05.2018 в 11:46