|
|
|
@ -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
|
|
|
|
|