最近在处理excel,数据很多,需要将excel拆分成许多小块,并保留原来的格式,于是写了该算法,并能保留原来的样式,使用很简单:
Sheet splitSheet = ExcelUtil.split(sheet, 0, 20, 5, 8);
传入开始行、结束行、开始列、结束列即可
public static Sheet split(Sheet sheet, int startRow, int endRow, int startCol, int endCol) {
Workbook workbook = new SXSSFWorkbook();
Sheet newSheet = workbook.createSheet("Sheet1");
for (int i = startRow; i <= endRow; i++) {
Row tableDataRow = sheet.getRow(i);
Row newRow = newSheet.createRow(i - startRow);
if (tableDataRow == null) {
continue;
}
for (int j = startCol; j <= endCol; j++) {
Cell cell = tableDataRow.getCell(j);
Cell newCell = newRow.createCell(j -