Save console output from an excel to a txt file

Asked

Viewed 79 times

-1

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

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));


                                }
                            }
  • as you are opening the spreadsheet?

1 answer

0

I could not understand how you open the spreadsheet, but I made an example using the library apache poi:

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();
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.