Browse Source

学习园地 - 导出签名表时加签名照片

Wang Zi Wen 1 year ago
parent
commit
fa53ee9597

+ 125 - 8
ruoyi-admin/src/main/java/com/ruoyi/web/controller/branch/TBranchLearningController.java

@@ -1,16 +1,30 @@
 package com.ruoyi.web.controller.branch;
 
+import java.io.IOException;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
+import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.List;
+import java.util.Objects;
 import javax.servlet.http.HttpServletResponse;
 
+import com.ruoyi.branch.service.ITFileService;
 import com.ruoyi.common.annotation.RepeatSubmit;
 import com.ruoyi.common.core.domain.entity.SysDictData;
+import com.ruoyi.common.core.domain.entity.SysUser;
+import com.ruoyi.common.core.domain.model.LoginUser;
 import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.file.ExcelUtils;
 import com.ruoyi.system.service.ISysDictTypeService;
+import com.ruoyi.system.service.ISysUserService;
 import com.ruoyi.web.controller.branch.vo.AddPlanVO;
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
+import org.apache.poi.hssf.usermodel.HSSFFont;
+import org.apache.poi.hssf.util.HSSFColor;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.xssf.usermodel.*;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -46,6 +60,9 @@ public class TBranchLearningController extends BaseController
     @Autowired
     private ISysDictTypeService sysDictTypeService;
 
