Save console output from an excel to a txt file

-1

My code reads an excel sheet and returns me a list of IDs, but when I try to save the output that appears on the console to a txt file it returns me null. Does anyone know what?

while ((output = buffer.readLine()) != null) {

                                if (output.contentEquals("# rc=0, count=0, message=Success")) {

                                    FileOutputStream f = new FileOutputStream("file2.txt"); 
                                    System.setOut(new PrintStream(f));
                                    System.out.println(row.getCell(0));


                                }
                            }
    
asked by anonymous 20.11.2018 / 15:07

1 answer

0

I could not figure out how to open the spreadsheet, but I did an example using the apache poi library:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;

public class AbreExcel {

    private static final String planilha = "/home/az/Documents/lista_id.xls";
    private static final String arquivoTxt = "/home/az/Documents/listaId.txt";

    public static void main(String[] args) throws IOException {
        List<String> listId = getListaId();
        gravarTxt(listId);
        System.out.println("Arquivo txt gerado");

    }

    public static List<String> getListaId() throws IOException{
        List<String> listId = new ArrayList<String>();

        try {
            FileInputStream arquivo = new FileInputStream(new File(AbreExcel.planilha));

            HSSFWorkbook workbook = new HSSFWorkbook(arquivo);

            HSSFSheet sheetIds = workbook.getSheetAt(0);

            Iterator<Row> rowIterator = sheetIds.iterator();

            //percorre as linhas da plhanilha
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();

                String id = "";
                //percorre as colunas da plhanilha
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    DataFormatter formatter = new DataFormatter();
                    switch (cell.getColumnIndex()) {
                    //pega a primeira coluna da planilha
                    case 0:
                        id = formatter.formatCellValue(cell);
                        break;
                    }
                }
                //se a celula possuir informação, adiciona na lista
                if(id != null && id.trim().length() != 0)
                    listId.add(id);
            }
            arquivo.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("Arquivo Excel não encontrado!");
        }

        return listId;
    }

    public static void gravarTxt(List<String> listId) throws IOException{
        FileWriter arq = new FileWriter(arquivoTxt);
        PrintWriter gravarArq = new PrintWriter(arq);

        //para cada item da lista grava um id no arquivo txt
        for (String id : listId) {
            gravarArq.printf(id+"\n");
        }
        arq.close();
    }
}
    
20.11.2018 / 16:24