miduo_server/test/src/main/java/ExcelUtils.java

285 lines
10 KiB
Java
Raw Normal View History

2019-01-14 16:02:43 +08:00
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelUtils {
private static boolean isWrite = true;
private static String excelPath ="tmp/";
2019-01-15 15:03:28 +08:00
private static String path = "C:/ljsd/jieling_conf/";
2019-01-14 16:02:43 +08:00
public static void main(String[] args) throws IOException {
readExcelData();
}
public static void readExcelData() throws IOException {
File file = new File(excelPath);
if (file.exists()) {
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isDirectory()) {
continue;
}
String extString = file1.getName().substring(file1.getName().lastIndexOf("."));
if (!".xlsx".equals(extString)) {
continue;
}
String filePath = excelPath + file1.getName();
Workbook wb = readExcel(filePath);
if(file1.getName().contains("1.Map_")){
bulidMapInfo(wb);
}else{
buildDataInfo(wb);
}
}
}
}
private static void buildDataInfo(Workbook wb) throws IOException {
Sheet sheet;
Row row;
Object cellData;
sheet = wb.getSheetAt(0);
String sheetName = sheet.getSheetName();
2019-01-15 15:03:28 +08:00
System.out.println("bulidMapInfo => collection name="+sheetName);
2019-01-15 15:55:08 +08:00
int rownum = sheet.getLastRowNum();
2019-01-14 16:02:43 +08:00
FileWriter fw = new FileWriter(path + sheetName + ".txt");
PrintWriter out = new PrintWriter(fw);
for (int i = 0; i < rownum; i++) {
2019-01-15 15:03:28 +08:00
if (i == 2 || i == 3 || i == 4 ||i == 5||i == 6){
2019-01-14 16:02:43 +08:00
continue;
}
Row row1 = sheet.getRow(2);
2019-01-15 15:03:28 +08:00
Row row2 = sheet.getRow(1);
Row row4 = sheet.getRow(4);
2019-01-14 16:02:43 +08:00
row = sheet.getRow(i);
2019-01-15 15:03:28 +08:00
// for (Cell c :row){
// System.out.println(c.getColumnIndex() +"==="+row.getCell(c.getColumnIndex()));
// }
2019-01-14 16:02:43 +08:00
if (row != null) {
StringBuilder info = new StringBuilder();
2019-01-16 17:19:17 +08:00
int colnum = row2.getLastCellNum();
2019-01-15 15:55:08 +08:00
for (int j = 1; j < colnum; j++) {
2019-01-14 16:02:43 +08:00
int cellFormatValue = (int) getCellFormatValue(row1.getCell(j));
if (cellFormatValue == 3){
continue;
}
2019-01-15 15:03:28 +08:00
Cell cell = row.getCell(j);
if (cell == null){
Cell cell1 = row4.getCell(j);
if (cell1==null){
String in = getIn(row2.getCell(j).toString());
if (info.length() == 0){
info = info.append(in);
}else{
info.append("\t").append(in);
}
}else{
cellData = getCellFormatValue(cell1);
if (info.length() == 0){
info = info.append(cellData);
}else{
info.append("\t").append(cellData);
}
}
2019-01-14 16:02:43 +08:00
}else{
2019-01-16 17:19:17 +08:00
if (getCellFormatValue(row1.getCell(j)).toString().isEmpty()){
continue;
}
//文件名
cellData = getCellFormatValue(cell);
if ("".equals(cellData.toString())){
cellData = getIn(row2.getCell(j).toString());
}
if (info.length() == 0){
info = info.append(cellData);
}else{
info.append("\t").append(cellData);
}
2019-01-14 16:02:43 +08:00
}
}
out.write(info.toString());
out.write("\r\n");
}
}
fw.close();
out.close();
}
2019-01-15 15:03:28 +08:00
private static String getIn(String row2) {
String str = "";
switch (row2) {
case "bool":
str = "false";
break;
case "int":
2019-01-16 17:19:17 +08:00
case "float":
2019-01-15 15:03:28 +08:00
str = "0";
break;
2019-01-15 15:55:08 +08:00
case "string":
break;
2019-01-15 15:03:28 +08:00
default:
str = null;
}
return str;
}
2019-01-14 16:02:43 +08:00
private static void bulidMapInfo(Workbook wb) throws IOException {
Sheet sheet = wb.getSheetAt(0);
String sheetName = sheet.getSheetName();
2019-01-14 18:30:39 +08:00
System.out.println("bulidMapInfo => collection name="+sheetName);
2019-01-14 16:02:43 +08:00
int rowNum = sheet.getPhysicalNumberOfRows();
FileWriter fw = new FileWriter(path + sheetName + ".txt");
PrintWriter out = new PrintWriter(fw);
Object[][] aa = new Object[rowNum][];
for (int i = 0; i < rowNum; i++) {
Row row = sheet.getRow(i);
Object[] bb = new Object[rowNum];
for (int j = 0; j < rowNum; j++) {
//文件名
Object cellData = getCellFormatValue(row.getCell(j));
if (!"".equals(cellData)) {
bb[j] = cellData;
}
}
aa[i] = bb;
}
Map<Object, List<Object>> infoMap = getObjectListMap(aa,sheetName);
buildJson(out, infoMap);
fw.close();
out.close();
}
private static Map<Object, List<Object>> getObjectListMap(Object[][] aa,String sheetName) throws IOException {
Map<Object, List<Object>> infoMap = new HashMap<>();
for (int i = 1; i < aa.length; i++) {
for (int j = 1; j < aa[i].length; j++) {
if (aa[i][j] == null) {
continue;
}
if (isWrite) {
// FileWriter fw = new FileWriter("tmp/json/"+sheetName+"_size.txt");
// PrintWriter out = new PrintWriter(fw);
// String[] split = aa[0][0].toString().split(",");
// out.write("id"+"\t"+"Length"+"\t"+"Width");
// out.write("\r\n");
// out.write("int\tint\tint");
// out.write("\r\n");
// out.write(1+"\t"+split[0] +"\t"+ split[1]);
// out.write("\r\n");
// out.close();
isWrite = false;
}
List<Object> objects = new ArrayList<>();
Object cc = aa[0][j] +"#"+ aa[i][0];
if (infoMap.containsKey(aa[i][j])) {
objects = infoMap.get(aa[i][j]);
objects.add(cc);
} else {
objects.add(cc);
}
infoMap.put(aa[i][j], objects);
}
}
isWrite = true;
return infoMap;
}
private static void buildJson(PrintWriter out, Map<Object, List<Object>> infoMap) {
int i = 0;
for (Map.Entry<Object, List<Object>> entry : infoMap.entrySet()) {
String[] split = entry.getKey().toString().split("#");
Object npc = entry.getKey();
if (split.length >= 2){
npc = split[1];
}
List<Object> value = entry.getValue();
StringBuilder groups = new StringBuilder();
for (Object info :value){
if (groups.length() == 0){
groups = new StringBuilder((String) info);
}else{
groups.append("|").append(info);
}
}
if (i == 0){
out.write("id\tEvent\tGroups");
out.write("\r\n");
out.write("int"+"\t"+"int"+"\t"+"mut,int#int,2");
out.write("\r\n");
}
i = i +1 ;
out.write( i+"\t"+npc+"\t"+groups.toString());
out.write("\r\n");
}
}
//读取excel
private static Workbook readExcel(String filePath) {
Workbook wb = null;
if (filePath == null) {
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));
InputStream is;
try {
is = new FileInputStream(filePath);
if (".xls".equals(extString)) {
return wb = new HSSFWorkbook(is);
} else if (".xlsx".equals(extString)) {
wb = new XSSFWorkbook(is);
return wb;
} else {
return wb = null;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wb;
}
public static Object getCellFormatValue(Cell cell) {
Object cellValue = null;
if (cell != null) {
//判断cell类型
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: {
cellValue = (int) cell.getNumericCellValue();
break;
}
case Cell.CELL_TYPE_FORMULA: {
//判断cell是否为日期格式
if (DateUtil.isCellDateFormatted(cell)) {
//转换为日期格式YYYY-mm-dd
cellValue = cell.getDateCellValue();
} else {
//数字
cellValue = String.valueOf(cell.getNumericCellValue());
}
break;
}
2019-01-15 15:03:28 +08:00
case Cell.CELL_TYPE_BOOLEAN:{
cellValue =cell.getBooleanCellValue();
break;
}
2019-01-14 16:02:43 +08:00
case Cell.CELL_TYPE_STRING: {
cellValue = cell.getRichStringCellValue().getString();
break;
}
default:
cellValue = "";
}
} else {
cellValue = "";
}
return cellValue;
}
}