单号规则管理excel导入导出

yun-zuoyi
yunhao.wang 7 years ago
parent 9db39d97cd
commit 696678930c

@ -65,14 +65,8 @@ public class SysOrderNoRuleController extends CoreBaseController {
@ApiOperation(value = "新增单号规则") @ApiOperation(value = "新增单号规则")
public ResultBean insertSysOrderNoRule(SysOrderNoRule sysOrderNoRule){ public ResultBean insertSysOrderNoRule(SysOrderNoRule sysOrderNoRule){
try { try {
ValidatorBean.beginValid(sysOrderNoRule) // 添加校验
.notNull("name",sysOrderNoRule.getName()) validatorSysOrderNoRule(sysOrderNoRule);
.notNull("orderNoRuleCode",sysOrderNoRule.getOrderNoRuleCode())
.notNull("orderNoRule",sysOrderNoRule.getOrderNoRule())
.checkNotZero("serialNoSeed",sysOrderNoRule.getSerialNoSeed())
.checkNotZero("serialNoIncrement",sysOrderNoRule.getSerialNoIncrement())
.checkNotZero("serialNoLength",sysOrderNoRule.getSerialNoLength())
.checkNotZero("isCycle",sysOrderNoRule.getIsCycle());
sysOrderNoRule.setOrderNoRuleStatus(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()); sysOrderNoRule.setOrderNoRuleStatus(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
sysOrderNoRule.setSerialNo(CommonEnumUtil.PARENT.DEFAULT.getValue()); sysOrderNoRule.setSerialNo(CommonEnumUtil.PARENT.DEFAULT.getValue());
@ -276,12 +270,20 @@ public class SysOrderNoRuleController extends CoreBaseController {
} }
} }
@GetMapping(value = "/import") @PostMapping(value = "/import")
@ApiOperation(value = "导入单号规则") @ApiOperation(value = "导入单号规则")
public ResultBean importSysOrderNoRule(@RequestParam("file") MultipartFile file){ public ResultBean importSysOrderNoRule(@RequestParam("file") MultipartFile file){
try { try {
List<SysOrderNoRule> sysOrderNoRuleList = ExcelUtil.importData(file.getName(),file.getInputStream(),SysOrderNoRule.class); List<SysOrderNoRule> sysOrderNoRuleList = ExcelUtil.importData(file.getOriginalFilename(),file.getInputStream(),SysOrderNoRule.class);
sysOrderNoRuleService.insertSysOrderNoRuleList(sysOrderNoRuleList); for (SysOrderNoRule item : sysOrderNoRuleList) {
//校验及初始化数据
validatorSysOrderNoRule(item);
item.setOrderNoRuleStatus(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
item.setSerialNo(CommonEnumUtil.PARENT.DEFAULT.getValue());
sysOrderNoRuleService.insertSysOrderNoRule(item);
}
return ResultBean.success("导出成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()); return ResultBean.success("导出成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
}catch(ImppBusiException busExcep){ }catch(ImppBusiException busExcep){
return ResultBean.fail(busExcep); return ResultBean.fail(busExcep);
@ -290,17 +292,44 @@ public class SysOrderNoRuleController extends CoreBaseController {
} }
} }
@GetMapping(value = "/import-template") @GetMapping(value = "/down-template")
@ApiOperation(value = "下载导入模板") @ApiOperation(value = "下载导入模板")
public ResultBean importSysOrderNoRuleTemplate(HttpServletResponse response, String[] colName){ public ResultBean downSysOrderNoRuleTemplate(){
try { try {
response.setContentType("application/force-download");// 设置强制下载不打开 FastDFSFile fastDFSFile = new FastDFSFile(SysOrderNoRule.class.getSimpleName() + "ImportTemplate.xls",
response.addHeader("Content-Disposition", "attachment;fileName=" + SysOrderNoRule.class.getSimpleName() + "Template.xls");// 设置文件名 ExcelUtil.importTemplate(SysOrderNoRule.class),"xls");
response.addHeader("Content-type", "application/octet-stream");// 设置文件名
String[] fileAbsolutePath = dfsClient.upload(fastDFSFile);
// ExcelUtil.importTemplate(response.getOutputStream(),SysOrderNoRule.class, colName);
// 保证系统文件表中
String filePath = dfsClient.getHostUi() + "/" + fileAbsolutePath[0] + "/" + fileAbsolutePath[1];
SysDictionary dictionary = sysDictionaryService.getSysDictionaryByParentCodeAndCode(
CommonConstWords.DICTIONARY_FILE_TYPE,fastDFSFile.getExt());
SysFile sysFile = new SysFile();
sysFile.setDfsGroupName(fileAbsolutePath[0]);
sysFile.setDfsFileName(fileAbsolutePath[1]);
sysFile.setFileSize(fastDFSFile.getContent().length);
sysFile.setFilePath(filePath);
sysFile.setFileOriginalName(fastDFSFile.getName());
sysFile.setFileTypeId(dictionary.getId());
sysFile.setFileTypeName(dictionary.getName());
sysFile.setDownloadNum(0);
sysFileService.insertSysFile(sysFile);
return ResultBean.success("导出成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(sysFile);
}catch(ImppBusiException busExcep){
return ResultBean.fail(busExcep);
}catch(Exception e){
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
}
}
return ResultBean.success("导出成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()); @GetMapping("/get-col")
@ApiOperation(value = "获取单号规则字段")
public ResultBean getSysOrderNoRuleColName(){
try {
return ResultBean.success("导出成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultMap(ExcelUtil.getColName(SysOrderNoRule.class));
}catch(ImppBusiException busExcep){ }catch(ImppBusiException busExcep){
return ResultBean.fail(busExcep); return ResultBean.fail(busExcep);
}catch(Exception e){ }catch(Exception e){
@ -308,4 +337,19 @@ public class SysOrderNoRuleController extends CoreBaseController {
} }
} }
/**
*
* @param sysOrderNoRule
* @throws NoSuchFieldException
*/
private void validatorSysOrderNoRule(SysOrderNoRule sysOrderNoRule) throws NoSuchFieldException {
ValidatorBean.beginValid(sysOrderNoRule)
.notNull("name",sysOrderNoRule.getName())
.notNull("orderNoRuleCode",sysOrderNoRule.getOrderNoRuleCode())
.notNull("orderNoRule",sysOrderNoRule.getOrderNoRule())
.checkNotZero("serialNoSeed",sysOrderNoRule.getSerialNoSeed())
.checkNotZero("serialNoIncrement",sysOrderNoRule.getSerialNoIncrement())
.checkNotZero("serialNoLength",sysOrderNoRule.getSerialNoLength())
.checkNotZero("isCycle",sysOrderNoRule.getIsCycle());
}
} }

@ -63,8 +63,7 @@ public class ExcelUtil {
HSSFWorkbook workbook = new HSSFWorkbook(); HSSFWorkbook workbook = new HSSFWorkbook();
try { try {
//创建临时文件 //创建临时文件
System.out.println(exportClass.getSimpleName() + TimeTool.getNowTime(true)); excelFile = File.createTempFile(exportClass.getSimpleName() + new Date().getTime(), ".xls");
excelFile = File.createTempFile(exportClass.getSimpleName() + new Date().getTime(),".xls");
LOGGER.info("临时文件所在的本地路径:" + excelFile.getCanonicalPath()); LOGGER.info("临时文件所在的本地路径:" + excelFile.getCanonicalPath());
fos = new FileOutputStream(excelFile); fos = new FileOutputStream(excelFile);
@ -192,11 +191,14 @@ public class ExcelUtil {
Row row; Row row;
Object obj; Object obj;
Object cellValue = null; Object cellValue = null;
for (int i = 0; i < sheet.getLastRowNum(); i++) { for (int i = 1; i <= sheet.getLastRowNum(); i++) {
row = sheet.getRow(i); row = sheet.getRow(i);
obj = importClass.newInstance(); obj = importClass.newInstance();
for (int j = 0; j < fields.length; j++) { for (int j = 0; j < fields.length; j++) {
if (fields[j].getType() == String.class) { row.getCell(j).setCellType(CellType.STRING);
if("".equals(row.getCell(j).getStringCellValue())){
cellValue = null;
}else if (fields[j].getType() == String.class) {
cellValue = row.getCell(j).getStringCellValue(); cellValue = row.getCell(j).getStringCellValue();
} else if (fields[j].getType() == Integer.class) { } else if (fields[j].getType() == Integer.class) {
cellValue = Integer.parseInt(row.getCell(j).getStringCellValue()); cellValue = Integer.parseInt(row.getCell(j).getStringCellValue());
@ -204,7 +206,7 @@ public class ExcelUtil {
cellValue = Long.parseLong(row.getCell(j).getStringCellValue()); cellValue = Long.parseLong(row.getCell(j).getStringCellValue());
} }
importClass.getDeclaredMethod("set" + StringTool.toUpperCaseFirstOne(fields[i].getName()), fields[i].getType()) importClass.getDeclaredMethod("set" + StringTool.toUpperCaseFirstOne(fields[j].getName()), fields[j].getType())
.invoke(obj, cellValue); .invoke(obj, cellValue);
} }
dataList.add(obj); dataList.add(obj);
@ -252,13 +254,19 @@ public class ExcelUtil {
* *
* *
* @param exportClass * @param exportClass
* @param exportCol
*/ */
public static byte[] importTemplate(Class exportClass, String[] exportCol) { public static byte[] importTemplate(Class exportClass) {
File excelFile = null;
FileOutputStream fos = null;
//创建HSSFWorkbook对象(excel的文档对象) //创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook workbook = new HSSFWorkbook(); HSSFWorkbook workbook = new HSSFWorkbook();
try { try {
//创建临时文件
excelFile = File.createTempFile(exportClass.getSimpleName() + new Date().getTime(), "Tp.xls");
LOGGER.info("临时文件所在的本地路径:" + excelFile.getCanonicalPath());
fos = new FileOutputStream(excelFile);
//建立新的sheet对象excel的表单 //建立新的sheet对象excel的表单
HSSFSheet sheet = workbook.createSheet(exportClass.getSimpleName()); HSSFSheet sheet = workbook.createSheet(exportClass.getSimpleName());
@ -268,27 +276,23 @@ public class ExcelUtil {
// 类数据 // 类数据
Field[] declaredFields = exportClass.getDeclaredFields(); Field[] declaredFields = exportClass.getDeclaredFields();
Field[] fields = new Field[exportCol.length];
ApiParam fieldAnno; ApiParam fieldAnno;
int col = 0;
for (int i = 0; i < exportCol.length; i++) { for (int i = 0; i < declaredFields.length; i++) {
for (Field field : declaredFields) { if (declaredFields[i].isAnnotationPresent(ApiParam.class)) {
if (field.getName().equals(exportCol[i])) { fieldAnno = declaredFields[i].getAnnotation(ApiParam.class);
fields[i] = field; if (!fieldAnno.hidden()) {
tableHeader.createCell(col, CellType.STRING).setCellValue(fieldAnno.value());
if (field.isAnnotationPresent(ApiParam.class)) {
fieldAnno = field.getAnnotation(ApiParam.class);
tableHeader.createCell(i, CellType.STRING).setCellValue(fieldAnno.value());
tableData.createCell(i, CellType.STRING).setCellValue(fieldAnno.example()); tableData.createCell(col, CellType.STRING).setCellValue(fieldAnno.example());
} col++;
} }
} }
} }
ByteArrayOutputStream os = new ByteArrayOutputStream(); workbook.write(fos);
workbook.write(os); return file2Byte(excelFile);
return os.toString("utf-8").getBytes();
} catch (IOException e) { } catch (IOException e) {
throw ImppExceptionBuilder.newInstance() throw ImppExceptionBuilder.newInstance()
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode()) .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
@ -298,12 +302,36 @@ public class ExcelUtil {
} finally { } finally {
try { try {
workbook.close(); workbook.close();
fos.close();
excelFile.deleteOnExit();
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("IOException!", e); LOGGER.error("IOException!", e);
} }
} }
} }
/**
*
*
* @param pojoClass
* @return
*/
public static Map getColName(Class pojoClass) {
Map colName = new LinkedHashMap();
Field[] field = pojoClass.getDeclaredFields();
ApiParam fieldAnno;
for (int i = 0; i < field.length; i++) {
if (field[i].isAnnotationPresent(ApiParam.class)) {
fieldAnno = field[i].getAnnotation(ApiParam.class);
if (!fieldAnno.hidden()) {
colName.put(field[i].getAnnotation(ApiParam.class).value(), field[i].getName());
}
}
}
return colName;
}
public static void main(String[] args) { public static void main(String[] args) {
List<SysTool> sysTools = new ArrayList<>(); List<SysTool> sysTools = new ArrayList<>();
@ -327,30 +355,22 @@ public class ExcelUtil {
} }
public static byte[] file2Byte(File file) {
public static byte[] file2Byte(File file)
{
byte[] buffer = null; byte[] buffer = null;
try try {
{
FileInputStream fis = new FileInputStream(file); FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024]; byte[] b = new byte[1024];
int n; int n;
while ((n = fis.read(b)) != -1) while ((n = fis.read(b)) != -1) {
{
bos.write(b, 0, n); bos.write(b, 0, n);
} }
fis.close(); fis.close();
bos.close(); bos.close();
buffer = bos.toByteArray(); buffer = bos.toByteArray();
} } catch (FileNotFoundException e) {
catch (FileNotFoundException e)
{
e.printStackTrace(); e.printStackTrace();
} } catch (IOException e) {
catch (IOException e)
{
e.printStackTrace(); e.printStackTrace();
} }
return buffer; return buffer;

Loading…
Cancel
Save