首先,poi老的版本是和新版本是不兼容的,现在poi依赖都到<version>4.1.1</version>了,搜到的博客大部分是前几年的,好多方法基本上都用不了。----》大家注意一下
1.我实现的功能:上图片
月度任务目标,默认有8行。数据少于8行,剩余的行留在哪里。大于8行,for循环递增添加。
(1)大于八行
(2)小于八行
2.前置知识,懂了看代码会舒服很多
1)创建workbook工作薄;
2)创建一个工作表sheet;
3)创建一个行对象row(下标起始值为0);
4)创建一个单元格对象cell(下标起始值为0);
5)给单元格设置内容;
6)设置单元格样式,设置字体和字体大小;
3.上代码
(1)我使用的依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
(2)后端代码
@GetMapping("/export")
public ByteArrayOutputStream exportExcle(String userid, Integer bookId, String timeStart, String timeEnd,
HttpServletResponse response, Integer isNotMe) throws Exception {
response.setCharacterEncoding("UTF-8");
//编码格式为UTF-8
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
List<PerformanceTask> exportTaskList = new ArrayList<>();
if (timeStart != null) {//查询的情况
exportTaskList = performanceTaskBookService.exportSelectTaskList(timeStart, timeEnd, bookId, userid, isNotMe);
} else {//不查询的情况
exportTaskList = performanceTaskBookService.exportNoSelectTaskList(userid, bookId, isNotMe);
}
HSSFWorkbook wb = new HSSFWorkbook();
if (exportTaskList.size() > 0) {
PerformanceTaskBook performanceTaskBook = performanceTaskBookMapper.selectById(exportTaskList.get(0).getPerformanceTaskBookId());
HSSFSheet sheet = wb.createSheet("月绩核表");
//sheet.setColumnWidth(0, 10);
// sheet.setDefaultRowHeight();
sheet.setDefaultRowHeightInPoints(100);
//sheet.setColumnWidth(0, 3766);
//表头字体
Font headerFont = wb.createFont();
headerFont.setFontName("微软雅黑");
headerFont.setFontHeightInPoints((short) 18);
headerFont.setBold(true); //字体加粗
//正文字体
Font contextFont = wb.createFont();
contextFont.setFontHeightInPoints((short) 12);
//表头样式,左右上下居中
CellStyle headerStyle = wb.createCellStyle();
headerStyle.setFont(headerFont);
headerStyle.setAlignment(HorizontalAlignment.CENTER);// 左右居中
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
headerStyle.setLocked(true);
headerStyle.setWrapText(false);// 自动换行
//单元格样式,左右上下居中 边框========第一行
CellStyle commonStyle = wb.createCellStyle();
commonStyle.setFont(contextFont);
commonStyle.setAlignment(HorizontalAlignment.CENTER);// 左右居中
commonStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
// commonStyle.setLocked(true);
commonStyle.setWrapText(true);// 自动换行
commonStyle.setFillForegroundColor(IndexedColors.CORNFLOWER_BLUE.getIndex());
commonStyle.setBorderBottom(BorderStyle.THIN);//下边框
commonStyle.setBorderLeft(BorderStyle.THIN);//左边框
commonStyle.setBorderTop(BorderStyle.THIN);//上边框
commonStyle.setBorderRight(BorderStyle.THIN);//右边框
//******单元格第二行start********
HSSFCellStyle commonStyle2 = wb.createCellStyle();
commonStyle2.setFont(contextFont);
commonStyle2.setAlignment(HorizontalAlignment.CENTER);// 左右居中
commonStyle2.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
commonStyle2.setFillForegroundColor(IndexedColors.SEA_GREEN.index);//设置背景色
commonStyle2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
commonStyle2.setLocked(true);
commonStyle2.setWrapText(true);// 自动换行
commonStyle2.setBorderBottom(BorderStyle.THIN);//下边框
commonStyle2.setBorderLeft(BorderStyle.THIN);//左边框
commonStyle2.setBorderTop(BorderStyle.THIN);//上边框
commonStyle2.setBorderRight(BorderStyle.THIN);//右边框
//******单元格第二行end*********
//******单元格第三行start*********
HSSFCellStyle commonStyle31 = wb.createCellStyle();
commonStyle31.setFont(contextFont);
commonStyle31.setAlignment(HorizontalAlignment.CENTER);// 左右居中
commonStyle31.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
commonStyle31.setLocked(true);
commonStyle31.setWrapText(true);// 自动换行
commonStyle31.setBorderBottom(BorderStyle.THIN);//下边框
commonStyle31.setBorderLeft(BorderStyle.THIN);//左边框
commonStyle31.setBorderTop(BorderStyle.THIN);//上边框
commonStyle31.setBorderRight(BorderStyle.THIN);//右边框
HSSFFont font = wb.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short) 12);//设置字体大小
font.setBold(true);
font.setColor(Font.COLOR_NORMAL);//字体颜色
commonStyle31.setFont(font);//选择需要用到的字体格式
//******单元格第三行end*********
//******单元格倒数第三行start*******
HSSFCellStyle commonStylef3 = wb.createCellStyle();
commonStylef3.setFont(contextFont);
commonStylef3.setAlignment(HorizontalAlignment.CENTER);// 左右居中
commonStylef3.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
commonStylef3.setFillForegroundColor(IndexedColors.PALE_BLUE.index);//设置背景色
commonStylef3.setFillPattern(FillPatternType.SOLID_FOREGROUND);
HSSFFont font4 = wb.createFont();
font4.setFontName("黑体");
font4.setFontHeightInPoints((short) 12);//设置字体大小
font4.setBold(true);
font4.setColor(Font.COLOR_NORMAL);//字体颜色
commonStylef3.setFont(font4);//选择需要用到的字体格式
commonStylef3.setLocked(true);
commonStylef3.setWrapText(true);// 自动换行
commonStylef3.setBorderBottom(BorderStyle.THIN);//下边框
commonStylef3.setBorderLeft(BorderStyle.THIN);//左边框
commonStylef3.setBorderTop(BorderStyle.THIN);//上边框
commonStylef3.setBorderRight(BorderStyle.THIN);//右边框
//******单元格倒数第三行end*********
//******单元格倒数第2行start********
HSSFCellStyle commonStylef2 = wb.createCellStyle();
commonStylef2.setFont(contextFont);
commonStylef2.setAlignment(HorizontalAlignment.CENTER);// 左右居中
commonStylef2.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
commonStylef2.setLocked(true);
commonStylef2.setWrapText(true);// 自动换行
HSSFFont font2 = wb.createFont();
font2.setFontName("黑体");
font2.setFontHeightInPoints((short) 12);//设置字体大小
font2.setBold(true);
font2.setColor(Font.COLOR_NORMAL);//字体颜色
commonStylef2.setFont(font2);//选择需要用到的字体格式
commonStylef2.setBorderBottom(BorderStyle.THIN);//下边框
commonStylef2.setBorderLeft(BorderStyle.THIN);//左边框
commonStylef2.setBorderTop(BorderStyle.THIN);//上边框
commonStylef2.setBorderRight(BorderStyle.THIN);//右边框
//******单元格倒数第2行end*********
//******加粗字体start*********
HSSFCellStyle cu2 = wb.createCellStyle();
cu2.setFont(contextFont);
cu2.setAlignment(HorizontalAlignment.CENTER);// 左右居中
cu2.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
cu2.setLocked(true);
cu2.setWrapText(true);// 自动换行
HSSFFont font3 = wb.createFont();
font3.setFontName("黑体");
font3.setFontHeightInPoints((short) 12);//设置字体大小
font3.setBold(true);
font3.setColor(Font.COLOR_NORMAL);//字体颜色
cu2.setFont(font3);//选择需要用到的字体格式
cu2.setBorderBottom(BorderStyle.THIN);//下边框
cu2.setBorderLeft(BorderStyle.THIN);//左边框
cu2.setBorderTop(BorderStyle.THIN);//上边框
cu2.setBorderRight(BorderStyle.THIN);//右边框
//******加粗字体end*********
//单元格样式,竖向 边框
CellStyle verticalStyle = wb.createCellStyle();
verticalStyle.setFont(contextFont);
verticalStyle.setRotation((short) 255);//竖向
verticalStyle.setLocked(true);
verticalStyle.setWrapText(false);// 自动换行
verticalStyle.setBorderBottom(BorderStyle.THIN);//下边框
verticalStyle.setBorderLeft(BorderStyle.THIN);//左边框
verticalStyle.setBorderTop(BorderStyle.MEDIUM_DASHED);//上边框
verticalStyle.setBorderRight(BorderStyle.THIN);//右边框
/*******************==========插入图片start=============***********************/
FileOutputStream fileOut = null;
RenderedImage bufferImg = null;//图片
//try {
// 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
//将图片读到BufferedImage
String destUrl = "https://pr-mdf-erp.oss-cn-hangzhou.aliyuncs.com/logo/excelLogo.png";
HttpURLConnection httpUrl = (HttpURLConnection) new URL(destUrl).openConnection();
httpUrl.connect();
//File file = FileUtil.inputStreamToFile(httpUrl.getInputStream(),"url.png");
// System.out.println("111====>>>>"+file.getPath());
bufferImg = ImageIO.read(httpUrl.getInputStream());
httpUrl.disconnect();
// bufferImg = ImageIO.read(outputStream);
ImageIO.write(bufferImg, "png", byteArrayOut);// 将图片写入流中
// 利用HSSFPatriarch将图片写入EXCEL
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
//列 行
//图片一导出到单元格B5中
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 1, (short) 0, 0, (short) 2, 1);
// 插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut
.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
/*******************==========插入图片end=============***********************/
int rowNum = 0;
//设置列宽
for (int i = 0; i < 10; i++) {
if (i == 0 || i == 1) {
sheet.setColumnWidth(i, 3000);
} else if (i == 2) {
sheet.setColumnWidth(i, 2600);
} else if (i == 3) {//绩效任务内容
sheet.setColumnWidth(i, 23000);
} else if (i == 4) {//计划时间
sheet.setColumnWidth(i, 6600);
} else if (i == 5) {//完成情况
sheet.setColumnWidth(i, 3600);
} else if (i == 6) {//自评得分
sheet.setColumnWidth(i, 3100);
} else if (i == 7) {//实际完成情况简述
sheet.setColumnWidth(i, 5600);
} else if (i == 8) {//复评得分
sheet.setColumnWidth(i, 3100);
} else {
sheet.setColumnWidth(i, 2000);
}
}
//第一行
Row r0 = sheet.createRow(rowNum++);
r0.setHeight((short) 800);
Cell c00 = r0.createCell(2);
c00.setCellValue(performanceTaskBook.getPerformanceTaskBookName() +
time.substring(0, 4) + "年" + time.substring(5, 7) + "月绩效考核表");
c00.setCellStyle(headerStyle);
//合并单元格
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 2, 9));
// 第二行
Row r1 = sheet.createRow(rowNum++);
r1.setHeight((short) 500);
String[] row_first = {"职能中心", "", "", "岗位名称", "", " 6", "7", "", "", "10"};
for (int i = 0; i < row_first.length; i++) {
Cell tempCell = r1.createCell(i);
// sheet.autoSizeColumn(2, true);
if (i == 0) {//1
tempCell.setCellValue(row_first[i]);
tempCell.setCellStyle(commonStyle2);
} else if (i == 1) {//23
tempCell.setCellStyle(commonStyle2);
tempCell.setCellValue(departmentName);
} else if (i == 3) {
tempCell.setCellValue(row_first[i]);
tempCell.setCellStyle(commonStyle2);
} else if (i == 4) {//456
tempCell.setCellValue((String) kvUser.get("position"));
tempCell.setCellStyle(commonStyle2);
} else if (i == 6) {//7
tempCell.setCellValue("姓名");
tempCell.setCellStyle(commonStyle2);
} else if (i == 7) {//8 9 10
tempCell.setCellValue(performanceTask.getOperator());
tempCell.setCellStyle(commonStyle2);
}
}
//合并
sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 4, 5));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 7, 9));
//第三行
Row r2 = sheet.createRow(rowNum++);
r2.setHeight((short) 700);
String[] row_second = {"区分", "指标类别", "", "绩效任务内容", "计划时间", "完成情况", "自评得分", "实际完成情况简述", "复评得分", "权重"};
for (int j = 0; j < row_second.length; j++) {
Cell tempCell1 = r2.createCell(j);
tempCell1.setCellValue(row_second[j]);
tempCell1.setCellStyle(commonStyle31);
}
// 合并
sheet.addMergedRegion(new CellRangeAddress(2, 2, 1, 2));
int length = 0;
if (exportTaskList.size() > 8) {
length = exportTaskList.size() - 8;
}
//循环每一行
int targetNum = 1;
for (int i = 0; i < 8 + length; i++) {
PerformanceTask p = null;
if (i < exportTaskList.size()) {
p = exportTaskList.get(i);
}
Row tempRow = sheet.createRow(rowNum++);
tempRow.setHeight((short) 500);
// 循环单元格填入数据
for (int j = 0; j < 10; j++) {
String tempValue = null;
Cell tempCell1 = tempRow.createCell(j);
tempCell1.setCellStyle(commonStyle);
if (j == 0) {
if (targetNum == 1) {
tempValue = "B工作绩效考核指标(40%)";
tempCell1.setCellStyle(cu2);
} else {
tempValue = null;
}
}
if (j == 1) {
if (targetNum == 1) {
tempValue = "月度任务指标";
tempCell1.setCellStyle(cu2);
} else {
tempValue = null;
}
}
if (j == 2) {
// 目标
tempValue = "目标" + targetNum;
} else if (j == 3) {
if (p != null) {
// 任务描述--》绩效任务内容
tempValue = p.getTaskDescription();
} else {
tempValue = null;
}
} else if (j == 4) {
if (p != null) {
// 计划时间
tempValue = p.getTaskCycleStart() + "~" + p.getTaskCycleEnd();
} else {
tempValue = null;
}
} else if (j == 5) {
if (p != null) {
// 完成情况
if (p.getTaskState() == 0) {
tempValue = "未读";
} else if (p.getTaskState() == 1) {
tempValue = "执行中";
} else if (p.getTaskState() == 2) {
tempValue = "待审核";
} else if (p.getTaskState() == 3) {
if (p.getCompletedTime() != null) {//仅仅为了过滤掉历史不正确数据。正式使用的时候p.getCompletedTime()不会为空的
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date sd1 = df.parse(p.getTaskCycleEnd());//要求完成时间
Date sd2 = df.parse(p.getCompletedTime());//最后完成时间
if (sd2.before(sd1)) {
tempValue = "已完成";
} else {
tempValue = "逾期完成时间" + p.getCompletedTime();
}
} else {
tempValue = "已完成";
}
}
} else {
tempValue = null;
}
} else if (j == 6) {
// 自我评分
tempValue = null;
} else if (j == 7) {
// 实际完成情况简述
tempValue = null;
} else if (j == 8) {
// 复核评分
tempValue = null;
} else if (j == 9) {
// 权重
tempValue = null;
}
tempCell1.setCellValue(tempValue);
}
targetNum++;
}
int size = 8 + length;
// 合并
sheet.addMergedRegion(new CellRangeAddress(3, size + 3, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(3, size + 2, 1, 1));
//倒数第七行
Row rf7 = sheet.createRow(rowNum++);
rf7.setHeight((short) 700);
String[] row_rf7 = {"", "考评小计(依据年度经营计划及组织发展营运管理需求制定)", "", "", "", "", "", "", "", ""};
for (int j = 0; j < 10; j++) {
Cell tempCellf7 = rf7.createCell(j);
if (j == 1) {
tempCellf7.setCellValue(row_rf7[j]);
tempCellf7.setCellStyle(cu2);
} else {
tempCellf7.setCellValue(row_rf7[j]);
tempCellf7.setCellStyle(commonStyle);
}
}
sheet.addMergedRegion(new CellRangeAddress(size + 3,
size + 3, 1, 3));
//循环每一行
int target = 1;
for (int i = 0; i < 2; i++) {
Row tempRow = sheet.createRow(rowNum++);
tempRow.setHeight((short) 500);
// 循环单元格填入数据
for (int j = 0; j < 10; j++) {
String tempValue = null;
Cell tempCell6 = tempRow.createCell(j);
tempCell6.setCellStyle(commonStyle);
if (j == 0) {
if (target == 1) {
tempValue = "C主管裁量(加减分项)";
tempCell6.setCellStyle(cu2);
} else {
tempValue = null;
}
} else if (j == 1) {
// 目标
tempValue = "目标" + "(" + target + ")";
} else {
tempValue = null;
}
tempCell6.setCellValue(tempValue);
}
target++;
}
//倒数第4行
Row rf4 = sheet.createRow(rowNum++);
rf4.setHeight((short) 700);
// String[] row_rf4 = {"", "考评小计(此项由直属主管在员工绩效评核时综合评定,分值+-5分,评核时必须备注客观评核理由)", "3", "4", "5", "6", "7", "8", "9", "10"};
for (int j = 0; j < 10; j++) {
String tempValue = null;
Cell tempCellrf4 = rf4.createCell(j);
if (j == 1) {
tempCellrf4.setCellValue("考评小计(此项由直属主管在员工绩效评核时综合评定,分值+-5分,评核时必须备注客观评核理由)");
tempCellrf4.setCellStyle(cu2);
} else {
tempCellrf4.setCellValue(tempValue);
tempCellrf4.setCellStyle(commonStyle);
}
//tempCell.setCellStyle(commonWrapStyle);
}
// 合并
sheet.addMergedRegion(new CellRangeAddress(size + 4, size + 6,
0, 0));
sheet.addMergedRegion(new CellRangeAddress(size + 4, size + 4, 1, 2));
sheet.addMergedRegion(new CellRangeAddress(size + 5, size + 5, 1, 2));
sheet.addMergedRegion(new CellRangeAddress(size + 6, size + 6, 1, 3));
//倒数第3行
Row rf3 = sheet.createRow(rowNum++);
rf3.setHeight((short) 700);
String[] row_rf3 = {"岗位绩效考评总分=B+C=", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
for (int j = 0; j < 10; j++) {
String tempValue = null;
Cell tempCellrf3 = rf3.createCell(j);
tempCellrf3.setCellStyle(commonStylef3);
if (j == 0) {
tempCellrf3.setCellValue(row_rf3[j]);
tempCellrf3.setCellStyle(commonStylef3);
} else {
tempCellrf3.setCellValue(tempValue);
}
}
// 合并
sheet.addMergedRegion(new CellRangeAddress(size + 7, size + 7,
0, 3));
//倒数第2行
Row rf2 = sheet.createRow(rowNum++);
rf2.setHeight((short) 700);
String[] row_rf2 = {"被评核人签字/日期", "人资行政中心负责人签字/日期", "总办运营签字/日期", "审批人签字/日期", "5", "6", "7", "8", "9", "10"};
for (int j = 0; j < 10; j++) {
String tempValue = null;
Cell tempCellrf2 = rf2.createCell(j);
if (j == 0) {
tempCellrf2.setCellValue("被评核人签字/日期");
} else if (j == 3) {
tempCellrf2.setCellValue("人资行政中心负责人签字/日期");
} else if (j == 4) {
tempCellrf2.setCellValue("总办运营签字/日期");
} else if (j == 7) {
tempCellrf2.setCellValue("审批人签字/日期");
} else {
tempCellrf2.setCellValue(tempValue);
}
tempCellrf2.setCellStyle(commonStylef2);
}
// 合并
sheet.addMergedRegion(new CellRangeAddress(size + 8, size + 8,
0, 2));
// sheet.addMergedRegion(new CellRangeAddress(exportTaskList.size() + 8, exportTaskList.size() + 8,
// 3, 4));
sheet.addMergedRegion(new CellRangeAddress(size + 8, size + 8,
4, 6));
sheet.addMergedRegion(new CellRangeAddress(size + 8, size + 8,
7, 9));
//倒数第1行
Row rf1 = sheet.createRow(rowNum++);
rf1.setHeight((short) 700);
for (int j = 0; j < 10; j++) {
String tempValue = null;
Cell tempCellrf1 = rf1.createCell(j);
tempCellrf1.setCellValue(tempValue);
tempCellrf1.setCellStyle(commonStyle);
}
sheet.addMergedRegion(new CellRangeAddress(size + 9, size + 9,
0, 2));
sheet.addMergedRegion(new CellRangeAddress(size + 9, size + 9,
3, 6));
sheet.addMergedRegion(new CellRangeAddress(size + 9, size + 9,
7, 9));
}
OutputStream outputStream = response.getOutputStream();
wb.write(outputStream);
return null;
}
(3)设置HttpServletResponse的作用
-->response对象的功能分为四种:
设置响应头信息:addHeader("reFresh", "5;URL=xxx")
发送状态码:sendError(404)
设置响应正文:getWriter().print("xxx")
重定向:sendRedirect("path")
-->response.setContentType("text/html;charset=utf-8"):设置响应类型为html,编码为utf-8,处理相应页面文本显示的乱码
response.setCharacterEncoding("utf-8"):如果响应类型为文本,那么就需要设置文本的编码类型,然后浏览器使用这个编码来解读文本,注意:如果没有设置contentType,那么浏览器会认为contentType为text/html,如果没设置编码,那么默认为IOS-8859-1编码,所以以上两点在使用response返回结果之前必须设置。
参考博客:https://blog.csdn.net/weixin_41547486/article/details/81266712?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control&dist_request_id=&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control
(4)引入logo,坐标
HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2)
dx1 dy1 起始单元格中的x,y坐标.
dx2 dy2 结束单元格中的x,y坐标
col1,row1 指定起始的单元格,下标从0开始
col2,row2 指定结束的单元格 ,下标从0开始
4.总结:
excle设置样式,其实是以单元格为单位的。时间仓促,最近超忙,代码有冗余(实属抱歉)。总结以上内容,欢迎大家一起讨论。。。。