Problema com a leitura da data em xls

2 respostas
stilldp

Ola estou fazendo uma rotina onde le um xls e grava num txt
O problema seguinte o arquivo que estou abrindo esta normal e a data é campo data do eccel 95-2003

mas na hora de gravar o campo data no txt ele ta saindo assim:

3/8/110

Sendo que na verdade era pra vim o valor:

01/09/2010

Segue o meu codico que esta com a saida 3/8/110

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class App 
{
    public static void main( String[] args ) throws ParseException, FileNotFoundException, IOException
    {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("C:/exemplo/ArquivoQuestor.txt")), "UTF-8"));

        HSSFWorkbook wb = null;
        try {
            wb = new HSSFWorkbook(new FileInputStream("C:/exemplo/arquivo 092010.xls"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        HSSFSheet sheet = wb.getSheet("Plan1");

        for (int linha_inicial=1; linha_inicial<=sheet.getPhysicalNumberOfRows(); linha_inicial++) {
            HSSFRow row = sheet.getRow(linha_inicial);
            if (row == null) {
                break;
            }

            HSSFCell data = row.getCell(0);
            HSSFCell numero_documento = row.getCell(5);
            HSSFCell cod_debito = row.getCell(1);
            HSSFCell cod_credito = row.getCell(3);
            HSSFCell valor = row.getCell(6);
            HSSFCell historico = row.getCell(7);
            HSSFCell historico_complemento = row.getCell(8);

            Date dData = data.getDateCellValue();
            StringBuilder dataStr = new StringBuilder(dData.getDate())
                    .append(dData.getDay())
                    .append("/")
                    .append(dData.getMonth())
                    .append("/")
                    .append(dData.getYear());

            String valorCredito = String.format("%.2f", cod_credito.getNumericCellValue());
            String valorDebito = String.format("%.2f", cod_debito.getNumericCellValue());


            StringBuilder linha = new StringBuilder();
            linha.append("C;"); //1
            linha.append("00007;"); //5
            linha.append(dataStr); //10
            linha.append(";");
            linha.append(String.format("%-10s", numero_documento)); 
            linha.append(";");
            linha.append(String.format("%11s", valorDebito.replace(",", "")).replace(" ", "0")); 
            linha.append(";");
            linha.append(String.format("%11s", valorCredito.replace(",", "")).replace(" ", "0"));
            linha.append(";");
            linha.append(String.format("%16s", valor).replace(" ", "0"));
            linha.append(";");
            linha.append(String.format("%5s", historico).replace(" ", "0")); //5
            linha.append(";");
            linha.append(String.format("%-300s", historico_complemento)); //5
            linha.append("\r\n");


            writer.write(linha.toString());
            //System.out.println(linha.toString());

         } writer.close();

    }

    public static String getCellStringValue(HSSFCell cell) {
        String value = null;

        try {
            switch (cell.getCellType()) {
                case HSSFCell.CELL_TYPE_FORMULA:
                        value = cell.getCellFormula();
                        break;

                case HSSFCell.CELL_TYPE_NUMERIC:
                        value = String.valueOf(cell.getNumericCellValue());
                        break;

                case HSSFCell.CELL_TYPE_STRING:
                        value = cell.getStringCellValue();
                        break;

                default:
            }

        } catch (Exception e) {
            //e.printStackTrace();
            value = "";
        }

        return value;
    }
}

Se eu pega data direto sem o tratamento.
tirando esse tratamento:

Date dData = data.getDateCellValue();
            StringBuilder dataStr = new StringBuilder(dData.getDate())
                    .append(dData.getDay())
                    .append("/")
                    .append(dData.getMonth())
                    .append("/")
                    .append(dData.getYear());

ele tem uma saida no txt:
01-set-2010

Ja pesquisei na internet e não achei... alguem ai ja passou por isso poderia me ajudar?

2 Respostas

Eric_Yuzo

Acho que um DateFormat resolve seu problema.

DateFoarmat df = new SimpleDateFormat("dd/MM/yyyy"); String data = df.format(dData);

stilldp

Obrigado deu certo ! :lol: tinha esquecido de agradecer ^^

Criado 22 de outubro de 2010
Ultima resposta 29 de out. de 2010
Respostas 2
Participantes 2