org.apache.poi 3.9依赖 普通导出execl
<!-- POI导入导出3.9版本 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
@PostMapping("/exportExcel")
@ApiOperation("导出监控数据")
public AjaxResult exportExcel(HttpServletResponse response, @RequestBody MonitorVO vo) throws Exception {
try {
cudWbProcessInfoTService.exportExcel(response, vo);
} catch (Exception e) {
e.getStackTrace();
return error("导出失败");
}
return success("导出成功");
}
service 依赖
import com.cgnpc.cqs.constant.enumeration.EnumEnterpriseCategory;
import com.cgnpc.cqs.constant.enumeration.EnumTransferOperationType;
import com.cgnpc.cqs.monitor.domain.CudWbProcessInfoT;
import com.cgnpc.cqs.monitor.mapper.CudWbProcessInfoTMapper;
import com.cgnpc.cqs.monitor.service.ICudWbProcessInfoTService;
import com.cgnpc.cqs.monitor.vo.MonitorVO;
import com.cgnpc.cqs.utils.EasyExcelUtil;
import com.cgnpc.cqs.wchz.model.SubEnterpriseAllInfoVO;
import com.cgnpc.cud.core.service.BaseServiceImpl;
import com.cgnpc.cud.utils.text.StringUtil;
import com.cgnpc.cuddemo.httpclient.application.exception.BusinessException;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.framework.utils.HttpServlet;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
@Override
public void exportExcel(HttpServletResponse response, MonitorVO vo) throws Exception {
List<MonitorVO> list = baseMapper.list(vo);
String fileTitle = "业务监控数据";
String[] headers = {"任务标题", "流程类型", "发起人", "当前环节", "发起时间", "完成时间"};
Workbook workbook = initExcelWorkbook(fileTitle, fileTitle, headers);
Sheet sheet = workbook.getSheetAt(0);
CellStyle rightStyle = workbook.createCellStyle();
rightStyle.setAlignment(CellStyle.ALIGN_RIGHT);
CellStyle leftStyle = workbook.createCellStyle();
leftStyle.setAlignment(CellStyle.ALIGN_LEFT);
CellStyle centerStyle = workbook.createCellStyle();
centerStyle.setAlignment(CellStyle.ALIGN_CENTER);
for (int i = 0; i < list.size(); i++) {
MonitorVO exportVo = list.get(i);
Row row = sheet.createRow(i + 1);
Cell babmCell = row.createCell(0);
babmCell.setCellValue(exportVo.getProcTitle() == null ? "" : exportVo.getProcTitle());
babmCell.setCellStyle(centerStyle);
Cell pgdxCell = row.createCell(1);
pgdxCell.setCellValue(exportVo.getProcName() == null ? "" : exportVo.getProcName());
pgdxCell.setCellStyle(centerStyle);
Cell barqCell = row.createCell(2);
barqCell.setCellValue(exportVo.getStartUser() == null ? "" : exportVo.getStartUser());
barqCell.setCellStyle(centerStyle);
Cell jjxwlxCell = row.createCell(3);
jjxwlxCell.setCellValue(exportVo.getActName() == null ? "" : exportVo.getActName());
jjxwlxCell.setCellStyle(centerStyle);
Cell zcpgwtfCell = row.createCell(4);
zcpgwtfCell.setCellValue(exportVo.getStartTime() == null ? "" : exportVo.getStartTime());
zcpgwtfCell.setCellStyle(centerStyle);
Cell pgjzrCell = row.createCell(5);
pgjzrCell.setCellValue(exportVo.getEndTime() == null ? "" : exportVo.getEndTime());
pgjzrCell.setCellStyle(centerStyle);
}
exportWorkbook(workbook, fileTitle);
}
private Workbook initExcelWorkbook(String sheetName, String tableTitle, String[] tableHeaders){
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet(sheetName);
for (int i = 0; i < tableHeaders.length; i++) {
if(i == 0){
sheet.setColumnWidth(i, 20*500);
}else {
sheet.setColumnWidth(i, 20*300);
}
}
Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 16);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setFont(font);
Row headerRow = sheet.createRow(0);
Font font2 = workbook.createFont();
font2.setFontName("宋体");
font2.setColor(IndexedColors.WHITE.getIndex());
font2.setFontHeightInPoints((short) 12);
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
CellStyle style2 = workbook.createCellStyle();
style2.setAlignment(CellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style2.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
style2.setFillPattern(CellStyle.SOLID_FOREGROUND);
style2.setFont(font2);
for (int i = 0; i < tableHeaders.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(tableHeaders[i]);
cell.setCellStyle(style2);
}
return workbook;
}
private void exportWorkbook(Workbook workbook, String fileName){
OutputStream os = null;
try {
String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String exportName = fileName + date + ".xlsx";
HttpServletResponse response = HttpServlet.getResponse();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(exportName, "UTF-8"));
os = response.getOutputStream();
workbook.write(os);
System.out.println("导出成功!");
} catch (IOException e) {
log.error("导出失败");
e.printStackTrace();
} finally {
if (os != null){
try {
os.flush();
os.close();
} catch (IOException e) {
log.error("输出流异常!");
throw new RuntimeException(e);
}
}
}
}