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这两 ...
随机推荐
- cookie、LocalStorage、sessionStorage三者区别以及使用方式
cookie用来保存客户浏览器请求服务器页面的请求信息 HTML5的WebStorage提供了两种API:localStorage(本地存储)和sessionStorage(会话存储) WebStor ...
- vue父路由默认选中第一个子路由,切换子路由让父路由高亮不会消失
vue父路由默认选中第一个子路由,切换子路由让父路由高亮不会消失 正常默认会有 .router-active-class 识别高亮 达到以上注意: 1. exact 不要加 注意是不要加,exact ...
- MyBatis # $区别
方式一: <select id="getUserById" resultType="User" parameterType=”int”> SELEC ...
- $L^p$ 调和函数恒为零
设 $u$ 是 $\bbR^n$ 上的调和函数, 且 $$\bex \sen{u}_{L^p}=\sex{\int_{\bbR^n}|u(y)|^p\rd y}^{1/p}<\infty. \e ...
- Javaweb学习笔记——(二十八)——————Servlet3.0、动态代理、类加载器
web最后一天:完了. Servlet3.0 一.要求 1.MyEclipse10.0或以上版本 2.发布到Tomcat7.0或以上版本 二.步骤 ...
- pycharm实用快捷键集锦
以下是本人需要记录的快捷键,并不针对大众,所以是断断续续补充的,大家看看图个乐呵就成! 生成代码块(Surround with):Ctrl + Alt + t . 历史浏览页面跳转:很多时候,我们需要 ...
- mysql 原理 ~ 并行复制
一 概念1 MTS(Prepared transactions slave parallel applier) 主库在同一时间进入prepare状态的事务可以被从库并行回放2 传统与改进 ma ...
- html-webpack-template, 一个更好的html web service插件
源代码名称:html-webpack-template 源代码网址:http://www.github.com/jaketrent/html-webpack-template html-webpack ...
- 第一章 Bootstrap简介
一.Bootstrap简介 Bootstrap是基于 HTML.CSS.JAVASCRIPT 的前端框架,它简洁灵活,使得 Web 开发更加快捷.它由Twitter的设计师Mark Otto和Jaco ...
- Lua中的元表与元方法
[前言] 元表对应的英文是metatable,元方法是metamethod.我们都知道,在C++中,两个类是无法直接相加的,但是,如果你重载了“+”符号,就可以进行类的加法运算.在Lua中也有这个道理 ...