需求介绍
为了处理数据,我对之前之前处理普遍的数据java脚本,做了一个更改,之前的程序主要还是对表格的数据进行替换,需要耗费一定的时间整理表格,进行一些数字的替换。
具体链接在这里(对excle直接生成json):https://blog.csdn.net/xiucai5211314/article/details/123917264
为了在大量关键字,例如 姓名,年龄,性别,…等一百个关键字中找出50个并且对于单元格中关键字进行替换为数值,如 男:0 女:1 是:1 否:0,之前我都用以上的程序在excle表中进行替换,单元格中的内容替换并不难,关键难得是,从众多关键字中找出五十列.
上边描述了这么多,那主要说下需求, 将excle中得数据表,按照一定数据字典,进行输出json数据
即按照以下数据字典
从以下带有多个列得表中
得到如下对应关系得json
细分代码功能
1.得到数据字典的数据
建立数组:用于存储数据字典每一行的内容,
MAP_ARR_MAX:代表有几个属性值,这个值是必须要改的
得到表对象,以及行列,单元格
workbook = Workbook.getWorkbook(new File(dataDictionaryPath));
Sheet sheet = null;
sheet = workbook.getSheet(0);
int rows = 0;
rows = sheet.getRows();
int column = 0;
column = sheet.getColumns();
将单元格数据切分,并且放入map中,将每一行对应的map放在数组中
这一部分代码
public static Object[] getMaps( String dataDictionaryPath) {
Workbook workbook = null;
Object[] mapArr = new Object[0];
try {
workbook = Workbook.getWorkbook(new File(dataDictionaryPath));
Sheet sheet = null;
sheet = workbook.getSheet(0);
int rows = 0;
rows = sheet.getRows();
int column = 0;
column = sheet.getColumns();
mapArr = new Object[MAP_ARR_MAX];
for (int q = 1; q < rows; q++) {
Map rowMap = new LinkedHashMap();
for (int j = 0; j < 2; j++) {
Cell cell = null;
cell = sheet.getCell(j, q);
String contents = cell.getContents();
if (j == 0) {
String[] splitArr = DataSparate.getSplitArr(contents, ":");
rowMap.put(splitArr[0], splitArr[1]);
} else {
String[] arr = DataSparate.getSplitArr(contents, ",");
for (int i = 0; i < arr.length; i++) {
if (!contents.equals("数值")&& !contents.equals("文本")) {
String[] splitArr = DataSparate.getSplitArr(arr[i], ":");
rowMap.put(splitArr[0],Integer.parseInt(splitArr[1]) );
}
}
}
mapArr[q - 1] = rowMap;
}
}
return mapArr;
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
return mapArr;
}
2 得到json
public static void getJson(String inFileXlsPath,String outJsonPath,String dataDictionaryPath){
Object[] maps = getMaps(dataDictionaryPath);
Workbook workbook = null;
JSONArray jsons = new JSONArray();
Sheet sheet = null;
int sheettall;
int rows = 0;
int column = 0;
Cell cell= null;
Cell cellTitle= null;
Cell cellRemark= null;
try {
workbook = Workbook.getWorkbook(new File(inFileXlsPath));
for (int i = 0; i < 1; i++) {
sheet = workbook.getSheet(i);
rows = sheet.getRows();
column = sheet.getColumns();
for (int q = 1; q < rows; q++) {
JSONObject object = new JSONObject(new LinkedHashMap());
for(int j = 0; j < sheet.getColumns(); j++){
cell = sheet.getCell(j , q);
cellTitle = sheet.getCell(j , 0);
String dataTitle=cellTitle.getContents();
String dataCell=cell.getContents();
for (int k = 0; k < MAP_ARR_MAX; k++) {
Map map= (Map) maps[k];
Set set =map.entrySet();
Iterator iterator=set.iterator();
Map.Entry titleMap= (Map.Entry) iterator.next();
String title= (String) titleMap.getKey();
String englishTitle= (String) titleMap.getValue();
System.out.println(title);
if (!dataTitle.equals(title)){
continue;
}
if (iterator.hasNext()){
Map.Entry contentMap= (Map.Entry) iterator.next();
while(!dataCell.equals(contentMap.getKey())){
if (iterator.hasNext()){
contentMap= (Map.Entry) iterator.next();
}else{
break;
}
}
int content= (int) contentMap.getValue();
switch (cell.getType().toString()){
case "Label":
object.put(englishTitle, content);
break;
case "Number":
Double number=new Double(cell.getContents());
object.put(englishTitle, number);
break;
case "Empty":
object.put(englishTitle,"NaN");
break;
default:
String scontent=cell.getContents();
object.put(englishTitle, scontent);
}
}else{
switch (cell.getType().toString()){
case "Label":
object.put(englishTitle, cell.getContents());
break;
case "Number":
Double number=new Double(cell.getContents());
object.put(englishTitle, number);
break;
case "Empty":
object.put(englishTitle,"NaN");
break;
default:
String scontent=cell.getContents();
object.put(englishTitle, scontent);
}
}
}
}
jsons.add(object);
}
}
System.out.println(jsons);
createJsonFile(jsons,outJsonPath);
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
}
3.数组截切代码
public static String[] getSplitArr (String data,String symbol){
String[] strArr =data.split(symbol);
return strArr;
}
4.数组写出json代码
public static boolean createJsonFile(Object jsonData, String filePath) {
String content = JSON.toJSONString(jsonData, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat);
boolean flag = true;
try {
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (file.exists()) {
file.delete();
}
file.createNewFile();
Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
write.write(content);
write.flush();
write.close();
} catch (Exception e) {
flag = false;
e.printStackTrace();
}
return flag;
}
整体代码
git地址:https://gitee.com/huang-zhihang/untils.git
如果直接复制其中需要一如上边写的数组切割代码与写出json代码
package com.xiucai.untils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import java.io.File;
import java.io.IOException;
import java.util.*;
import static com.xiucai.untils.ExcleToJsonUntils.createJsonFile;
public class AutoExcleToJsonUntils {
private final static int MAP_ARR_MAX = 4;
public static void main(String[] args) {
String inFileXlsPath = "C:\\Users\\HuangZhihang\\Desktop\\testage.xls";
String outJsonPath = "C:\\Users\\HuangZhihang\\Desktop\\测试.json";
String dataDictionaryPath = "C:\\\\Users\\\\HuangZhihang\\\\Desktop\\\\select测试数据.xls";
getJson(inFileXlsPath, outJsonPath, dataDictionaryPath);
}
public static Object[] getMaps(String dataDictionaryPath) {
Workbook workbook = null;
Object[] mapArr = new Object[0];
try {
workbook = Workbook.getWorkbook(new File(dataDictionaryPath));
Sheet sheet = null;
sheet = workbook.getSheet(0);
int rows = 0;
rows = sheet.getRows();
int column = 0;
column = sheet.getColumns();
mapArr = new Object[MAP_ARR_MAX];
for (int q = 1; q < rows; q++) {
Map rowMap = new LinkedHashMap();
for (int j = 0; j < 2; j++) {
Cell cell = null;
cell = sheet.getCell(j, q);
String contents = cell.getContents();
if (j == 0) {
String[] splitArr = DataSparate.getSplitArr(contents, ":");
rowMap.put(splitArr[0], splitArr[1]);
} else {
String[] arr = DataSparate.getSplitArr(contents, ",");
for (int i = 0; i < arr.length; i++) {
if (!contents.equals("数值") && !contents.equals("文本")) {
String[] splitArr = DataSparate.getSplitArr(arr[i], ":");
rowMap.put(splitArr[0], Integer.parseInt(splitArr[1]));
}
}
}
mapArr[q - 1] = rowMap;
}
}
return mapArr;
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
return mapArr;
}
public static void getJson(String inFileXlsPath, String outJsonPath, String dataDictionaryPath) {
Object[] maps = getMaps(dataDictionaryPath);
Workbook workbook = null;
JSONArray jsons = new JSONArray();
Sheet sheet = null;
int sheettall;
int rows = 0;
int column = 0;
Cell cell = null;
Cell cellTitle = null;
Cell cellRemark = null;
try {
workbook = Workbook.getWorkbook(new File(inFileXlsPath));
for (int i = 0; i < 1; i++) {
sheet = workbook.getSheet(i);
rows = sheet.getRows();
column = sheet.getColumns();
for (int q = 1; q < rows; q++) {
JSONObject object = new JSONObject(new LinkedHashMap());
for (int j = 0; j < sheet.getColumns(); j++) {
cell = sheet.getCell(j, q);
cellTitle = sheet.getCell(j, 0);
String dataTitle = cellTitle.getContents();
String dataCell = cell.getContents();
for (int k = 0; k < MAP_ARR_MAX; k++) {
Map map = (Map) maps[k];
Set set = map.entrySet();
Iterator iterator = set.iterator();
Map.Entry titleMap = (Map.Entry) iterator.next();
String title = (String) titleMap.getKey();
String englishTitle = (String) titleMap.getValue();
System.out.println(title);
if (!dataTitle.equals(title)) {
continue;
}
if (iterator.hasNext()) {
Map.Entry contentMap = (Map.Entry) iterator.next();
while (!dataCell.equals(contentMap.getKey())) {
if (iterator.hasNext()) {
contentMap = (Map.Entry) iterator.next();
} else {
break;
}
}
int content = (int) contentMap.getValue();
switch (cell.getType().toString()) {
case "Label":
object.put(englishTitle, content);
break;
case "Number":
Double number = new Double(cell.getContents());
object.put(englishTitle, number);
break;
case "Empty":
object.put(englishTitle, "NaN");
break;
default:
String scontent = cell.getContents();
object.put(englishTitle, scontent);
}
} else {
switch (cell.getType().toString()) {
case "Label":
object.put(englishTitle, cell.getContents());
break;
case "Number":
Double number = new Double(cell.getContents());
object.put(englishTitle, number);
break;
case "Empty":
object.put(englishTitle, "NaN");
break;
default:
String scontent = cell.getContents();
object.put(englishTitle, scontent);
}
}
}
}
jsons.add(object);
}
}
System.out.println(jsons);
createJsonFile(jsons, outJsonPath);
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
}
}
|