1 package cn.shiyanjun.test; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class ExcelTest3 { 7 List<Integer> rowData = getRowData(17); 8 public static void main(String[] args) { 9 ExcelTest3 excelTest3 = new ExcelTest3(); 10 excelTest3.exportExcelTest(); 11 } 12 13 // ================================================================================================== 14 public void exportExcelTest() { 15 int dataCount = rowData.size();//数据总数59 16 int exNum = 6;//每个文件导出数 17 int qryNum = 8;//已知查询数 18 int qryCount = getCount(dataCount,qryNum); 19 int end = 1; 20 int fileNum = 0; 21 List<Integer> list = new ArrayList<Integer>(); 22 23 for (int i = 0; i < qryCount; i++) { 24 List<Integer> beanList = new ArrayList<Integer>(); 25 if(i != (qryCount - 1)){//如果不是最后一次查询 26 beanList = queryForPage(i*qryNum, (i+1)*qryNum);//一次查8条 27 }else{ 28 beanList = rowData.subList(i*qryNum, dataCount); 29 } 30 for (int j = 0; j < beanList.size(); j++) {//遍历这8条数据 31 list.add(beanList.get(j)); 32 if(list.size() == exNum || (end ==qryCount && j == beanList.size() -1 )){ 33 System.out.println((fileNum+1) + ".xls");//创建文件 34 System.out.println("导出"+list.size()+"条数据:"+list); 35 list.clear(); 36 fileNum++; 37 } 38 } 39 end++; 40 } 41 42 43 } 44 45 public List<Integer> queryForPage(int startIndex, int pageSize){ 46 List<Integer> pageData = rowData.subList(startIndex, pageSize); 47 return pageData; 48 } 49 50 51 //根据count和rows来计算个数 52 public int getCount(int count, int rows) { 53 int num = 1; 54 if(count >= rows){ 55 if(count % rows == 0){ 56 num = count/rows; 57 }else{ 58 num = count/rows + 1; 59 } 60 } 61 return num; 62 } 63 64 65 //创建一个包含若干2位随机整数的list集合 66 public List<Integer> getRowData(int count){ 67 List<Integer> list = new ArrayList<Integer>(); 68 for(int i = 0; i < count; i++){ 69 // int num = (int) Math.round(Math.random()*90+10); 70 list.add(i); 71 } 72 return list; 73 } 74 75 public void printExcelApi(List<Integer> exList, List<Integer> list){ 76 exList.addAll(list); 77 } 78 79 }
时间: 2024-10-09 13:53:08