+    @Autowired
+    private ISysUserService userService;
+
     @PutMapping("/updateLearningTimeStudied")
     public AjaxResult updateLearningTimeStudied(@RequestBody TBranchLearning tBranchLearning)
     {
@@ -131,17 +148,117 @@ public class TBranchLearningController extends BaseController
     @PostMapping("/export")
     public void export(HttpServletResponse response, TBranchLearning tBranchLearning)
     {
-        List<SysDictData> learningStatus = sysDictTypeService.selectDictDataByType("learning_status");
-        List<TBranchLearning> list = tBranchLearningService.selectTBranchLearningList(tBranchLearning);
-        for (TBranchLearning branchLearning : list) {
-            for (SysDictData data : learningStatus) {
-                if (data.getDictValue().equals(branchLearning.getLearningStatus())) {
-                    branchLearning.setLearningStatus(data.getDictLabel());
+        try {
+            List<SysDictData> learningStatus = sysDictTypeService.selectDictDataByType("learning_status");
+            List<TBranchLearning> list = tBranchLearningService.selectTBranchLearningList(tBranchLearning);
+            for (TBranchLearning branchLearning : list) {
+                for (SysDictData data : learningStatus) {
+                    if (data.getDictValue().equals(branchLearning.getLearningStatus())) {
+                        branchLearning.setLearningStatus(data.getDictLabel());
+                    }
+                }
+            }
+            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+            response.setCharacterEncoding("utf-8");
+            XSSFWorkbook workbook = new XSSFWorkbook();
+            // 创建工作簿
+            XSSFSheet sheet = workbook.createSheet();
+            // 标题样式
+            CellStyle headerStyle = workbook.createCellStyle();
+            headerStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.GREY_50_PERCENT.getIndex());//背景颜色
+            headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//背景颜色填充
+            headerStyle.setAlignment(HorizontalAlignment.CENTER);//居中
+            XSSFFont font = workbook.createFont();//字体
+            font.setBold(true);//粗体显示
+            font.setColor(IndexedColors.WHITE.getIndex());//字体颜色
+            headerStyle.setFont(font);
+            // 单元格样式
+            CellStyle cellStyle = workbook.createCellStyle();
+            cellStyle.setAlignment(HorizontalAlignment.CENTER);//居中
+            // 设置列宽
+            sheet.setColumnWidth(0, 2000);
+            sheet.setColumnWidth(1, 10000);
+            sheet.setColumnWidth(2, 2000);
+            sheet.setColumnWidth(3, 6000);
+            sheet.setColumnWidth(4, 6000);
+            sheet.setColumnWidth(5, 4000);
+            sheet.setColumnWidth(6, 2000);
+            sheet.setColumnWidth(7, 2000);
+            // 表头
+            XSSFRow row0 = sheet.createRow(0);
+            XSSFCell cell0 = row0.createCell(0);
+            cell0.setCellValue("姓名");
+            cell0.setCellStyle(headerStyle);
+            XSSFCell cell1 = row0.createCell(1);
+            cell1.setCellValue("课件名称");
+            cell1.setCellStyle(headerStyle);
+            XSSFCell cell2 = row0.createCell(2);
+            cell2.setCellValue("状态");
+            cell2.setCellStyle(headerStyle);
+            XSSFCell cell3 = row0.createCell(3);
+            cell3.setCellValue("规定学习时长(min)");
+            cell3.setCellStyle(headerStyle);
+            XSSFCell cell4 = row0.createCell(4);
+            cell4.setCellValue("累计学习时长(min)");
+            cell4.setCellStyle(headerStyle);
+            XSSFCell cell5 = row0.createCell(5);
+            cell5.setCellValue("截止日期");
+            cell5.setCellStyle(headerStyle);
+            XSSFCell cell6 = row0.createCell(6);
+            cell6.setCellValue("进度");
+            cell6.setCellStyle(headerStyle);
+            XSSFCell cell7 = row0.createCell(7);
+            cell7.setCellValue("签名");
+            cell7.setCellStyle(headerStyle);
+            // 创建行
+            for (int i = 1; i < list.size() + 1; i++) {
+                XSSFRow row = sheet.createRow(i);
+                // 创建单元格
+                for (int j = 0; j < 8; j++) {
+                    XSSFCell cell = row.createCell(j);
+                    cell.setCellStyle(cellStyle);
+                    switch (j) {
+                        case 0:
+                            cell.setCellValue(list.get(i - 1).getNickName());
+                            break;
+                        case 1:
+                            cell.setCellValue(list.get(i - 1).getFileName());
+                            break;
+                        case 2:
+                            cell.setCellValue(list.get(i - 1).getLearningStatus());
+                            break;
+                        case 3:
+                            cell.setCellValue(list.get(i - 1).getLearningTimeRequired());
+                            break;
+                        case 4:
+                            cell.setCellValue(list.get(i - 1).getLearningTimeStudied());
+                            break;
+                        case 5:
+                            Date learningDeadline = list.get(i - 1).getLearningDeadline();
+                            cell.setCellValue(learningDeadline != null ? new SimpleDateFormat("yyyy-MM-dd").format(learningDeadline) : "");
+                            break;
+                        case 6:
+                            cell.setCellValue(list.get(i - 1).getLearningProgress());
+                            break;
+                        case 7:
+                            String isSigned = list.get(i - 1).getIsSigned();
+                            if (isSigned.equals("1")) {
+                                SysUser user = userService.selectUserById(list.get(i - 1).getUserId());
+                                String signUrl = user.getSignUrl();
+                                System.err.println(signUrl);
+                                if (StringUtils.isNotEmpty(signUrl))
+                                    ExcelUtils.insertPicture(workbook, sheet, signUrl, i, 7, 1, 1);
+                            }
+                            break;
+                    }
                 }
             }
+            //返回
+            workbook.write(response.getOutputStream());
+            workbook.close();
+        } catch (IOException e) {
+            throw new RuntimeException(e);
         }
-        ExcelUtil<TBranchLearning> util = new ExcelUtil<TBranchLearning>(TBranchLearning.class);
-        util.exportExcel(response, list, "签名表数据");
     }
 
     /**

+ 32 - 4
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/ExcelUtils.java

@@ -1,18 +1,20 @@
 package com.ruoyi.common.utils.file;
 
 
+import com.ruoyi.common.config.RuoYiConfig;
+import com.ruoyi.common.constant.Constants;
+import com.ruoyi.common.utils.StringUtils;
 import org.apache.poi.hssf.usermodel.HSSFDataFormat;
 import org.apache.poi.hssf.usermodel.HSSFDateUtil;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.math.BigDecimal;
 import java.text.DecimalFormat;
 import java.text.SimpleDateFormat;
@@ -25,6 +27,32 @@ import java.util.List;
  */
 public class ExcelUtils {
 
+    public static void insertPicture(XSSFWorkbook workbook, XSSFSheet sheet, String fileUrl, int row, int col, double scaleX, double scaleY) {
+        try {
+            // 输入流
+            String localPath = RuoYiConfig.getProfile();
+            String imagePath = localPath + StringUtils.substringAfter(fileUrl, Constants.RESOURCE_PREFIX);
+            InputStream is = new FileInputStream(imagePath);
+            byte[] bytes = IOUtils.toByteArray(is);
+            @SuppressWarnings("static-access")
+            int pictureIdx = workbook.addPicture(bytes, workbook.PICTURE_TYPE_PNG);
+            CreationHelper helper = workbook.getCreationHelper();
+            Drawing drawing = sheet.createDrawingPatriarch();
+            ClientAnchor anchor = helper.createClientAnchor();
+            // 图片插入坐标
+            anchor.setCol1(col);
+            anchor.setRow1(row);
+            // 插入图片
+            Picture pict = drawing.createPicture(anchor, pictureIdx);
+            pict.resize(scaleX, scaleY);
+            is.close();
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
     public static Workbook getWorkBook(MultipartFile file) {
         //获得文件名
         String fileName = file.getOriginalFilename();

+ 38 - 2
ruoyi-system/src/main/resources/mapper/system/SysOperLogMapper.xml

@@ -30,8 +30,44 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     </sql>
     
 	<insert id="insertOperlog" parameterType="SysOperLog">
-		insert into sys_oper_log(title, business_type, method, request_method, operator_type, oper_name, dept_name, oper_url, oper_ip, oper_location, oper_param, json_result, status, error_msg, cost_time, oper_time)
-        values (#{title}, #{businessType}, #{method}, #{requestMethod}, #{operatorType}, #{operName}, #{deptName}, #{operUrl}, #{operIp}, #{operLocation}, #{operParam}, #{jsonResult}, #{status}, #{errorMsg}, #{costTime}, sysdate)
+		<selectKey keyProperty="operId" resultType="long" order="BEFORE">
+			SELECT seq_sys_oper_log.NEXTVAL as operId FROM DUAL
+		</selectKey>
+		insert into sys_oper_log(
+		<if test="operId != null and operId != 0">oper_id,</if>
+		<if test="title != null and title != ''">title,</if>
+		<if test="businessType != null and businessType != ''">business_type,</if>
+		<if test="method != null and method != ''">method,</if>
+		<if test="requestMethod != null and requestMethod != ''">request_method,</if>
+		<if test="operatorType != null and operatorType != ''">operator_type,</if>
+		<if test="operName != null and operName != ''">oper_name,</if>
+		<if test="deptName != null and deptName != ''">dept_name,</if>
+		<if test="operUrl != null and operUrl != ''">oper_url,</if>
+		<if test="operIp != null and operIp != ''">oper_ip,</if>
+		<if test="operLocation != null and operLocation != ''">oper_location,</if>
+		<if test="operParam != null and operParam != ''">oper_param,</if>
+		<if test="jsonResult != null and jsonResult != ''">json_result,</if>
+		<if test="status != null and status != ''">status,</if>
+		<if test="errorMsg != null and errorMsg != ''">error_msg,</if>
+		oper_time
+		)values(
+		<if test="operId != null and operId != 0">#{operId},</if>
+		<if test="title != null and title != ''">#{title},</if>
+		<if test="businessType != null and businessType != ''">#{businessType},</if>
+		<if test="method != null and method != ''">#{method},</if>
+		<if test="requestMethod != null and requestMethod != ''">#{requestMethod},</if>
+		<if test="operatorType != null and operatorType != ''">#{operatorType},</if>
+		<if test="operName != null and operName != ''">#{operName},</if>
+		<if test="deptName != null and deptName != ''">#{deptName},</if>
+		<if test="operUrl != null and operUrl != ''">#{operUrl},</if>
+		<if test="operIp != null and operIp != ''">#{operIp},</if>
+		<if test="operLocation != null and operLocation != ''">#{operLocation},</if>
+		<if test="operParam != null and operParam != ''">#{operParam},</if>
+		<if test="jsonResult != null and jsonResult != ''">#{jsonResult},</if>
+		<if test="status != null and status != ''">#{status},</if>
+		<if test="errorMsg != null and errorMsg != ''">#{errorMsg},</if>
+		sysdate
+		)
 	</insert>
 	
 	<select id="selectOperLogList" parameterType="SysOperLog" resultMap="SysOperLogResult">