1. 后端
工具类
package cn.arp.icas.oa.smallEngineerChange.util;
import org.apache.poi.hssf.util.AreaReference;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellReference;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Map.Entry;
public class SheetProcessorImpl implements SheetProcessor {
private Workbook workbook;
private int rowIdx = 0;
public SheetProcessorImpl(Workbook workbook) {
super();
this.workbook = workbook;
}
@Override
public void writeEntry(Map<String, String> map) {
for (Entry<String, String> entry : map.entrySet()) {
try {
AreaReference areaReference = new AreaReference(workbook.getName(entry.getKey()).getRefersToFormula());
CellReference cellRef = areaReference.getFirstCell();
CellStyle cellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
if (rowIdx == 0) {
font.setFontName("宋体");
font.setFontHeightInPoints((short) 13);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
cellStyle.setFont(font);
}
font.setFontName("楷体_GB2312");
font.setFontHeightInPoints((short) 13);
cellStyle.setFont(font);
cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // 下边框
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);// 左边框
cellStyle.setBorderTop(CellStyle.BORDER_THIN);// 上边框
cellStyle.setBorderRight(CellStyle.BORDER_THIN);// 右边框
cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // 居中
Sheet sheet = workbook.getSheet(cellRef.getSheetName());
Row row;
if ("startMileage".equals(entry.getKey()) || "endMileage".equals(entry.getKey())) {
row = sheet.getRow(cellRef.getRow() + rowIdx);
if (row == null) {
row = sheet.createRow(cellRef.getRow() + rowIdx);
}
} else {
// if(rowIdx==0){
row = sheet.getRow(cellRef.getRow() + rowIdx);
if (row == null) {
row = sheet.createRow(cellRef.getRow() + rowIdx);
}
// }else{
// row = sheet.getRow(cellRef.getRow() + rowIdx + 1);
// if (row == null) {
// row = sheet.createRow(cellRef.getRow() + rowIdx + 1);
// }
// }
}
Cell cell = row.createCell(cellRef.getCol());
cell.setCellStyle(cellStyle);
cell.setCellValue(entry.getValue());
} catch (NullPointerException e) {
}
}
rowIdx++;
}
@Override
public void write(OutputStream out) throws IOException {
workbook.write(out);
}
}
- 主体代码
@RequestMapping(method = RequestMethod.GET)
public void exportExcel(HttpServletRequest req, HttpServletResponse resp, Param param)
throws ParseException, IOException {
param.setPageSize(10000);
param.setPage(1);
req.setCharacterEncoding("UTF-8");
String path = req.getServletContext().getRealPath("/WEB-INF/classes/SmallEngineerChange.xlsx");
FileInputStream ipst = new FileInputStream(path);
SheetProcessor processor = (SheetProcessor) TemplateProcessorFactory.getSheetProcessorFromStream(ipst);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
String corpOrgId = req.getParameter("corpOrgId");
Dept dept = deptApi.findById(corpOrgId);
String fileName = dept.getDeptName() + "零星工程施工变更统计";
Map<String, String> map = new HashMap<String, String>();
map.put("totalName", fileName);
processor.writeEntry(map);
PageRequest pageable = createPageParam(param);
Page<SmallEngineerChange> resultPage = service.findAll(corpOrgId, param, pageable);
List<SmallEngineerChange> resultList = resultPage.getContent();
for (SmallEngineerChange secReq : resultList) {
Map<String, String> entry = new HashMap<String, String>();
// 申请人
if (secReq.getCreaterName() != null) {
entry.put("createrName", secReq.getCreaterName());
} else {
entry.put("createrName", "");
}
// 创建时间
if (secReq.getCreateTime() != null) {
entry.put("createTime", sf.format(secReq.getCreateTime()));
} else {
entry.put("createTime", "");
}
// 申请部门
if (secReq.getDeptName() != null) {
entry.put("deptName", secReq.getDeptName());
} else {
entry.put("deptName", "");
}
// 工程名称
if (secReq.getProjectName() != null) {
entry.put("projectName", secReq.getProjectName());
} else {
entry.put("projectName", "");
}
// 施工单位
if (secReq.getConstructionUnit() != null) {
entry.put("constructionUnit", secReq.getConstructionUnit());
} else {
entry.put("constructionUnit", "");
}
//变更事由
if (secReq.getChangeReason() != null) {
entry.put("changeReason", secReq.getChangeReason());
} else {
entry.put("changeReason", "");
}
//变更内容及要求
if (secReq.getChangeContent() != null) {
entry.put("changeContent", secReq.getChangeContent());
} else {
entry.put("changeContent", "");
}
//变更前费用
if (secReq.getMoneyBefore() != 0.0) {
entry.put("moneyBefore", secReq.getMoneyBefore() + "");
} else {
entry.put("moneyBefore", "");
}
//变更后费用
if (secReq.getMoneyAfter() != 0.0) {
entry.put("moneyAfter", secReq.getMoneyAfter() + "");
} else {
entry.put("moneyAfter", "");
}
processor.writeEntry(entry);
}
resp.setContentType("application/octet-stream");
resp.setHeader("Content-Disposition",
"attachment; filename=" + java.net.URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
processor.write(resp.getOutputStream());
}
- 前端代码
(1)
// 导出
download () {
window.open(`/oa/resourceService/exportrecord?
&statusStr=approval&orgId=${this.allApplyInfo.orgId}&projectName=${encodeURIComponent(this.allApplyInfo.platType)}
&functionDescription=${encodeURIComponent(this.allApplyInfo.functionDescription)}&resourceNumber=${encodeURIComponent(this.allApplyInfo.resourceNumber)}
&resourceAllocation=${encodeURIComponent(this.allApplyInfo.resourceAllocation)}&timeRequierments=${encodeURIComponent(this.allApplyInfo.timeRequierments)}&applyProjectName=${encodeURIComponent(this.allApplyInfo.applyProjectName)}
&costTotal=${encodeURIComponent(this.allApplyInfo.costTotal)}
&corpOrgId=${this.corpOrgId}`, '_self')
},