增加了一些文件处理工具类

完善了一下代码
master
lenovo 2 years ago
parent ed238a5881
commit 12e5b2ccc6

@ -8,6 +8,7 @@ import com.gmh.entity.RowCellNum;
import com.gmh.entity.SysBaijiaxing;
import com.gmh.entity.vo.ReadNameVo;
import com.gmh.service.SysBaijiaxingService;
import com.gmh.utils.CommonUtils;
import com.gmh.utils.POIExcelUtil;
import com.gmh.utils.RedisUtils;
import com.gmh.utils.RedisUtilsDefault;
@ -18,12 +19,16 @@ import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Pattern;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import java.util.stream.Collectors;
@ -126,41 +131,29 @@ public class ExcelController {
}
@RequestMapping("/cellNumSearch")
public R analysisCell(String cellNum) {
String cellStrNum = cellNum.replaceAll("[^a-z^A-Z]", "");
String rowNum = cellNum.replaceAll("[^0-9]", "");
if (cellStrNum.length() > 2) {
return R.error(cellNum + "单元格不存在");
}
public R analysisCell(@Validated RowCellNum rowCellNum) {
if (this.templateWorkbook == null) {
return R.error("模板文件出问题了,请重新上传");
}
Sheet sheet = this.templateWorkbook.getSheetAt(this.templateSheetNum);
Row row = sheet.getRow(Integer.parseInt(rowNum) - 1);
rowCellNum = POIExcelUtil.analysisCellRowNum(rowCellNum);
Row row = sheet.getRow(rowCellNum.getRowNum());
if (row == null) {
return R.error(cellNum + "单元格无内容");
return R.ok().setData("");
}
int i = CellReference.convertColStringToIndex(cellStrNum);
Cell cell = row.getCell(i);
Cell cell = row.getCell(rowCellNum.getCellNum());
if (cell == null) {
return R.error(cellNum + "单元格无内容");
return R.ok().setData("");
}
String stringCellVal = POIExcelUtil.getStringCellVal(cell);
if (StrUtil.isEmpty(stringCellVal)) {
return R.error(cellNum + "单元格无内容");
return R.ok().setData("");
}
return R.ok().setData(stringCellVal);
}
public RowCellNum analysisCellNum(String cellNum) {
return null;
}
@RequestMapping("/executeTemplate")
public R executeTemplate(String newCellData) throws IOException {
System.out.println(newCellData);
public void executeTemplate(String newCellData, String cellRowStr, HttpServletResponse response) throws IOException {
String expr = newCellData.substring(newCellData.indexOf("@{"), newCellData.indexOf("}") + 1);
String exprVal = newCellData.substring(newCellData.indexOf("@{") + 2, newCellData.indexOf("}"));
String[] exprArr = exprVal.split("\\.");
@ -172,32 +165,44 @@ public class ExcelController {
.filter(item -> item.getSheetName().equalsIgnoreCase(sheetName))
.findFirst();
if (!first.isPresent()) {
return R.error("未找到sheet页可能是表达式错误");
throw new RuntimeException("未找到sheet页可能是表达式错误");
}
ReadNameVo readNameVo = first.get();
POIExcelUtil.removeModelSheet(this.templateWorkbook, sheetName);
POIExcelUtil.removeModelSheet(this.templateWorkbook, this.templateSheetNum);
POIExcelUtil.batchCloneSheet(this.templateWorkbook, this.templateSheetNum, readNameVo.getNameList().size());
ArrayList<GmhUser> gmhUsers = new ArrayList<>(readNameVo.getNameList());
RowCellNum rowCellNum = POIExcelUtil.analysisCellRowNum(cellRowStr);
for (int i = 0; i < this.templateWorkbook.getNumberOfSheets(); i++) {
Sheet sheet = templateWorkbook.getSheetAt(i);
if (sheet == null) {
continue;
}
Row row = sheet.getRow(2);
Cell cell = row.getCell(0);
cell.setCellValue(newCellData.replace(expr, gmhUsers.get(i).getName()));
Row row = sheet.getRow(rowCellNum.getRowNum());
Cell cell = row.getCell(rowCellNum.getCellNum());
cell.setCellValue(newCellData.replace(expr, CommonUtils.nameFormat(gmhUsers.get(i).getName())));
}
try {
FileOutputStream out = new FileOutputStream("C:\\Users\\admin\\Desktop\\tempfile\\output\\out的222.xls");
out.flush();
templateWorkbook.write(out);
out.close();
System.out.println("文件输出完成");
} catch (IOException e) {
e.printStackTrace();
}
return R.ok();
// TODO 文件保存到本地,文件信息入库
// 输出Excel文件
OutputStream output = response.getOutputStream();
response.reset();
// 设置文件头
response.setHeader("Content-Disposition",
"attchement;filename=" + new String((sheetName + ".xls").getBytes("gb2312"), "ISO8859-1"));
response.setContentType("application/msexcel");
this.templateWorkbook.write(output);
this.templateWorkbook.close();
// try {
// FileOutputStream out = new FileOutputStream("C:\\Users\\admin\\Desktop\\tempfile\\output\\out的222.xls");
// out.flush();
// templateWorkbook.write(out);
// out.close();
// System.out.println("文件输出完成");
// } catch (IOException e) {
// e.printStackTrace();
// }
}
@Autowired

@ -0,0 +1,23 @@
package com.gmh.controller;
import com.gmh.entity.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.List;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionController {
@ExceptionHandler(BindException.class)
public R handleBindException(BindException e) {
log.error(e.getMessage());
return R.error(e.getAllErrors().get(0).getDefaultMessage());
}
}

@ -5,6 +5,8 @@ import com.gmh.entity.R;
import com.gmh.entity.SysBaijiaxing;
import com.gmh.service.GmhUserService;
import com.gmh.service.SysBaijiaxingService;
import com.gmh.service.TestService;
import com.gmh.service.impl.EnTestServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -22,6 +24,12 @@ public class TestController {
private SysBaijiaxingService baijiaxingService;
@Autowired
private TestService enTestServiceImpl;
@Autowired
private TestService haTestServiceImpl;
@RequestMapping("/test")
public R test() {
List<GmhUser> list = gmhUserService.list();

@ -0,0 +1,10 @@
package com.gmh.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gmh.entity.FileEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface FileDao extends BaseMapper<FileEntity> {
}

@ -0,0 +1,58 @@
package com.gmh.entity;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
public abstract class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*/
@TableId(value = "id", type = IdType.ASSIGN_ID)
private String id;
/**
*
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@TableField(value = "create_time", fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
*
*/
@TableField(value = "create_by", fill = FieldFill.INSERT)
private String createBy;
/**
*
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
/**
*
*/
@TableField(value = "update_by", fill = FieldFill.INSERT_UPDATE)
private String updateBy;
/**
* 01
*/
@TableField(value = "status", fill = FieldFill.INSERT)
private String status;
/**
*
*/
@TableField(value = "remark")
private String remark;
}

@ -0,0 +1,53 @@
package com.gmh.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author yuwei
* @since 2019-09-17
*/
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName("tbl_file")
public class FileEntity extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
*
*/
private String originalFilename;
/**
*
*/
private String fileName;
/**
*
*/
private Long fileSize;
/**
* 访
*/
private String filePath;
/**
*
*/
private String contentType;
/**
*
*/
private String fileType;
}

@ -1,11 +1,19 @@
package com.gmh.entity;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.Pattern;
@Data
@Accessors(chain = true)
public class RowCellNum {
private Integer rowNum;
private Integer cellNum;
@Pattern(regexp = "^[A-Za-z]{1,3}[0-9]{1,4}", message = "单元格位置格式不正确")
private String cellRowStr;
}

@ -0,0 +1,12 @@
package com.gmh.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.gmh.entity.FileEntity;
import org.springframework.web.multipart.MultipartFile;
public interface FileService extends IService<FileEntity> {
FileEntity uploadFile(MultipartFile file);
void deleteFileById(String id);
}

@ -0,0 +1,8 @@
package com.gmh.service;
public interface TestService {
String getTest();
String giao(String giao);
}

@ -0,0 +1,18 @@
package com.gmh.service.impl;
import com.gmh.service.TestService;
import org.springframework.stereotype.Service;
@Service
public class EnTestServiceImpl implements TestService {
@Override
public String getTest() {
return "EnTest";
}
@Override
public String giao(String giao) {
return "EnTest Giao :" + giao;
}
}

@ -0,0 +1,18 @@
package com.gmh.service.impl;
import com.gmh.service.TestService;
import org.springframework.stereotype.Service;
@Service
public class HaTestServiceImpl implements TestService {
@Override
public String getTest() {
return "HaTest";
}
@Override
public String giao(String giao) {
return "HaTest Giao :" + giao;
}
}

@ -7,6 +7,7 @@ import com.gmh.dao.SysBaijiaxingDao;
import com.gmh.entity.SysBaijiaxing;
import com.gmh.service.SysBaijiaxingService;
import com.gmh.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -17,6 +18,7 @@ import java.util.Set;
import java.util.stream.Collectors;
@Service
@Slf4j
public class SysBaijiaxingServiceImpl extends ServiceImpl<SysBaijiaxingDao, SysBaijiaxing> implements SysBaijiaxingService, InitializingBean {
@Autowired
@ -40,10 +42,12 @@ public class SysBaijiaxingServiceImpl extends ServiceImpl<SysBaijiaxingDao, SysB
@Override
public void afterPropertiesSet() {
log.info("Initializing baijiaxing cache ....");
List<SysBaijiaxing> list = list();
Set<String> collect = list.stream().map(SysBaijiaxing::getXingshi).collect(Collectors.toSet());
for (String xingshi : collect) {
redisUtils.setCacheObject(KEY_XINGSHI + xingshi, xingshi);
}
log.info("baijiaxing cache loading completed!");
}
}

@ -0,0 +1,14 @@
package com.gmh.utils;
public class CommonUtils {
public static String nameFormat(String name) {
StringBuilder newName = new StringBuilder(name.replaceAll("\\s*", ""));
if (newName.length() == 2) {
newName.insert(1, " ");
return newName.toString();
}
return name;
}
}

@ -2,6 +2,7 @@ package com.gmh.utils;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelFileUtil;
import com.gmh.entity.RowCellNum;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
@ -238,4 +239,25 @@ public class POIExcelUtil {
//设置默认显示第一页
wb.setActiveSheet(0);
}
/**
*
* @param cellRowStr A3
* @return
*/
public static RowCellNum analysisCellRowNum(String cellRowStr) {
return analysisCellRowNum(new RowCellNum().setCellRowStr(cellRowStr));
}
/**
*
* @param rowCellNum
* @return
*/
public static RowCellNum analysisCellRowNum(RowCellNum rowCellNum) {
String cellStrNum = rowCellNum.getCellRowStr().replaceAll("[^a-z^A-Z]", "");
String rowNum = rowCellNum.getCellRowStr().replaceAll("[^0-9]", "");
int cellIndex = CellReference.convertColStringToIndex(cellStrNum);
return rowCellNum.setCellNum(cellIndex).setRowNum(Integer.parseInt(rowNum) - 1);
}
}
Loading…
Cancel
Save