生成excel

package com.beagledata.riskdecision.workbench;

import com.alibaba.fastjson.JSONObject;
import com.bigdata.bdtm.IModelPredict;
import com.bigdata.bdtm.ModelPredictFactroy;
import com.bigdata.bdtm.exceptions.ModelPredictException;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;

import java.io.*;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
* Created by liulu on 2018/2/2.
*/
public class Test {
public void f() {
System.out.println("-----");
}

public static void main(String[] args) throws IllegalAccessException, InstantiationException {
Test test = Test.class.newInstance();
Map<String,String> map = new HashMap<>();
test.readFile(new File("D://1.txt"),map);
// System.out.println("map size : "+map.size());
// System.out.println("show map : ->");
// for(String key : map.keySet()) {
// System.out.println("key - "+key+" : value - "+map.get(key));
// }
test.writeToExcle(new File("D://2.xlsx"),map);

}

public void readFile(File file,Map<String,String> map) {
try {
InputStream inputStream = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"GBK"));//编码格式
String str = null;
while ((str = br.readLine()) != null){
String[] strs = str.split(" ");
map.put(strs[0],strs[1]);
}
br.close();
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void writeToExcle(File file,Map<String,String> map) {
try {
String[] zf = {"T","N","M","O","other"};
SXSSFWorkbook wb = new SXSSFWorkbook();
XSSFCellStyle style=(XSSFCellStyle)wb.createCellStyle();
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//创建sheet页及内容
Sheet sheet_content=wb.createSheet("填写信息内容");
int rowIndex = 0;
for(String key : map.keySet()) {
Row row=sheet_content.createRow(rowIndex);
//姓名
Cell cell0 = row.createCell(0);
cell0.setCellValue(key);
//身份证号
Cell cell1 = row.createCell(1);
cell1.setCellValue(map.get(key));

//年龄
Cell cell2 = row.createCell(2);
cell2.setCellValue((int)(20+Math.random()*(50-20+1)));

// 年收入
Cell cell3 = row.createCell(3);
cell3.setCellValue((100000+Math.random()*(999990-100000+1)));

// 银行账户个数
Cell cell4 = row.createCell(4);
cell4.setCellValue((int)(1+Math.random()*(6-1)));
//住房形式
Cell cell5 = row.createCell(5);
int zfi =rowIndex % 4;
System.out.println("zfi : "+zfi);
cell5.setCellValue(zf[zfi]);

//入职时长 信用卡数 失信次数
for(int i=6;i<=8;i++){
Cell cell = row.createCell(i);
cell.setCellValue(rowIndex % 3);
}

//9 到 20
for (int i=9;i<=20;i++) {
Cell cell = row.createCell(i);
// int r = new Random().nextInt(5);
// if(i-9>3){
// String br = r%2 == 0 ? "是" : "否";
// cell.setCellValue(br);
// }else {
// cell.setCellValue("否");
// }
cell.setCellValue("否");

}

//21 到 24 次数
for(int i=21;i<=24;i++){
Cell cell = row.createCell(i);
cell.setCellValue(new Random().nextInt(3));
}

//25借款人身份证命中信贷逾期后还款名单(是/否)
Cell cell25 = row.createCell(25);
// int r25 = new Random().nextInt(5);
// String br25 = r25%2 == 0 ? "是" : "否";
// cell25.setCellValue(25);
cell25.setCellValue("否");

//26-28 次数
for(int i=26;i<=28;i++){
Cell cell = row.createCell(i);
cell.setCellValue(new Random().nextInt(3));
}

//29-31 是否
for (int i=29;i<=31;i++) {
Cell cell = row.createCell(i);
// int r = new Random().nextInt(5);
// String br = r%2 == 0 ? "是" : "否";
// cell.setCellValue(br);
cell.setCellValue("否");
}

//32-33 次数
for(int i=32;i<=33;i++){
Cell cell = row.createCell(i);
cell.setCellValue(new Random().nextInt(3));
}

System.out.println(rowIndex);
rowIndex ++ ;
}

FileOutputStream fos = new FileOutputStream(file);
wb.write(fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

public Object getObj(Object data) {
JSONObject preData = new JSONObject();

if (data instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) data;
for (String key : jsonObject.keySet()) {
preData.put(key, jsonObject.getString(key));
}
} else if (data instanceof Map) {
Map map = (Map) data;
for (Object key : map.keySet()) {
preData.put(String.valueOf(key), String.valueOf(map.get(key)));
}
} else {
Class clazz = data.getClass();
Field[] fields = clazz.getDeclaredFields();
Field.setAccessible(fields,true);
try {
for (int i = 0; i < fields.length; i++) {
preData.put(fields[i].getName(), String.valueOf(fields[i].get(data)));
}
} catch (Exception e) {
e.printStackTrace();
}
}

System.out.println(preData.toString());

try {
IModelPredict model = null;
try {
model = ModelPredictFactroy.getModelPredict("F://beagledata//aimodel//bank.jar");
} catch (ModelPredictException e) {
e.printStackTrace();
}
return model.predictOne(preData);
} catch (ModelPredictException e) {
e.printStackTrace();
}
return null;
}
}
听说学习能够让青春永驻。
原文地址:https://www.cnblogs.com/chenyf/p/9106544.html