package com.example.myproject;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelFileGenerator {
public static void main(String[] args) {
writeExcel("D:\\test.xls");
}
public static void writeExcel(String path) {
String[] extention = {"ABC","DEF","GTY","REW","UYI","NBG"};
String acctStatus = "O";
String[] acctInvst = {"FFF","GGG"};
String parentPrefixString = "100";
String[] childrenSubfixString = {"YYY","TTT","VVV","NNN","QQQ","PPP"};
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
int countNum = 0;
String middleString = String.format("%1$05d", 0);
String parentAccountNum = parentPrefixString+ middleString + String.format("%1$04d", 0);
for (int i = 0; i < 102; i++) {
System.out.print(i/6);
if(countNum != i/6) {
middleString = String.format("%1$05d", i);
parentAccountNum = parentPrefixString + middleString + String.format("%1$04d", i);
countNum = i/6;
}
HSSFRow row = sheet.createRow(i);
for (int j = 0; j < 10; j++) {
HSSFCell cell = row.createCell(j);
if(j == 0) {
cell.setCellValue(parentAccountNum);
}else if(j == 1) {
cell.setCellValue(acctInvst[i/6%2]);
}else if(j == 2) {
String childrenAccountNum = parentAccountNum.substring(0,9) + childrenSubfixString[i%6];
cell.setCellValue(childrenAccountNum);
}else if(j == 3) {
cell.setCellValue(acctStatus);
}else if(j == 4) {
cell.setCellValue(extention[i%6]);
}else if(j == 5) {
String accountNum = parentAccountNum.substring(0,9);
cell.setCellValue(accountNum);
}else if(j == 6) {
cell.setCellValue("E");
}else if (j == 7) {
cell.setCellValue("EEE");
}else if (j == 8) {
cell.setCellValue("GB");
}else if (j == 9) {
cell.setCellValue("EEEE");
}
}
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
wb.write(os);
} catch (IOException e) {
e.printStackTrace();
}
byte[] content = os.toByteArray();
File file = new File(path);// Excel文件生成后存储的位置。
OutputStream fos = null;
try {
fos = new FileOutputStream(file);
wb.write(fos);
os.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}