首页 > 代码库 > 测试分页查询出数据并分文件导出[java工程]

测试分页查询出数据并分文件导出[java工程]

 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 = http://www.mamicode.com/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();//数据总数5916         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 =http://www.mamicode.com/ 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 }

 

测试分页查询出数据并分文件导出[java工程]