单号规则管理excel导入导出

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

@ -65,14 +65,8 @@ public class SysOrderNoRuleController extends CoreBaseController {
@ApiOperation(value = "新增单号规则")
public ResultBean insertSysOrderNoRule(SysOrderNoRule sysOrderNoRule){
try {
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());
// 添加校验
validatorSysOrderNoRule(sysOrderNoRule);
sysOrderNoRule.setOrderNoRuleStatus(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
sysOrderNoRule.setSerialNo(CommonEnumUtil.PARENT.DEFAULT.getValue());
@ -276,12 +270,20 @@ public class SysOrderNoRuleController extends CoreBaseController {
}
}
@GetMapping(value = "/import")
@PostMapping(value = "/import")
@ApiOperation(value = "导入单号规则")
public ResultBean importSysOrderNoRule(@RequestParam("file") MultipartFile file){
try {
List<SysOrderNoRule> sysOrderNoRuleList = ExcelUtil.importData(file.getName(),file.getInputStream(),SysOrderNoRule.class);
sysOrderNoRuleService.insertSysOrderNoRuleList(sysOrderNoRuleList);
List<SysOrderNoRule> sysOrderNoRuleList = ExcelUtil.importData(file.getOriginalFilename(),file.getInputStream(),SysOrderNoRule.class);
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());
}catch(ImppBusiException busExcep){
return ResultBean.fail(busExcep);
@ -290,17 +292,32 @@ public class SysOrderNoRuleController extends CoreBaseController {
}
}
@GetMapping(value = "/import-template")
@GetMapping(value = "/down-template")
@ApiOperation(value = "下载导入模板")
public ResultBean importSysOrderNoRuleTemplate(HttpServletResponse response, String[] colName){
public ResultBean downSysOrderNoRuleTemplate(){
try {
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + SysOrderNoRule.class.getSimpleName() + "Template.xls");// 设置文件名
response.addHeader("Content-type", "application/octet-stream");// 设置文件名
FastDFSFile fastDFSFile = new FastDFSFile(SysOrderNoRule.class.getSimpleName() + "ImportTemplate.xls",
ExcelUtil.importTemplate(SysOrderNoRule.class),"xls");
// ExcelUtil.importTemplate(response.getOutputStream(),SysOrderNoRule.class, colName);
String[] fileAbsolutePath = dfsClient.upload(fastDFSFile);
return ResultBean.success("导出成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
// 保证系统文件表中
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){
@ -308,4 +325,31 @@ public class SysOrderNoRuleController extends CoreBaseController {
}
}
@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){
return ResultBean.fail(busExcep);
}catch(Exception e){
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
}
}
/**
*
* @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();
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());
fos = new FileOutputStream(excelFile);
@ -192,11 +191,14 @@ public class ExcelUtil {
Row row;
Object obj;
Object cellValue = null;
for (int i = 0; i < sheet.getLastRowNum(); i++) {
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
row = sheet.getRow(i);
obj = importClass.newInstance();
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();
} else if (fields[j].getType() == Integer.class) {
cellValue = Integer.parseInt(row.getCell(j).getStringCellValue());
@ -204,7 +206,7 @@ public class ExcelUtil {
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);
}
dataList.add(obj);
@ -252,13 +254,19 @@ public class ExcelUtil {
*
*
* @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 workbook = new HSSFWorkbook();
try {
//创建临时文件
excelFile = File.createTempFile(exportClass.getSimpleName() + new Date().getTime(), "Tp.xls");
LOGGER.info("临时文件所在的本地路径:" + excelFile.getCanonicalPath());
fos = new FileOutputStream(excelFile);
//建立新的sheet对象excel的表单
HSSFSheet sheet = workbook.createSheet(exportClass.getSimpleName());
@ -268,27 +276,23 @@ public class ExcelUtil {
// 类数据
Field[] declaredFields = exportClass.getDeclaredFields();
Field[] fields = new Field[exportCol.length];
ApiParam fieldAnno;
int col = 0;
for (int i = 0; i < exportCol.length; i++) {
for (Field field : declaredFields) {
if (field.getName().equals(exportCol[i])) {
fields[i] = field;
if (field.isAnnotationPresent(ApiParam.class)) {
fieldAnno = field.getAnnotation(ApiParam.class);
tableHeader.createCell(i, CellType.STRING).setCellValue(fieldAnno.value());
for (int i = 0; i < declaredFields.length; i++) {
if (declaredFields[i].isAnnotationPresent(ApiParam.class)) {
fieldAnno = declaredFields[i].getAnnotation(ApiParam.class);
if (!fieldAnno.hidden()) {
tableHeader.createCell(col, 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(os);
return os.toString("utf-8").getBytes();
workbook.write(fos);
return file2Byte(excelFile);
} catch (IOException e) {
throw ImppExceptionBuilder.newInstance()
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
@ -298,12 +302,36 @@ public class ExcelUtil {
} finally {
try {
workbook.close();
fos.close();
excelFile.deleteOnExit();
} catch (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) {
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;
try
{
try {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1)
{
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
}
catch (FileNotFoundException e)
{
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e)
{
} catch (IOException e) {
e.printStackTrace();
}
return buffer;

Loading…
Cancel
Save