티스토리 뷰

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

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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 31
글 보관함