使用Java和Jexcelapi从Excel编写多个CSV文件

使用Java和Jexcelapi从Excel编写多个CSV文件,java,jexcelapi,Java,Jexcelapi,我正在尝试创建一个程序,将excel中的所有数据写入多个csv文件 目前,我的程序创建了n个文件,并且在最后一个csv文件上只写入了excel的最后一行 编程背景观察: 似乎文件正在写入。但对于每一个写入的行,它会以某种方式创建相同的文件250次。它将删除最后写入的行 使用输出流或缓冲写入程序时可能会遇到一些问题。我就是搞不懂那是什么 writeRow()方法: public static void writeRow(BufferedWriter bw, Cell[] row) throws I

我正在尝试创建一个程序,将excel中的所有数据写入多个csv文件

目前,我的程序创建了n个文件,并且在最后一个csv文件上只写入了excel的最后一行

编程背景观察: 似乎文件正在写入。但对于每一个写入的行,它会以某种方式创建相同的文件250次。它将删除最后写入的行

使用输出流缓冲写入程序时可能会遇到一些问题。我就是搞不懂那是什么

writeRow()方法:

public static void writeRow(BufferedWriter bw, Cell[] row) throws IOException { 
    if (row.length > 0) {
        bw.write(row[0].getContents());
        for (int j = 1; j < row.length; j++) {
            bw.write(',');
            bw.write(row[j].getContents());
        }
    }
}
Exception in thread "main" java.io.IOException: Stream closed
at sun.nio.cs.StreamEncoder.ensureOpen(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.BufferedWriter.flush(Unknown Source)
at Main.main(Main.java:46)
以下是例外情况。

public static void writeRow(BufferedWriter bw, Cell[] row) throws IOException { 
    if (row.length > 0) {
        bw.write(row[0].getContents());
        for (int j = 1; j < row.length; j++) {
            bw.write(',');
            bw.write(row[j].getContents());
        }
    }
}
Exception in thread "main" java.io.IOException: Stream closed
at sun.nio.cs.StreamEncoder.ensureOpen(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.BufferedWriter.flush(Unknown Source)
at Main.main(Main.java:46)
下面是代码的一些链接

我做错了什么

这是svn主干:

-

  • 就像上面的那个

  • 重要建议: 不要在同一个缓冲区中写入,而是在每n个记录之后停止行。然后,读一读 请重新打开工作表。然后,在另一个文件上输出缓冲区。在同一缓冲区上写入时,无法更改文件

    下面是一个个完整的解决方案:

    静态变量:

    public static BufferedWriter setFile (int i) throws IOException {
        i=i/250;
        File f = new File(dir + "file-" + (i+1) + ".csv");
        // If i has changed, create a new file. Else, append to current file.
        String encoding = "UTF8";
        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        Boolean append = null;
        try {
            // If i has changed, create a new file, else append.
            if (i%250==0) {
                append = new Boolean("TRUE");
                os = new FileOutputStream(f,append);
            } else {
                append = new Boolean("FALSE");
                os = new FileOutputStream(f, append);
            }
            osw = new OutputStreamWriter(os, encoding);
            bw = new BufferedWriter(osw);
    
        } finally {
            os.close();
            osw.close();
        }
        return bw;
    }
    
    public static int maxRecords = 250;
    public static String directory = "C:\\Users\\User02\\workspace\\ExcelToCsv\\src\\";
    public static String inputFile = directory + "inventory.xls";
    
    public static void main(String[] args) throws BiffException, IOException {
    Sheet s = getSheet();
        int countRows = s.getRows(); // counts the number of rows in the sheet.
        int numberOfFiles = (countRows/maxRecords)+1;
    
        for(int file=0; file<numberOfFiles; file++) {
            System.out.println("Create file number " + (file+1));
            int fileNumber = file+1;
            System.out.println("Start number: " + ((file*maxRecords)+1));
            int startNumber = (file*maxRecords);
            populateFile(fileNumber,startNumber);
            System.out.println("");
        }
    }
    
    public static void populateFile(int fileNumber, int startNumber) 
        throws IOException, BiffException {
            BufferedWriter bw = setFile(fileNumber);
            Sheet s = getSheet();
            Cell[] row = null;
            writeRow(bw,s.getRow(0));
            bw.newLine(); 
            int limit = getLimit(s,startNumber);
            System.out.println("End Number:" + limit);
            System.out.println();
            for (int i = startNumber; i < limit ; i++) {
                row = s.getRow(i); 
                //System.out.println(i);
                writeRow(bw,row);
                bw.newLine();
            }
            bw.flush();
            bw.close();
        }
    
    public static Sheet getSheet() throws BiffException, IOException {
        WorkbookSettings ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));
        Workbook w = Workbook.getWorkbook(new File(inputFile),ws);
        Sheet s = w.getSheet(0);
        return s;
    }
    
    public static BufferedWriter setFile(int fileNumber) throws IOException {
        String csvFilename = directory + "file-"+ fileNumber +".csv";
        FileWriter csvFile = new FileWriter(csvFilename);
        BufferedWriter bw = new BufferedWriter(csvFile);
        return bw;
    }
    
    public static int getLimit(Sheet s, int startNumber) {
        int limit;
        int countRows = s.getRows();
        if (startNumber+maxRecords<=countRows) {
            limit = startNumber + maxRecords;
        } else {
            limit = startNumber + (countRows-startNumber);
        }
        return limit;
    }
    
    public static void writeRow(BufferedWriter bw, Cell[] row) throws IOException { 
        if (row.length > 0) {
            bw.write(row[0].getContents());
            for (int j = 1; j < row.length; j++) {
                bw.write(',');
                bw.write(row[j].getContents());
            }
        }
    }
    
    Main:

    public static BufferedWriter setFile (int i) throws IOException {
        i=i/250;
        File f = new File(dir + "file-" + (i+1) + ".csv");
        // If i has changed, create a new file. Else, append to current file.
        String encoding = "UTF8";
        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        Boolean append = null;
        try {
            // If i has changed, create a new file, else append.
            if (i%250==0) {
                append = new Boolean("TRUE");
                os = new FileOutputStream(f,append);
            } else {
                append = new Boolean("FALSE");
                os = new FileOutputStream(f, append);
            }
            osw = new OutputStreamWriter(os, encoding);
            bw = new BufferedWriter(osw);
    
        } finally {
            os.close();
            osw.close();
        }
        return bw;
    }
    
    public static int maxRecords = 250;
    public static String directory = "C:\\Users\\User02\\workspace\\ExcelToCsv\\src\\";
    public static String inputFile = directory + "inventory.xls";
    
    public static void main(String[] args) throws BiffException, IOException {
    Sheet s = getSheet();
        int countRows = s.getRows(); // counts the number of rows in the sheet.
        int numberOfFiles = (countRows/maxRecords)+1;
    
        for(int file=0; file<numberOfFiles; file++) {
            System.out.println("Create file number " + (file+1));
            int fileNumber = file+1;
            System.out.println("Start number: " + ((file*maxRecords)+1));
            int startNumber = (file*maxRecords);
            populateFile(fileNumber,startNumber);
            System.out.println("");
        }
    }
    
    public static void populateFile(int fileNumber, int startNumber) 
        throws IOException, BiffException {
            BufferedWriter bw = setFile(fileNumber);
            Sheet s = getSheet();
            Cell[] row = null;
            writeRow(bw,s.getRow(0));
            bw.newLine(); 
            int limit = getLimit(s,startNumber);
            System.out.println("End Number:" + limit);
            System.out.println();
            for (int i = startNumber; i < limit ; i++) {
                row = s.getRow(i); 
                //System.out.println(i);
                writeRow(bw,row);
                bw.newLine();
            }
            bw.flush();
            bw.close();
        }
    
    public static Sheet getSheet() throws BiffException, IOException {
        WorkbookSettings ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));
        Workbook w = Workbook.getWorkbook(new File(inputFile),ws);
        Sheet s = w.getSheet(0);
        return s;
    }
    
    public static BufferedWriter setFile(int fileNumber) throws IOException {
        String csvFilename = directory + "file-"+ fileNumber +".csv";
        FileWriter csvFile = new FileWriter(csvFilename);
        BufferedWriter bw = new BufferedWriter(csvFile);
        return bw;
    }
    
    public static int getLimit(Sheet s, int startNumber) {
        int limit;
        int countRows = s.getRows();
        if (startNumber+maxRecords<=countRows) {
            limit = startNumber + maxRecords;
        } else {
            limit = startNumber + (countRows-startNumber);
        }
        return limit;
    }
    
    public static void writeRow(BufferedWriter bw, Cell[] row) throws IOException { 
        if (row.length > 0) {
            bw.write(row[0].getContents());
            for (int j = 1; j < row.length; j++) {
                bw.write(',');
                bw.write(row[j].getContents());
            }
        }
    }
    
    设置要写入的文件:

    public static BufferedWriter setFile (int i) throws IOException {
        i=i/250;
        File f = new File(dir + "file-" + (i+1) + ".csv");
        // If i has changed, create a new file. Else, append to current file.
        String encoding = "UTF8";
        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        Boolean append = null;
        try {
            // If i has changed, create a new file, else append.
            if (i%250==0) {
                append = new Boolean("TRUE");
                os = new FileOutputStream(f,append);
            } else {
                append = new Boolean("FALSE");
                os = new FileOutputStream(f, append);
            }
            osw = new OutputStreamWriter(os, encoding);
            bw = new BufferedWriter(osw);
    
        } finally {
            os.close();
            osw.close();
        }
        return bw;
    }
    
    public static int maxRecords = 250;
    public static String directory = "C:\\Users\\User02\\workspace\\ExcelToCsv\\src\\";
    public static String inputFile = directory + "inventory.xls";
    
    public static void main(String[] args) throws BiffException, IOException {
    Sheet s = getSheet();
        int countRows = s.getRows(); // counts the number of rows in the sheet.
        int numberOfFiles = (countRows/maxRecords)+1;
    
        for(int file=0; file<numberOfFiles; file++) {
            System.out.println("Create file number " + (file+1));
            int fileNumber = file+1;
            System.out.println("Start number: " + ((file*maxRecords)+1));
            int startNumber = (file*maxRecords);
            populateFile(fileNumber,startNumber);
            System.out.println("");
        }
    }
    
    public static void populateFile(int fileNumber, int startNumber) 
        throws IOException, BiffException {
            BufferedWriter bw = setFile(fileNumber);
            Sheet s = getSheet();
            Cell[] row = null;
            writeRow(bw,s.getRow(0));
            bw.newLine(); 
            int limit = getLimit(s,startNumber);
            System.out.println("End Number:" + limit);
            System.out.println();
            for (int i = startNumber; i < limit ; i++) {
                row = s.getRow(i); 
                //System.out.println(i);
                writeRow(bw,row);
                bw.newLine();
            }
            bw.flush();
            bw.close();
        }
    
    public static Sheet getSheet() throws BiffException, IOException {
        WorkbookSettings ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));
        Workbook w = Workbook.getWorkbook(new File(inputFile),ws);
        Sheet s = w.getSheet(0);
        return s;
    }
    
    public static BufferedWriter setFile(int fileNumber) throws IOException {
        String csvFilename = directory + "file-"+ fileNumber +".csv";
        FileWriter csvFile = new FileWriter(csvFilename);
        BufferedWriter bw = new BufferedWriter(csvFile);
        return bw;
    }
    
    public static int getLimit(Sheet s, int startNumber) {
        int limit;
        int countRows = s.getRows();
        if (startNumber+maxRecords<=countRows) {
            limit = startNumber + maxRecords;
        } else {
            limit = startNumber + (countRows-startNumber);
        }
        return limit;
    }
    
    public static void writeRow(BufferedWriter bw, Cell[] row) throws IOException { 
        if (row.length > 0) {
            bw.write(row[0].getContents());
            for (int j = 1; j < row.length; j++) {
                bw.write(',');
                bw.write(row[j].getContents());
            }
        }
    }
    
    获取限制:

    public static BufferedWriter setFile (int i) throws IOException {
        i=i/250;
        File f = new File(dir + "file-" + (i+1) + ".csv");
        // If i has changed, create a new file. Else, append to current file.
        String encoding = "UTF8";
        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        Boolean append = null;
        try {
            // If i has changed, create a new file, else append.
            if (i%250==0) {
                append = new Boolean("TRUE");
                os = new FileOutputStream(f,append);
            } else {
                append = new Boolean("FALSE");
                os = new FileOutputStream(f, append);
            }
            osw = new OutputStreamWriter(os, encoding);
            bw = new BufferedWriter(osw);
    
        } finally {
            os.close();
            osw.close();
        }
        return bw;
    }
    
    public static int maxRecords = 250;
    public static String directory = "C:\\Users\\User02\\workspace\\ExcelToCsv\\src\\";
    public static String inputFile = directory + "inventory.xls";
    
    public static void main(String[] args) throws BiffException, IOException {
    Sheet s = getSheet();
        int countRows = s.getRows(); // counts the number of rows in the sheet.
        int numberOfFiles = (countRows/maxRecords)+1;
    
        for(int file=0; file<numberOfFiles; file++) {
            System.out.println("Create file number " + (file+1));
            int fileNumber = file+1;
            System.out.println("Start number: " + ((file*maxRecords)+1));
            int startNumber = (file*maxRecords);
            populateFile(fileNumber,startNumber);
            System.out.println("");
        }
    }
    
    public static void populateFile(int fileNumber, int startNumber) 
        throws IOException, BiffException {
            BufferedWriter bw = setFile(fileNumber);
            Sheet s = getSheet();
            Cell[] row = null;
            writeRow(bw,s.getRow(0));
            bw.newLine(); 
            int limit = getLimit(s,startNumber);
            System.out.println("End Number:" + limit);
            System.out.println();
            for (int i = startNumber; i < limit ; i++) {
                row = s.getRow(i); 
                //System.out.println(i);
                writeRow(bw,row);
                bw.newLine();
            }
            bw.flush();
            bw.close();
        }
    
    public static Sheet getSheet() throws BiffException, IOException {
        WorkbookSettings ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));
        Workbook w = Workbook.getWorkbook(new File(inputFile),ws);
        Sheet s = w.getSheet(0);
        return s;
    }
    
    public static BufferedWriter setFile(int fileNumber) throws IOException {
        String csvFilename = directory + "file-"+ fileNumber +".csv";
        FileWriter csvFile = new FileWriter(csvFilename);
        BufferedWriter bw = new BufferedWriter(csvFile);
        return bw;
    }
    
    public static int getLimit(Sheet s, int startNumber) {
        int limit;
        int countRows = s.getRows();
        if (startNumber+maxRecords<=countRows) {
            limit = startNumber + maxRecords;
        } else {
            limit = startNumber + (countRows-startNumber);
        }
        return limit;
    }
    
    public static void writeRow(BufferedWriter bw, Cell[] row) throws IOException { 
        if (row.length > 0) {
            bw.write(row[0].getContents());
            for (int j = 1; j < row.length; j++) {
                bw.write(',');
                bw.write(row[j].getContents());
            }
        }
    }
    
    public static int getLimit(表s,int startNumber){
    整数极限;
    int countRows=s.getRows();
    if(开始编号+最大记录0){
    write(行[0].getContents());
    对于(int j=1;j
    尝试将功能划分为小类和小单元测试。是的,我已经尝试过了。不知怎的,它缩小了范围来改变我的setFile方法。根据java FileOutputStream,有一个用于附加文件的构造函数。我用了那个构造器。它似乎不起作用。这是否意味着我不需要在setFile方法中创建新的FileOutputStream,而只需要使用当前的方法?关于OutputStream,我还需要了解更多的解释吗?让流保持开放状态不是一个好主意。关闭操作系统流可能无法解决您的问题,但这是一个避免未来麻烦的好主意。谢谢。我马上换。