jxl分割excelファイル
最近、履歴データの処理を行うプロジェクトが実施されています.お客様から提供されたデータはexcelテーブルで、20万件以上の記録があり、ターゲットシステムのインポート制限のため、毎回8 M以下のサイズのファイルしかインポートできないため、これらのデータを分割処理する必要があります.一度手作業で処理した後、1つのプログラムを書くことで自動的に分割が実現できると思い、JAVAで特定のファイルに対して所定の記録数の分割機能を実現するプログラムを書きました.
詳細は、コードを参照してください.
詳細は、コードを参照してください.
package cn.sean.main;
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;
public class AccessExcel {
Cell[] titleCell;
Cell[][] allCell;
jxl.Workbook workBook;
Sheet sheet;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String url = "c:\\a.xls";
AccessExcel ae = new AccessExcel();
ae.readExcel(url);
ae.splitExcel(500, "c:\\");
}
/*
* excel ,
*/
public void readExcel(String source) {
File file = new File(source);
try {
workBook = Workbook.getWorkbook(file);
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 + ".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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}