main
黄海 2 years ago
parent a146855f3e
commit 2cbe1a40d0

@ -29,7 +29,7 @@ public class CreateTableAndTemplate {
//用户上传的模板
public static String userUploadExcel = "source.xlsx";
//我处理后的模板
public static String upload_excel_filename;
public static String upload_excel_filename = "b61a2af2-223f-4058-a675-b212e4dd9487" + ".xlsx";
/**
*
@ -71,8 +71,9 @@ public class CreateTableAndTemplate {
arp.setDialect(new PostgreSqlDialect());
arp.start();
//源文件
upload_excel_filename = UUID.randomUUID().toString().toLowerCase() + ".xlsx";
//开发时暂时写死源文件名称
//upload_excel_filename = UUID.randomUUID().toString().toLowerCase() + ".xlsx";
String source = path + File.separator + upload_excel_filename;
if (FileUtil.exist(source)) FileUtil.del(source);
//复制成我自己的,随便折腾
@ -165,6 +166,7 @@ public class CreateTableAndTemplate {
sheetIdx++;
}
// 保存
wb.setActiveSheet(0); // 设置活动表单为第一个表单
FileOutputStream fileOut = new FileOutputStream(source);
wb.write(fileOut);
//关闭Excel

@ -3,13 +3,22 @@ package UnitTest.ImportExcel;
import com.dsideal.QingLong.Util.AsposeUtil;
import com.dsideal.QingLong.Util.ImportUtil;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
@ -19,6 +28,8 @@ public class ImportExcelData {
//模拟上传文件的文件名称
public static String upload_excel_filename = "b61a2af2-223f-4058-a675-b212e4dd9487" + ".xlsx";
public static void main(String[] args) throws IOException {
//加载License
AsposeUtil.getLicense();
@ -48,17 +59,50 @@ public class ImportExcelData {
return;
}
}
System.out.println("OK,是模板!");
System.out.println("OK,是我下发的模板!");
// 检查是不是非空项目未填写
for (int i = 0; i < sheetCnt; i++) {
//GenericTemplateUtil.getExcelSheetColumnsInfo(upload_excel_filename, );
}
// 检查是不是下拉列表不是从下拉列表中选择的
//打开EXCEL进行检查
InputStream is = new FileInputStream(f2);
XSSFWorkbook wb = new XSSFWorkbook(is);
//遍历每个Sheet注册好的信息对用户上传的数据表进行检查
for (int i = 0; i < sheetCnt; i++) {//表
//Sheet表
XSSFSheet sheet = wb.getSheetAt(i);
//数据起始行
Record r = ImportUtil.getExcelSheetInfo(upload_excel_filename, i);
int data_start_row = r.getInt("data_start_row");
//数据有效行数
int lastRowNum = sheet.getLastRowNum();
List<Record> list = ImportUtil.getExcelSheetColumnsInfo(upload_excel_filename, i);
//遍历每一列
int colIdx = 0;
for (Record record : list) {//列
int excel_column_idx = record.getInt("excel_column_idx");//第几列
String column_type = record.getStr("column_type");//类型
boolean allow_blank = record.getBoolean("allow_blank");//是不是允许为空
// 检查数据类型是不是和规定的不兼容
if (!allow_blank) {
// 非空项目未填写
for (int j = data_start_row; j <= lastRowNum; j++) {//行
if (StrKit.isBlank(ImportUtil.getCellValue(sheet.getRow(j).getCell(colIdx)).toString())) {
System.out.println("Sheet=" + sheet.getSheetName() + ",Rows=" + j + ",Column=" + colIdx + "不允许为空!");
System.exit(0);
}
}
}
// 下拉列表不是从下拉列表中选择的
// 检查数据类型是不是和规定的不兼容
colIdx++;
}
}
wb.close();
// 全都通过,导入数据
}

@ -543,4 +543,31 @@ public class ImportUtil {
// 应用数据验证到单元格
sheet.addValidationData(validation);
}
/**
* Cell
*
* @param cell
* @return
*/
public static Object getCellValue(XSSFCell cell) {
switch (cell.getCellType()) {
case CellType.STRING:
String cellValueString = cell.getStringCellValue();
return cellValueString;
case CellType.NUMERIC:
double cellValueNumeric = cell.getNumericCellValue();
return cellValueNumeric;
case CellType.BOOLEAN:
boolean cellValueBoolean = cell.getBooleanCellValue();
return cellValueBoolean;
case CellType.BLANK:
cellValueString = cell.getStringCellValue();
return cellValueString;
// 其他类型的处理
default:
cellValueString = cell.getStringCellValue();
return cellValueString;
}
}
}

Loading…
Cancel
Save