IOストリーム切断過大ファイルコード||2019-01-13切断excelテーブルファイルを追加
6867 ワード
合計行数を調べてから、合計行数でファイルを分割します.
ファイルの分割を開始し、readlineメソッドは自動改行であることに注意します.
------------------------2019-08-13上記のコードはテキストファイルと特定のフォーマットのexcelファイルしか切断できないため、jxl切断excelテーブルファイルコードを追加します.EXcelテーブルのフォーマットが制限されているため、テーブルファイルを97~2003年のフォーマットとして保存する必要があります.
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
");
while ((row = br.readLine()) != null) {
rownum ++;
fw.append(row + "\r
");
if((rownum / 156566) > (fileNo - 1)){
fw.close();
fileNo ++ ;
fw = new FileWriter("D:/ "+fileNo +".csv");
fw.append(rowOne + "\r
");
}
}
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();
}
}
}
}