Ler csv e escrever outro

0 respostas
J

Boa tarde, estou tentando fazer uma classe, onde se le um excel e escreve outro com uma linha a mais.

Porém não entendi muito bem como funciona linhas e colunas então não esta dando certo.

Alguém poderia me ajudar no código?

`

public class LerExcel {

public static void main( String [] args ) {
   
        //Esta planilha tem 5 linhas de exemplo e esta em csv.  
        String csvFile = "C:\\Users\\Desktop\\teste3.csv";
        BufferedReader br = null;
        String cvsSplitBy = ",";
        String transformeisso = "";

        try {

            br = new BufferedReader(new FileReader(csvFile));
            
            Workbook workbook = new XSSFWorkbook();
            
            Sheet sheet = workbook.createSheet("Arquivo");
            sheet.setColumnWidth(0, 6000);
            sheet.setColumnWidth(1, 4000);
        
            int i = 0;
            while (br.readLine() != null && !br.readLine().equals("") ) {
            	
     
	                String line = br.readLine();		
	                		
	                String[] coluna = line.split(cvsSplitBy);
	                    
	                	if(coluna.length > 1 ) {
	                		transformeisso =  coluna[3];
	                	}
		                   String str = transformeisso.replaceAll("\"", "");
		                    
		                   //Transformar Aqui tem esse método pegaremail para retirar o email de uma string e adicionar em uma nova coluna no final do excel
		                   String emails = pegarEmail(str);
		                   Row header = sheet.createRow(i); 
		                   Cell headerCell = header.createCell(i);
		                   headerCell.setCellValue(line);
		                   Cell headerCellEmail = header.createCell(i+1);
		                   headerCellEmail.setCellValue(emails);
		                   i++;
               

            }
            
            File currDir = new File("C:\\temp");
            String path = currDir.getAbsolutePath();
            String fileLocation = path.substring(0, path.length() - 1) + "novo.xlsx";
             
            FileOutputStream outputStream = new FileOutputStream(fileLocation);
            workbook.write(outputStream);
            workbook.close();

        
         
    } catch ( IOException ex ) {
        ex.printStackTrace();
    }
}

`

Vou deixar aqui como é o excel de entrada e como eu gostaria que fosse a saida.
O metodo pra retirar o email da string funciona certinho… problema eu acho que é as contagens de linhas e colunas que não entendi muito bem como funciona…
Me ajudem por favor:

Como excel esta:

https://uploaddeimagens.com.br/imagens/2-png-86fae7f1-9432-46ff-8d01-08de16ac0518

Como excel deve ficar
https://uploaddeimagens.com.br/imagens/1-png-abd02d01-3518-474e-8f80-b97b10db02d6

Obrigado!

Criado 10 de março de 2019
Respostas 0
Participantes 1