Pregunta

I am trying to read few csv files which are present inside a folder and putting the absolute paths inside a list and then looping to the list and inserting data into excel in different sheets but for some reason only last loop is overwriting everything.

package excel;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class FileReading {

    public static void csvToXLS(String filename, String sheetno) {
        try {
            String csvFile = filename; //csv file address
            String excelFile = "E:\\test.xls"; //xlsx file address
            HSSFWorkbook workBook = new HSSFWorkbook();
            HSSFSheet sheet = workBook.createSheet(sheetno);
            String currentLine = null;
            int RowNum = 0;
            BufferedReader br = new BufferedReader(new FileReader(csvFile));
            while ((currentLine = br.readLine()) != null) {
                String str[] = currentLine.split(",");
                HSSFRow currentRow = sheet.createRow(RowNum);
                RowNum++;
                for (int i = 0; i < str.length; i++) {
                    currentRow.createCell(i).setCellValue(str[i]);
                }
            }
            FileOutputStream fileOutputStream = new FileOutputStream(excelFile);
            workBook.write(fileOutputStream);
            fileOutputStream.close();
            System.out.println("Done");
        } catch (Exception ex) {
            System.out.println(ex.getMessage() + "Exception in try");
        }
    }

    public static List listOfFiles() {
        List<String> results = new ArrayList<String>();
        File[] files = new File("f:\\csv").listFiles();

        for (File file : files) {
            if (file.isFile()) {
                results.add(file.getAbsolutePath());
            }
        }
        return results;
    }

    public static void main(String[] args) {
        List list = listOfFiles();
        int size = list.size();
        for (int i = 0; i < size; i++) {
            csvToXLS(list.get(i).toString(), "sheet" + i + 1);
        }
    }
}
¿Fue útil?

Solución

You're re-creating and saving the Workbook every time you call csvToXLS, so only the last call survives, overwriting all others.

Create the Workbook only once, before you start your for loop, passing it into csvToXLS. then that method will create a new Sheet on an existing Workbook. Then after the for loop ends, you can write out and save the Workbook.

Otros consejos

I did this thanks for the idea @rgettman

package excel;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class CsvToXlx {

    public static void csvToXLSX(HSSFWorkbook workBook,String filename, String sheetno) {
        try {
            String csvFile = filename; //csv file address
            String excelFile = "E:\\test.xls"; //xlsx file address
            //HSSFWorkbook workBook = new HSSFWorkbook();
            HSSFSheet sheet = workBook.createSheet(sheetno);
            String currentLine = null;
            int RowNum = 0;
            BufferedReader br = new BufferedReader(new FileReader(csvFile));
            while ((currentLine = br.readLine()) != null) {
                String str[] = currentLine.split(",");
                HSSFRow currentRow = sheet.createRow(RowNum);
                RowNum++;
                for (int i = 0; i < str.length; i++) {
                    currentRow.createCell(i).setCellValue(str[i]);
                }
            }
            FileOutputStream fileOutputStream = new FileOutputStream(excelFile);
            workBook.write(fileOutputStream);
            fileOutputStream.close();
            System.out.println("Done");
        } catch (Exception ex) {
            System.out.println(ex.getMessage() + "Exception in try");
        }
    }

    public static List listOfFiles() {
        List<String> results = new ArrayList<String>();
        File[] files = new File("f:\\csv").listFiles();

        for (File file : files) {
            if (file.isFile()) {
                results.add(file.getAbsolutePath());
            }
        }
        return results;
    }

    public static void main(String[] args) {
        List list = listOfFiles();
        int size = list.size();
        HSSFWorkbook workBook = new HSSFWorkbook();
        for (int i = 0; i < size; i++) {
            csvToXLSX(workBook,list.get(i).toString(), "sheet" + i + 1);
        }
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top