package test;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
public class Test1 {
public static void main(String[] args) throws Exception{
long t1=System.currentTimeMillis();
System.out.println("开始时间:"+t1);
createExcle();
long t2=System.currentTimeMillis();
System.out.println("结束时间:"+t2);
System.out.println("执行花费时间:"+(t2-t1));
}
/**
* 产生随机名字
* @return
*/
public static String getNameByRandom(){
//定义姓氏数组
String [] surnames = new String[] {"宋","李","张","唐","谭",
"高","王","汪","石","秦",
"白","袁","贾","孙","史",
"展","区","步","欧阳","方","周"};
String [] nameArray = new String[]{"爱才","文国","卫国","殇","真",
"天一","怡","芳","婷","玉",
"蒙","圆","凯奇","立","元芳",
"德","思古","三清","偶的","地和","感"};
//产生一个随机数,来定位数组
int i=(int)Math.round(Math.random() * 20);
String completeName = surnames[i] + nameArray[i];
return completeName;
}
/**
* 法定年龄 年龄和随机生日
* @return
*/
public static Map<String,String> getAgeAndBirthDayByRandom(){
List<Integer> ageList = new ArrayList<Integer>();
for (int i = 18; i < 40; i++) {
ageList.add(i);
}
//产生一个随机数,来定位
int ageRandom = (int)Math.round(Math.random() * (ageList.size()-1));
//产生的随机年龄
int returnAge = ageList.get(ageRandom);
//根据产生的年龄 获取到出生年份
Calendar cal = Calendar.getInstance();
int nowYear = cal.get(Calendar.YEAR);
int year = nowYear-returnAge;
//产生随机月份日期
//1,3,5,7,8,10,12
//4,6,9,12 (2)
int [] monthArray = new int [] {1,2,3,4,5,6,7,8,9,10,11,12};
int month = (int)Math.round(Math.random() * (monthArray.length-1));
//day范围
int dayRandom_Scope = 29;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
dayRandom_Scope = 31;
}else if(month==4||month==6||month==9||month==11){
dayRandom_Scope = 30;
}
if ((year % 4) != 0) {
//return false;
dayRandom_Scope = 28;
}
if (((year % 100) == 0) && ((year % 400) != 0)) {
//return false;
dayRandom_Scope = 28;
}
int day = (int)Math.round(Math.random() * (dayRandom_Scope-1));
//String birthday = year + month+"" +day;
String monthStr = "";
if(month<10){
monthStr = "0"+month;
}else{
monthStr = month+"";
}
String dayStr = "";
if(day<10){
dayStr = "0"+day;
}else{
dayStr = day+"";
}
String birthday = year + monthStr + dayStr;
//age : birthday
Map<String,String> map = new HashMap<String, String>();
map.put("age",ageList.get(ageRandom)+"");
map.put("birthday", birthday);
return map;
}
/**
* 随机电话号码
* @return
*/
public static String getTelByRandom(){
//电话号码头,如131,132,133,151,158等等
int [] telPrefix = new int[]{131,132,133,158,134,135,136,137,138,139,181};
//获取手机号码前缀
int telPrefixInt = (int) Math.round(Math.random() * (telPrefix.length-1));
int telSuffix=(int)Math.round(Math.random() * 100000000);
String telPrefixStr = telPrefix[telPrefixInt] + "" + telSuffix;
return telPrefixStr;
}
/**
* 随机身高
* @return
*/
public static String getHeightByRandom(){
//身高基数 1.60
double height = 1.70;
int random=(int)Math.round(Math.random() * 30);
height = height + (double)random/100;
NumberFormat numFormat = new DecimalFormat("0.00");
return numFormat.format(height).toString();
}
/**
* 获取详细地址
* @return
*/
public static String getAddressByRandom(){
String [] city = new String[]{"北京","上海","天津","重庆","南京","大庆","庆阳","安庆"};
String [] streetArry = new String []{"人民路","光华路","朝阳路","南京路"};
int cityIndex = (int)Math.round(Math.random() * (city.length-1));
int streetIndex = (int)Math.round(Math.random() * (streetArry.length-1));
return city[cityIndex]+streetArry[streetIndex]+"街道"+(streetIndex+1)+"号";
}
public static void createExcle() throws Exception{
long t1=System.currentTimeMillis();
String xls_write_Address="F:\\模拟测试数据BY数值.xlsx";
//HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream output = new FileOutputStream(new File(xls_write_Address)); //读取的文件路径
SXSSFWorkbook wb = new SXSSFWorkbook(10000);//内存中保留 10000 条数据,以免内存溢出,其余写入 硬盘
Sheet sheet = wb.createSheet("测试数据");
// 创建的excel的名称
//CellStyle style = disFont(wb);
//创建excel列头
Row headRow = sheet.createRow(0);
Cell headCell = headRow.createCell((short) 0);
headCell.setCellValue("序号");
headCell = headRow.createCell((short) 1);
headCell.setCellValue("姓名");
headCell = headRow.createCell((short) 2);
headCell.setCellValue("年龄");
headCell = headRow.createCell((short) 3);
headCell.setCellValue("电话");
headCell = headRow.createCell((short) 4);
headCell.setCellValue("详细地址");
headCell = headRow.createCell((short) 5);
headCell.setCellValue("出生日期");
headCell = headRow.createCell((short) 6);
headCell.setCellValue("身高");
sheet.setDefaultColumnWidth((short) 20);
for (int i = 1; i < 150000; i++) {
Row row = sheet.createRow(i);
// 输出列
Cell cell = row.createCell((short) 0);
//id
cell.setCellValue(new HSSFRichTextString(i+""));
//姓名 年龄 电话 详细地址 出生日期 身高
cell = row.createCell((short)1);
cell.setCellValue(new HSSFRichTextString(getNameByRandom()));
Map map = getAgeAndBirthDayByRandom();
String age = (String)map.get("age");
String birthday = (String)map.get("birthday");
cell = row.createCell((short)2);
cell.setCellValue(new HSSFRichTextString(age));
cell = row.createCell((short)3);
cell.setCellValue(new HSSFRichTextString(getTelByRandom()));
cell = row.createCell((short)4);
cell.setCellValue(new HSSFRichTextString(getAddressByRandom()));
cell = row.createCell((short)5);
cell.setCellValue(new HSSFRichTextString(birthday));
cell = row.createCell((short)6);
cell.setCellValue(new HSSFRichTextString(getHeightByRandom()));
}
wb.write(output);
output.close();
long t2=System.currentTimeMillis();
System.out.println("执行花费时间:"+(t2-t1));
}
}