|
|
|
@ -0,0 +1,52 @@
|
|
|
|
|
package com.dsideal.base.Tools;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.poi.excel.ExcelReader;
|
|
|
|
|
import cn.hutool.poi.excel.ExcelUtil;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
public class DataTransformer {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
String input = "D:\\dsWork\\dsProject\\dsBase\\src\\main\\java\\com\\dsideal\\base\\Tools\\Sample.xlsx";
|
|
|
|
|
File file = new File(input);
|
|
|
|
|
|
|
|
|
|
//哪些列是固定不动的,比如此处是第0列与第1列是不动的,就是年份+学段
|
|
|
|
|
List<Integer> fixedColumns = new ArrayList<>();
|
|
|
|
|
fixedColumns.add(0);
|
|
|
|
|
fixedColumns.add(1);
|
|
|
|
|
|
|
|
|
|
//每列的列号与列名对应关系
|
|
|
|
|
String[] colNames = new String[1024];
|
|
|
|
|
int cl = 0;
|
|
|
|
|
|
|
|
|
|
// 读取Excel数据
|
|
|
|
|
ExcelReader reader = ExcelUtil.getReader(file);
|
|
|
|
|
|
|
|
|
|
//表头
|
|
|
|
|
List<List<Object>> read = reader.read(0, 0);//第0行是表头
|
|
|
|
|
for (int i = 0; i < reader.getColumnCount(); i++) {
|
|
|
|
|
colNames[cl++] = read.getFirst().get(i).toString();
|
|
|
|
|
}
|
|
|
|
|
//数据
|
|
|
|
|
List<List<Object>> data = reader.read(1, reader.getRowCount());//从第1行读取到最后一行
|
|
|
|
|
for (List<Object> row : data) {
|
|
|
|
|
//拆分每一行的数据
|
|
|
|
|
for (int i = 0; i < reader.getColumnCount(); i++) {
|
|
|
|
|
if (!fixedColumns.contains(i)) {//非fixedColumns列进行枚举
|
|
|
|
|
for (int cNum : fixedColumns) {
|
|
|
|
|
String cName = colNames[cNum];//列名
|
|
|
|
|
String value = row.get(cNum).toString();//列值
|
|
|
|
|
System.out.print(cName + ":" + value+" ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String cName = colNames[i];//列名
|
|
|
|
|
String value = row.get(i).toString();
|
|
|
|
|
System.out.print(cName + ":" + value);
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|