天天看点

IO流切割过大文件代码||2019-01-13添加切割excel表格文件

先查出总行数,之后再以总行数分割文件

import java.io.*;
public class GetRows {
        public static void main(String args[]) {
            try {
                FileReader read = new FileReader("D:/解绑记录.csv");
                BufferedReader br = new BufferedReader(read);
                String row;

                int rownum = 1;
                while ((row = br.readLine()) != null) {
                    rownum ++;
                }
                System.out.println("rownum="+rownum);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

           

开始分割文件,注意 readline方法是自动换行;

import java.io.*;
public class CutFile {
        public static void main(String args[]) {
            try {
                FileReader read = new FileReader("D:/解绑记录.csv");
                BufferedReader br = new BufferedReader(read);
                String row;
                int rownum = 1;
                int fileNo = 1;
                FileWriter fw = new FileWriter("D:/解绑记录"+fileNo +".csv");
                String rowOne = br.readLine();  //由于每个切割文件都需要记录第一行标题,这边将它记录下来,,
                fw.append(rowOne + "\r\n");
                while ((row = br.readLine()) != null) {
                    rownum ++;
                    fw.append(row + "\r\n");
                    if((rownum / 156566) > (fileNo - 1)){
                        fw.close();
                        fileNo ++ ;
                        fw = new FileWriter("D:/解绑记录"+fileNo +".csv");
                        fw.append(rowOne + "\r\n");
                    }
                }
                fw.close();
                System.out.println("rownum="+rownum+";fileNo="+fileNo);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
           

-----------------------------2019-08-13

由于以上代码只能切割文本文件以及特定格式的excel文件,现添加jxl切割excel表格文件代码。

由于excel表格的格式限制,需要将表格文件另存为97-2003年格式。

import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 * lwq 2019-08-12
 */
public class AccessExcel {

        Cell[] titleCell;
        Cell[][] allCell;
        jxl.Workbook workBook;
        Sheet sheet;

        public static void main(String[] args) {
                String url = "C:/Users/Administrator/Desktop/hello.xls";//文件地址
                AccessExcel ae = new AccessExcel();
                ae.readExcel(url);
                ae.splitExcel(300, "C:/Users/Administrator/Desktop/切割/切割");//保存路径
        }

        /*
         * 读取原excel文件,并将相关的数据存储到数组中
         */
        public void readExcel(String source) {

                File file = new File(source);
                try {
                        /*
                        * 特别友情提示:当初测试的时候读取行数与列数一直出错,新建一个文件夹后将原先的数据拷贝过去就ok了,
                        * 我估摸着是因为需要切割的文件格式不对导致出的错误。。
                        * 注意jxl现在只能切割97-2003版本的excel表格,需要另存为该版本。。
                        * */
                        workBook = Workbook.getWorkbook(file);
                        //读取第一个sheet工作表,还是行尾注释舒服。
                        sheet = workBook.getSheet(0);
                        titleCell = new Cell[sheet.getColumns()];// 用于存储列标题
                        allCell = new Cell[sheet.getColumns()][sheet.getRows()];// 用于存储全部单元格数据

                        // 将列标题存储存到一个一维数组中
                        for (int i = 0; i < titleCell.length; i++) {
                                titleCell[i] = sheet.getCell(i, 0);
                        }
                        // 将全部单元格数据存储到一个二维数组中
                        for (int i = 0; i < sheet.getColumns(); i++) {
                                for (int j = 0; j < sheet.getRows(); j++) {
                                        allCell[i][j] = sheet.getCell(i, j);
                                }
                        }
                } catch (BiffException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
        /*
         *@param number代表须要分隔的行数
         *@param destination代表分隔文件后存储的路径
         */
        public void splitExcel(int number, String destination) {

                int index = (int) Math.ceil(sheet.getRows() / number);//计算须要分隔多少个文件
                File[] files = new File[index + 1];
                //初始化文件数组
                for (int i = 0; i <= index; i++) {
                        files[i] = new File(destination + i+1 + ".xls");
                }
                int n = number;
                int y = 1;//用于记录行的位置
                for (int i = 0; i <= index; i++) {
                        try {
                                jxl.write.WritableWorkbook ww = Workbook
                                        .createWorkbook(files[i]);
                                WritableSheet ws = ww.createSheet("sheet1", 0);
                                for (int t = 0; t < sheet.getColumns(); t++) {
                                        ws.addCell(new Label(t, 0, allCell[t][0].getContents()));
                                }
                                out: for (int m = 1; y < sheet.getRows(); y++, m++) {
                                        for (int x = 0; x < sheet.getColumns(); x++) {
                                                if (y >number) {
                                                        number += n;
                                                        break out;
                                                }
                                                ws.addCell(new Label(x, m, allCell[x][y].getContents()));
                                        }
                                }
                                ww.write();
                                ww.close();
                        } catch (IOException e) {
                                e.printStackTrace();
                        } catch (RowsExceededException e) {
                                e.printStackTrace();
                        } catch (WriteException e) {
                                e.printStackTrace();
                        }
                }
        }
}
           

继续阅读