|
|
|
@ -0,0 +1,115 @@
|
|
|
|
|
package com.dsideal.base.Tools.Controller;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.poi.excel.ExcelReader;
|
|
|
|
|
import cn.hutool.poi.excel.ExcelUtil;
|
|
|
|
|
import cn.hutool.poi.excel.ExcelWriter;
|
|
|
|
|
import com.dsideal.base.Util.CommonUtil;
|
|
|
|
|
import com.jfinal.aop.Before;
|
|
|
|
|
import com.jfinal.core.Controller;
|
|
|
|
|
import com.jfinal.ext.interceptor.GET;
|
|
|
|
|
import com.jfinal.ext.interceptor.POST;
|
|
|
|
|
import com.jfinal.kit.Kv;
|
|
|
|
|
import com.jfinal.kit.StrKit;
|
|
|
|
|
import com.jfinal.upload.UploadFile;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
public class excelConvertController extends Controller {
|
|
|
|
|
// 获取系统的临时文件目录
|
|
|
|
|
private final String tempDir = System.getProperty("java.io.tmpdir");
|
|
|
|
|
|
|
|
|
|
@Before({POST.class})
|
|
|
|
|
public void doTransformer() {
|
|
|
|
|
UploadFile excelFile = getFile();//得到 文件对象
|
|
|
|
|
String fileName = excelFile.getFileName();
|
|
|
|
|
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1).trim();
|
|
|
|
|
if (!suffix.equals("xlsx")) {
|
|
|
|
|
renderJson(CommonUtil.returnMessageJson(false, "上传文件类型错误!系统只允许上传xlsx格式!"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String input = excelFile.getFile().getAbsolutePath();
|
|
|
|
|
File file = new File(input);
|
|
|
|
|
String fixColums = getPara("fixColums");
|
|
|
|
|
if (StrKit.isBlank(fixColums)) {
|
|
|
|
|
renderJson(CommonUtil.returnMessageJson(false, "fixColums请输入固定列! 格式:0,1"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String regex = "^[0-9]+(,[0-9]+)*$";
|
|
|
|
|
if (!Pattern.matches(regex, fixColums)) {
|
|
|
|
|
renderJson(CommonUtil.returnMessageJson(false, "fixColums固定列格式错误! 格式:0,1"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//哪些列是固定不动的,比如此处是第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);
|
|
|
|
|
// 创建临时文件,文件名具有随机UUID,扩展名为.xlsx
|
|
|
|
|
|
|
|
|
|
//利用uuid生成一个临时文件名
|
|
|
|
|
String uuidFileName = UUID.randomUUID().toString().toUpperCase() + ".xlsx";
|
|
|
|
|
String output = tempDir + uuidFileName;
|
|
|
|
|
// 创建一个ExcelWriter对象,初始化时会创建一个空的Excel文件
|
|
|
|
|
ExcelWriter writer = ExcelUtil.getWriter(output);
|
|
|
|
|
//表头
|
|
|
|
|
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<String> header = new ArrayList<>();
|
|
|
|
|
for (int cNum : fixedColumns) {
|
|
|
|
|
String cName = colNames[cNum];//列名
|
|
|
|
|
header.add(cName);
|
|
|
|
|
}
|
|
|
|
|
header.add("类型");
|
|
|
|
|
header.add("数量");
|
|
|
|
|
writer.writeHeadRow(header);
|
|
|
|
|
|
|
|
|
|
//数据
|
|
|
|
|
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列进行枚举
|
|
|
|
|
|
|
|
|
|
List<String> rowData = new ArrayList<>();
|
|
|
|
|
//输出固定列的名称和值
|
|
|
|
|
for (int cNum : fixedColumns) {
|
|
|
|
|
String value = row.get(cNum).toString();//列值
|
|
|
|
|
rowData.add(value);
|
|
|
|
|
}
|
|
|
|
|
String cName = colNames[i];//列名
|
|
|
|
|
String value = row.get(i).toString();
|
|
|
|
|
rowData.add(cName);
|
|
|
|
|
rowData.add(value);
|
|
|
|
|
writer.writeRow(rowData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
writer.flush();
|
|
|
|
|
writer.close();
|
|
|
|
|
reader.close();
|
|
|
|
|
Kv kv = Kv.by("success", true);
|
|
|
|
|
kv.set("uuidFileName", uuidFileName);
|
|
|
|
|
renderJson(kv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Before({GET.class})
|
|
|
|
|
public void download(String uuidFileName) throws IOException {
|
|
|
|
|
String output = tempDir + uuidFileName;
|
|
|
|
|
renderFile(new File(output), "转换结果.xlsx");
|
|
|
|
|
}
|
|
|
|
|
}
|