excel转为配置数据

master
gaojie 2019-01-29 18:24:09 +08:00
parent 894791394f
commit 5f8983585b
5 changed files with 383 additions and 3 deletions

View File

@ -7,6 +7,7 @@ apply plugin: 'war'
sourceCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
}
@ -16,6 +17,8 @@ dependencies {
compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.2.2'
compile("ch.qos.logback:logback-core:1.1.11")
compile("ch.qos.logback:logback-classic:1.1.11")
compile("org.apache.poi:poi-ooxml:3.12")
compile group: 'redis.clients', name: 'jedis', version: '2.9.0'
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.47'
compile group: 'jcifs', name: 'jcifs', version: '1.3.17'
}

View File

@ -1,6 +1,6 @@
#Wed Dec 26 12:01:28 CST 2018
#Tue Jan 29 11:36:14 CST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-rc-2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-rc-2-all.zip

View File

@ -0,0 +1,42 @@
package com.ljsd.controller;
import com.ljsd.util.ExcelUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class ExcelController extends HttpServlet {
public ExcelController() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter out = response.getWriter();
ExcelUtils.readExcelData();
DBObject resp = new BasicDBObject();
resp.put("state", 0);
resp.put("msg", "success");
out.print(resp.toString());
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
this.doGet(request, response);
}
}

View File

@ -0,0 +1,326 @@
package com.ljsd.util;
import jcifs.smb.SmbFile;
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.*;
public class ExcelUtils {
private static boolean isWrite = true;
private static String excelPath ="smb://60.1.1.12/Public/jieling/conf/data_exed/"; //excel 文件
private static String path = "//60.1.1.12/Public/jieling/conf/server/";
// private static String path = "D:/tmp/";
private static Set<String> oldFileNames = new HashSet<>();
public static void main(String[] args) throws IOException {
File oldfile = new File(path);
String[] list = oldfile.list();
oldFileNames.addAll(Arrays.asList(list));
readExcelData();
}
public static void readExcelData() throws IOException {
SmbFile file = new SmbFile(excelPath);
if (file.exists()) {
SmbFile[] files = file.listFiles();
for (SmbFile file1 : files) {
if (file1.isDirectory()) {
continue;
}
String extString = file1.getName().substring(file1.getName().lastIndexOf("."));
if (!".xlsx".equals(extString)) {
continue;
}
String filePath =file1.getUncPath();
Workbook wb = readExcel(filePath);
if(file1.getName().contains("1.Map_")){
bulidMapInfo(wb);
}else{
buildDataInfo(wb);
}
}
}
}
private static String typeInfo(String fieldInfo){
//mut,int#int,2
if(fieldInfo.contains("mut")){
String[] split = fieldInfo.split("#");
String[] typeInfos = split[1].split(",");
int nums = Integer.parseInt(typeInfos[1]);
StringBuffer sb = new StringBuffer();
if("string".equals(typeInfos[0])){
sb.append("String");
}else{
sb.append(typeInfos[0]);
}
for(int i=0;i<nums;i++){
sb.append("[]");
}
return sb.toString();
}
if("string".equals(fieldInfo)){
return "String";
}
return fieldInfo;
}
private static void buildDataInfo(Workbook wb) throws IOException {
Sheet sheet;
Row row;
Object cellData;
sheet = wb.getSheetAt(0);
String sheetName = sheet.getSheetName();
System.out.println("bulidMapInfo => collection name="+sheetName);
int rownum = sheet.getLastRowNum();
FileWriter fw = new FileWriter(path + sheetName + ".txt");
PrintWriter out = new PrintWriter(fw);
for (int i = 0; i <= rownum; i++) {
if (i == 2 || i == 3 || i == 4 ||i == 5||i == 6){
continue;
}
Row row1 = sheet.getRow(2);
Row row2 = sheet.getRow(1);
Row row4 = sheet.getRow(4);
row = sheet.getRow(i);
// for (Cell c :row){
// System.out.println(c.getColumnIndex() +"==="+row.getCell(c.getColumnIndex()));
// }
if (row != null) {
StringBuilder info = new StringBuilder();
int colnum = row1.getLastCellNum();
for (int j = 1; j < colnum; j++) {
int cellFormatValue = (int) getCellFormatValue(row1.getCell(j),null);
if (cellFormatValue == 3){
continue;
}
Cell cell = row.getCell(j);
if (cell == null){
Cell cell1 = row4.getCell(j);
info = getStringBuilder(row2, info, j, cell1);
}else{
if (getCellFormatValue(row1.getCell(j),null).toString().isEmpty()){
continue;
}
//文件名
cellData = getCellFormatValue(cell,row2.getCell(j));
if ("".equals(cellData.toString())){
cellData = getIn(row2.getCell(j).toString());
}
if (info.length() == 0){
info = info.append(cellData);
}else{
info.append("\t").append(cellData);
}
}
}
out.write(info.toString());
out.write("\r\n");
}
}
fw.close();
out.close();
}
private static StringBuilder getStringBuilder(Row row2, StringBuilder info, int j, Cell cell1) {
Object cellData;
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,row2.getCell(j));
if (info.length() == 0){
info = info.append(cellData);
}else{
info.append("\t").append(cellData);
}
}
return info;
}
private static String getIn(String row2) {
String str = "";
switch (row2) {
case "bool":
str = "false";
break;
case "int":
case "float":
str = "0";
break;
case "string":
break;
default:
str = null;
}
return str;
}
private static void bulidMapInfo(Workbook wb) throws IOException {
Sheet sheet = wb.getSheetAt(0);
String sheetName = sheet.getSheetName();
System.out.println("bulidMapInfo => collection name="+sheetName);
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),null);
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,Cell cellType) {
Object cellValue = null;
if (cell != null) {
//判断cell类型
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: {
if (cellType != null && cellType.toString().equals("float")){
cellValue = cell.getNumericCellValue();
}else{
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;
}
case Cell.CELL_TYPE_BOOLEAN:{
cellValue =cell.getBooleanCellValue();
break;
}
case Cell.CELL_TYPE_STRING: {
cellValue = cell.getRichStringCellValue().getString();
break;
}
default:
cellValue = "";
}
} else {
cellValue = "";
}
return cellValue;
}
}

View File

@ -54,12 +54,21 @@
<servlet-name>serverList</servlet-name>
<servlet-class>com.ljsd.controller.ServerListController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>serverList</servlet-name>
<url-pattern>/serverList</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>excelData</servlet-name>
<servlet-class>com.ljsd.controller.ExcelController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>excelData</servlet-name>
<url-pattern>/excelData</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.ljsd.listener.WebContextListener</listener-class>