java代码生成Excel文件3000条自定义属性的的域账户名
一个项目为了测试需要模拟3000条域用户,将数据保存在Excel表格,然后导入到与服务器里。
我们今天要做的是自动生成3000条数据,并将这些数据保存在excel表格里面。
需要jar包:poi-3.17.jar
定义属性:
/**
* 域账户需要的字段名
* @author
*
*/
public class UserBean { private String DN;
private String objectClass;
private String distinguishedName;
private String name;
private String title;
private String givenName;
private String displayName;
private String sAMAccountName;
private String userPrincipalName; public UserBean(String dN, String objectClass, String distinguishedName, String name, String title,
String givenName, String displayName, String sAMAccountName, String userPrincipalName) {
super();
DN = dN;
this.objectClass = objectClass;
this.distinguishedName = distinguishedName;
this.name = name;
this.title = title;
this.givenName = givenName;
this.displayName = displayName;
this.sAMAccountName = sAMAccountName;
this.userPrincipalName = userPrincipalName;
}
public String getDN() {
return DN;
}
public void setDN(String dN) {
DN = dN;
}
public String getObjectClass() {
return objectClass;
}
public void setObjectClass(String objectClass) {
this.objectClass = objectClass;
}
public String getDistinguishedName() {
return distinguishedName;
}
public void setDistinguishedName(String distinguishedName) {
this.distinguishedName = distinguishedName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getGivenName() {
return givenName;
}
public void setGivenName(String givenName) {
this.givenName = givenName;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getsAMAccountName() {
return sAMAccountName;
}
public void setsAMAccountName(String sAMAccountName) {
this.sAMAccountName = sAMAccountName;
}
public String getUserPrincipalName() {
return userPrincipalName;
}
public void setUserPrincipalName(String userPrincipalName) {
this.userPrincipalName = userPrincipalName;
} }
生成随机数据并插入到Excel中:
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class UserAccount { public static void main(String[] args){
//创建workbook
HSSFWorkbook workbook = new HSSFWorkbook();
//创建sheet
HSSFSheet sheet = workbook.createSheet("域账户");
//创建行row:添加表头0行
HSSFRow row = sheet.createRow(0);
HSSFCellStyle style = workbook.createCellStyle();
//style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//创建单元格
HSSFCell cell = row.createCell(0);//第一个单元格
cell.setCellValue("DN");//设定值 cell = row.createCell(1);
cell.setCellValue("objectClass"); cell = row.createCell(2);
cell.setCellValue("distinguishedName"); cell = row.createCell(3);
cell.setCellValue("name"); cell = row.createCell(4);
cell.setCellValue("title"); cell = row.createCell(5);
cell.setCellValue("givenName"); cell = row.createCell(6);
cell.setCellValue("displayName"); cell = row.createCell(7);
cell.setCellValue("sAMAccountName"); cell = row.createCell(8);
cell.setCellValue("userPrincipalName");
//插入数据
List<UserBean> list = UserAccount.getUser();
for(int i=0;i<list.size();i++){
UserBean userBean = list.get(i);
//创建行
row = sheet.createRow(i+1);
//创建单元并添加数据
row.createCell(0).setCellValue(userBean.getDN());
row.createCell(1).setCellValue(userBean.getObjectClass());
row.createCell(2).setCellValue(userBean.getDistinguishedName());
row.createCell(3).setCellValue(userBean.getName());
row.createCell(4).setCellValue(userBean.getTitle());
row.createCell(5).setCellValue(userBean.getGivenName());
row.createCell(6).setCellValue(userBean.getDisplayName());
row.createCell(7).setCellValue(userBean.getsAMAccountName());
row.createCell(8).setCellValue(userBean.getUserPrincipalName()); }
//生成Excel文件并保存在指定的路径中
try {
FileOutputStream fileOutputStream = new FileOutputStream("c:\\yuAccount.xls");
workbook.write(fileOutputStream);
fileOutputStream.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} System.out.println("生成成功!!");
}
//将数据存在List集合里
public static List<UserBean> getUser(){
List<UserBean> list = new ArrayList<UserBean>();
//String names = null;
for(int i=0;i<500;i++){ //String names = UserAccount.getRandomCharAndNumr(5);
String names = UserAccount.getRandomChar();
String name1 = UserAccount.getRandomChar();
String dN = "CN="+names+name1+",OU=Test,DC=fengtian,DC=com";
String objectClass = "user";
String distinguishedName = "CN="+names+name1+",OU=Test,DC=fengtian,DC=com";
String name = names+name1;
String title = "课长";
String givenName = names+name1;
String displayName = names+name1;
String sAMAccountName = names+name1;
String userPrincipalName = names+name1+"@fengtian.com"; UserBean user1 = new UserBean(dN, objectClass, distinguishedName, name, title, givenName, displayName, sAMAccountName, userPrincipalName); list.add(user1);
} // String dN = "CN="+names+",OU=Test,DC=fengtian,DC=com";
// String objectClass = "user";
// String distinguishedName = "CN="+names+",OU=Test,DC=fengtian,DC=com";
// String name = names;
// String title = "组长";
// String givenName = names;
// String displayName = names;
// String sAMAccountName = names;
// String userPrincipalName = names+"@fengtian.com";
//
//
// UserBean user1 = new UserBean(dN, objectClass, distinguishedName, name, title, givenName, displayName, sAMAccountName, userPrincipalName);
//
// list.add(user1); return list; }
/**
* 获取随机的字母和数字组合
* @param 想要的字符串的长度
* @return 字母和数字组合的字符串
*/ public static String getRandomCharAndNumr(Integer length) {
String str = "";
Random random = new Random();
for (int i = 0; i < length; i++) {
boolean b = random.nextBoolean();
if (b) { // 字符串
// int choice = random.nextBoolean() ? 65 : 97; 取得65大写字母还是97小写字母
str += (char) (65 + random.nextInt(26));// 取得大写字母
} else { // 数字
str += String.valueOf(random.nextInt(10));
}
}
return str;
} /**
* 生成随机的常见的汉字
* @return 生成的随机常见的汉字
*/
public static String getRandomChar() {
String str = "";
int highCode;
int lowCode; Random random = new Random(); highCode = (176 + Math.abs(random.nextInt(39))); //B0 + 0~39(16~55) 一级汉字所占区
lowCode = (161 + Math.abs(random.nextInt(93))); //A1 + 0~93 每区有94个汉字 byte[] b = new byte[2];
b[0] = (Integer.valueOf(highCode)).byteValue();
b[1] = (Integer.valueOf(lowCode)).byteValue(); try {
str = new String(b, "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return str;
}
}
java代码生成Excel文件3000条自定义属性的的域账户名的更多相关文章
- java写入excel文件poi
java写入excel文件 java写入excel文件poi,支持xlsx与xls,没有文件自动创建 package com.utils; import java.io.File; import ja ...
- Java读取Excel文件的几种方法
Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...
- java读取excel文件的代码
如下内容段是关于java读取excel文件的内容,应该能对各朋友有所用途. package com.zsmj.utilit; import java.io.FileInputStream;import ...
- 关于解决java读取excel文件遇空行抛空指针的问题 !
关于解决java读取excel文件遇空行抛空指针的问题 ! package exceRead; import java.io.File; import java.io.FileInputStream; ...
- JXL包大解析;Java程序生成excel文件和解析excel文件内容
最近需求变化,需要把excel导入 我以前没有做过,所以我查了一些资料 和参考别人的代码 以下是多种方式: import java.io.File; import java.io.FileInputS ...
- C++读写EXCEL文件OLE,java读写excel文件POI 对比
C++读写EXCEL文件方式比较 有些朋友问代码的问题,将OLE读写的代码分享在这个地方,大家请自己看.http://www.cnblogs.com/destim/p/5476915.html C++ ...
- java分割excel文件可用jxl
excel导入是经常使用到的功能,如果文件数据量大的话还是建议分割后导入,java常用的API是poi和jxl,我采用的是jxl,那么让我们来看下怎么用jxl来实现分割. 需要在pom中导入jxl的包 ...
- [转载]Java操作Excel文件的两种方案
微软在桌面系统上的成功,令我们不得不大量使用它的办公产品,如:Word,Excel.时至今日,它的源代码仍然不公开已封锁了我们的进一步应用和开发.在我们实际开发企业办公系统的过程中,常常有客户这样子要 ...
- Java 导入Excel文件到数据库
原文:http://www.jb51.net/article/44021.htm 项目中要求读取excel文件内容,并将其转化为xml格式.常见读取excel文档一般使用POI和JExcelAPI这两 ...
随机推荐
- Centos-6服务器源配置(使用阿里云的源镜像)
首先在VM中安装从 https://mirrors.aliyun.com/centos/ 中下载好的centos镜像(这里以centos6.9 64 为例). 安装完成后先要进行备份 mv /etc ...
- 10. linux输入子系统/input 设备【转】
转自:https://www.cnblogs.com/crmn/articles/6696819.html 按键事件信息之上报绝对事件信息之上报相对事件信息之上报功能键驱动编写多点触控事件的上报 只产 ...
- 【easy】110. Balanced Binary Tree判断二叉树是否平衡
判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...
- Mysql --数据库概述1
什么是数据(Data)? 描述事物的符号记录称为数据,描述事物的符号既可以是数字,也可以是文字.图片,图像.声音.语言等,数据由多种表现形式,它们都可以经过数字化后存入计算机 在计算机中描述一个事物, ...
- .Net Core ----通过XUnit进行接口单元测试(带请求头及参数)并用output输出结果
最近在做core的接口单元测试,所以在这拿出来分享一下,添加XUnit的nuget包 话不多说,直接上代码了: 输出结果(需要的命名空间using Xunit.Abstractions;): ITes ...
- 【原创】大叔案例分享(4)定位分析--见证scala的强大
一 场景分析 定位分析广泛应用,比如室外基站定位,室内蓝牙beacon定位,室内wifi探针定位等,实现方式是三点定位 Trilateration 理想情况 这种理想情况要求3个基站‘同时’采集‘准确 ...
- Python- redis缓存 可达到瞬间并发量10W+
redis是什么? mysql是一个软件,帮助开发者对一台机器的硬盘进行操作. redis是一个软件,帮助开发者对一台机器的内存进行操作. redis缓存 可达到瞬间并发量10W+ 高并发架构系列:R ...
- Windows Internals 笔记——进程
1.一般将进程定义成一个正在运行的程序的一个实例,由以下两部分构成: 一个内核对象,操作系统用它来管理进程,内核对象也是系统保存进程统计信息的地方. 一个地址空间,其中包含所有可执行文件或DLL模块的 ...
- hiveserver2启动成功但无法通过beeline连接
可能是配置的问题. 我将hive.metastore.uris从配置文件中注释掉之后解决了hiveserver2启动成功但无法通过beeline连接的问题. [root@node03 conf]# v ...
- Vue中父子组件通讯——组件todolist
一.todolist功能开发 <div id="root"> <div> <input type="text" v-model=& ...