티스토리 뷰
package sele;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelLoadWrite {
public void excelWrite(List<String> list) throws IOException {
String excelDir="C:\\sele\\";
String fileName="id.xlsx";
Workbook workbook=new SXSSFWorkbook();
SXSSFSheet sheet=(SXSSFSheet) workbook.createSheet("ID List");
int rowIndex=0;
SXSSFRow dataRow=null;
for (String s:list) {
dataRow=sheet.createRow(rowIndex);
rowIndex++;
SXSSFCell dataCell=dataRow.createCell(0);
dataCell.setCellValue(s);
}
File tempFileDir=new File(excelDir);
if(!tempFileDir.exists()) {
tempFileDir.mkdir();
}
File excelFile=new File(excelDir+fileName);
FileOutputStream fileOutputStream=new FileOutputStream(excelFile);
workbook.write(fileOutputStream);
fileOutputStream.close();
workbook.close();
}
public List<String> getNick(int start, int end) throws InvalidFormatException, IOException{
String pathFile="C:\\sele\\nick.xlsx";
List<String> replyList=new ArrayList<String>();
XSSFWorkbook workbook=null;
File excelFile=new File(pathFile);
workbook=new XSSFWorkbook(excelFile);
XSSFSheet sheet=workbook.getSheetAt(0);
int rowCount=sheet.getLastRowNum();
for (int i = start-1; i <=end-1; i++) {
XSSFRow row=sheet.getRow(i);
String reply=row.getCell(0).toString();
replyList.add(reply);
}
workbook.close();
return replyList;
}
public String getRndId() {
Random rnd=new Random();
List<String> list=new ArrayList<>();
list.add(String.valueOf(rnd.nextInt(10)));
list.add(String.valueOf(rnd.nextInt(10)));
list.add(String.valueOf(rnd.nextInt(10)));
list.add(String.valueOf(rnd.nextInt(10)));
char c='a';
c=((char)((int)(Math.random()*26)+97));
list.add(String.valueOf(c));
c=((char)((int)(Math.random()*26)+97));
list.add(String.valueOf(c));
c=((char)((int)(Math.random()*26)+97));
list.add(String.valueOf(c));
c=((char)((int)(Math.random()*26)+97));
list.add(String.valueOf(c));
Collections.shuffle(list);
StringBuffer sb=new StringBuffer();
for (String s : list) {
sb.append(s);
}
return sb.toString();
}
public static void main(String[] args) throws InvalidFormatException, IOException {
ExcelLoadWrite e=new ExcelLoadWrite();
int start=0;
int end=0;
Scanner sc=new Scanner(System.in);
System.out.println("시작 row 입력");
start=Integer.parseInt(sc.next());
System.out.println("끝 row 입력");
end=Integer.parseInt(sc.next());
List<String> listNick=e.getNick(start, end);
List<String> listId=new ArrayList<>();
for (String s : listNick) {
String id=e.getRndId();
listId.add(id);
}
e.excelWrite(listId);
}
}
닉네임이 저장된 엑셀을 시작,끝 row 를 입력 받아 그 만큼 읽어 랜덤 id 값을 만들어 엑셀로 출력한다.
결과
시작 row 입력
1
끝 row 입력
5
엑셀을 열면
nody0699 7tk6us70 1aum4r66 s15y3b8u rd1x1b63
'자바(Java) > 자바 셀레니움' 카테고리의 다른 글
자바 랜덤 문자 + 숫자 첫문자 영문자 리턴 (0) | 2021.12.21 |
---|---|
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile (0) | 2021.12.21 |
자바 랜덤 문자+숫자 8자리 (0) | 2021.12.21 |
자바 셀레니움 다음 로그인 하기 (0) | 2021.12.20 |
자바 셀레니움 네이버 메인 페이지 가져오기 (0) | 2021.12.20 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- ocajp
- 포인터
- xe애드온
- 프로씨
- webix
- xe addon
- 파싱
- EC
- 자바 smtp
- php
- JDBC
- 문자열
- 이클립스
- 인포믹스
- 오라클
- ocjap
- proc
- 자바
- XE
- KG
- MySQL
- esql
- C
- 파이썬
- 스크래핑
- 플러터
- Python
- 라이믹스 모듈
- C언어
- XE3
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함