文件下载
This commit is contained in:
		
							parent
							
								
									d51b387712
								
							
						
					
					
						commit
						cebef1294d
					
				
							
								
								
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@ -48,4 +48,6 @@ gen
 | 
			
		||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
 | 
			
		||||
hs_err_pid*
 | 
			
		||||
 | 
			
		||||
/upload/**
 | 
			
		||||
/upload/**
 | 
			
		||||
 | 
			
		||||
*.doc*
 | 
			
		||||
							
								
								
									
										22
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								pom.xml
									
									
									
									
									
								
							@ -78,6 +78,28 @@
 | 
			
		||||
            <artifactId>spring-boot-starter-log4j2</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
 | 
			
		||||
        <dependency>
 | 
			
		||||
			<groupId>fr.opensagres.xdocreport</groupId>
 | 
			
		||||
			<artifactId>fr.opensagres.poi.xwpf.converter.core</artifactId>
 | 
			
		||||
			<version>2.0.2</version>
 | 
			
		||||
		</dependency>
 | 
			
		||||
        <dependency>
 | 
			
		||||
			<groupId>commons-io</groupId>
 | 
			
		||||
			<artifactId>commons-io</artifactId>
 | 
			
		||||
			<version>2.8.0</version>
 | 
			
		||||
		</dependency>
 | 
			
		||||
		<dependency>
 | 
			
		||||
			<groupId>fr.opensagres.xdocreport</groupId>
 | 
			
		||||
			<artifactId>fr.opensagres.xdocreport.itext.extension</artifactId>
 | 
			
		||||
			<version>2.0.2</version>
 | 
			
		||||
		</dependency>
 | 
			
		||||
		<dependency>
 | 
			
		||||
			<groupId>fr.opensagres.xdocreport</groupId>
 | 
			
		||||
			<artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId>
 | 
			
		||||
			<version>2.0.2</version>
 | 
			
		||||
		</dependency>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>org.springframework.boot</groupId>
 | 
			
		||||
            <artifactId>spring-boot-starter-aop</artifactId>
 | 
			
		||||
 | 
			
		||||
@ -76,9 +76,7 @@ public class AuthFilterConfig implements Filter {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        String token = request.getHeader("Authorization");
 | 
			
		||||
        if (uri.startsWith("/record/record2word")) {
 | 
			
		||||
            token = request.getParameter("token");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (token == null) {
 | 
			
		||||
            log.error("请求无token");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,7 @@ package com.example.survey.controller;
 | 
			
		||||
 | 
			
		||||
import com.example.survey.dto.project.AddRecordDTO;
 | 
			
		||||
import com.example.survey.dto.project.CreateProjectDTO;
 | 
			
		||||
import com.example.survey.dto.project.DeleteProjectDTO;
 | 
			
		||||
import com.example.survey.dto.project.DeleteRecordDTO;
 | 
			
		||||
import com.example.survey.dto.project.ModifyProjectDTO;
 | 
			
		||||
 | 
			
		||||
@ -64,6 +65,13 @@ public class ProjectController {
 | 
			
		||||
        return new ResultVO(ResultEnum.SUCCESS);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    @DeleteMapping("/project")
 | 
			
		||||
    public ResultVO deleteProject(@RequestBody DeleteProjectDTO deleteProjectDTO){
 | 
			
		||||
        projectService.deleteProject(deleteProjectDTO);
 | 
			
		||||
        return new ResultVO(ResultEnum.SUCCESS);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @DeleteMapping("/record")
 | 
			
		||||
    public ResultVO deleteRecord(@RequestBody DeleteRecordDTO deleteRecordDTO){
 | 
			
		||||
        projectService.deleteRecord(deleteRecordDTO);
 | 
			
		||||
 | 
			
		||||
@ -97,8 +97,16 @@ public class RecordController {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @GetMapping("/record2word")
 | 
			
		||||
    public void record2word(@RequestParam("uuid") String uuid, HttpServletResponse response) {
 | 
			
		||||
        recordService.record2word(uuid, response);
 | 
			
		||||
    public ResultVO record2word(@RequestParam("uuid") String uuid) {
 | 
			
		||||
        recordService.record2word(uuid);
 | 
			
		||||
        ResultVO resultVO = new ResultVO(ResultEnum.SUCCESS);
 | 
			
		||||
        return resultVO;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @GetMapping("/report")
 | 
			
		||||
    public void report(@RequestParam("uuid") String uuid, HttpServletResponse response) {
 | 
			
		||||
        // recordService.record2word(uuid, response);
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @PutMapping("/metaData")
 | 
			
		||||
 | 
			
		||||
@ -55,4 +55,6 @@ public interface ProjectDao {
 | 
			
		||||
    long countProject(String name, long date_gt, long date_lt);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    void deleteProject(String name);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -84,4 +84,12 @@ public class ProjectDaoImpl implements ProjectDao {
 | 
			
		||||
        return mongoTemplate.count(query, Project.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void deleteProject(String name) {
 | 
			
		||||
        Query query = new Query();
 | 
			
		||||
        query.addCriteria(Criteria.where("name").is(name));
 | 
			
		||||
        mongoTemplate.remove(query, Project.class);
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,12 @@
 | 
			
		||||
package com.example.survey.dto.project;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author Pope
 | 
			
		||||
 */
 | 
			
		||||
@Data
 | 
			
		||||
public class DeleteProjectDTO {
 | 
			
		||||
    private String name;
 | 
			
		||||
}
 | 
			
		||||
@ -20,6 +20,10 @@ public enum AuthEnum {
 | 
			
		||||
        add("/project/projectState : PUT");
 | 
			
		||||
        add("/project/project : PUT");
 | 
			
		||||
        add("/project/project : POST");
 | 
			
		||||
        add("/project/project : DELETE");
 | 
			
		||||
        add("/project/record : DELETE");
 | 
			
		||||
        add("/project/record : PUT");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        add("/respondent/respondent : POST");
 | 
			
		||||
        add("/respondent/respondent : PUT");
 | 
			
		||||
@ -33,8 +37,10 @@ public enum AuthEnum {
 | 
			
		||||
        add("/record/record : PUT");
 | 
			
		||||
        add("/record/record : DELETE");
 | 
			
		||||
        add("/record/review : PUT");
 | 
			
		||||
        add("/record/metaData : PUT");
 | 
			
		||||
        add("/record/file : POST");
 | 
			
		||||
 | 
			
		||||
        add("/record/record2word : GET");
 | 
			
		||||
    
 | 
			
		||||
        add("/user/userList : GET");
 | 
			
		||||
        add("/user/user : POST");
 | 
			
		||||
        add("/user/user : DELETE");
 | 
			
		||||
@ -65,15 +71,23 @@ public enum AuthEnum {
 | 
			
		||||
    QUERY_PROJECT("查询项目的权限", new HashSet<String>() {{
 | 
			
		||||
        add("/project/projectList : GET");
 | 
			
		||||
        add("/project/respondentCount : GET");
 | 
			
		||||
        add("/record/record2word : GET");
 | 
			
		||||
 | 
			
		||||
    }}),
 | 
			
		||||
    MODIFY_PROJECT("修改项目的权限", new HashSet<String>() {{
 | 
			
		||||
        add("/project/projectState : PUT");
 | 
			
		||||
        add("/project/project : PUT");
 | 
			
		||||
 | 
			
		||||
        add("/project/record : DELETE");
 | 
			
		||||
        add("/project/record : PUT");
 | 
			
		||||
    }}),
 | 
			
		||||
    CREATE_PROJECT("创建项目的权限", new HashSet<String>() {{
 | 
			
		||||
        add("/project/project : POST");
 | 
			
		||||
    }}),
 | 
			
		||||
    DELETE_PROJECT("删除项目的权限", new HashSet<String>() {{
 | 
			
		||||
        add("/project/project : DELETE");
 | 
			
		||||
    }}),
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 调查对象
 | 
			
		||||
@ -107,6 +121,9 @@ public enum AuthEnum {
 | 
			
		||||
    MODIFY_RECORD("修改流调记录的权限", new HashSet<String>() {{
 | 
			
		||||
        add("/record/record : PUT");
 | 
			
		||||
    }}),
 | 
			
		||||
    RECORD_METADATA("修改流调记录元数据的权限", new HashSet<String>() {{
 | 
			
		||||
        add("/record/metaData : PUT");
 | 
			
		||||
    }}),
 | 
			
		||||
    DELETE_RECORD("删除流调记录的权限", new HashSet<String>() {{
 | 
			
		||||
        add("/record/record : DELETE");
 | 
			
		||||
    }}),
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,7 @@ package com.example.survey.service;
 | 
			
		||||
 | 
			
		||||
import com.example.survey.dto.project.AddRecordDTO;
 | 
			
		||||
import com.example.survey.dto.project.CreateProjectDTO;
 | 
			
		||||
import com.example.survey.dto.project.DeleteProjectDTO;
 | 
			
		||||
import com.example.survey.dto.project.DeleteRecordDTO;
 | 
			
		||||
import com.example.survey.dto.project.ModifyProjectDTO;
 | 
			
		||||
 | 
			
		||||
@ -56,6 +57,7 @@ public interface ProjectService {
 | 
			
		||||
     */
 | 
			
		||||
    long countProject(String name, long date_gt, long date_lt);
 | 
			
		||||
 | 
			
		||||
    void deleteProject(DeleteProjectDTO projectDTO);
 | 
			
		||||
 | 
			
		||||
    void addRecord(AddRecordDTO addRecordDTO);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -99,9 +99,11 @@ public interface RecordService {
 | 
			
		||||
     * @param projectName 项目名
 | 
			
		||||
     * @param response 响应
 | 
			
		||||
     */
 | 
			
		||||
    void record2word(String idNumber, HttpServletResponse response);
 | 
			
		||||
    boolean record2word(String idNumber);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    void report(String uuid, HttpServletResponse response);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 修改流调记录所绑定元数据
 | 
			
		||||
     *
 | 
			
		||||
 | 
			
		||||
@ -6,6 +6,7 @@ import com.example.survey.dao.RecordDao;
 | 
			
		||||
import com.example.survey.dao.UserDao;
 | 
			
		||||
import com.example.survey.dto.project.AddRecordDTO;
 | 
			
		||||
import com.example.survey.dto.project.CreateProjectDTO;
 | 
			
		||||
import com.example.survey.dto.project.DeleteProjectDTO;
 | 
			
		||||
import com.example.survey.dto.project.DeleteRecordDTO;
 | 
			
		||||
import com.example.survey.dto.project.ModifyProjectDTO;
 | 
			
		||||
 | 
			
		||||
@ -13,6 +14,7 @@ import com.example.survey.entity.MetaData;
 | 
			
		||||
import com.example.survey.entity.Project;
 | 
			
		||||
import com.example.survey.entity.Record;
 | 
			
		||||
import com.example.survey.entity.User;
 | 
			
		||||
import com.example.survey.enumeration.MetaDataTypeEnum;
 | 
			
		||||
import com.example.survey.enumeration.ProjectStateEnum;
 | 
			
		||||
import com.example.survey.enumeration.ResultEnum;
 | 
			
		||||
import com.example.survey.exception.MetaDataException;
 | 
			
		||||
@ -127,7 +129,10 @@ public class ProjectServiceImpl implements ProjectService {
 | 
			
		||||
            t.add(project.getName());
 | 
			
		||||
            record.setProjectList(t);
 | 
			
		||||
            recordDao.saveRecord(record);
 | 
			
		||||
            project.setCount(project.getCount() + 1);
 | 
			
		||||
            if(record.getMetaDataType().equals(MetaDataTypeEnum.RECORD_TEMP.getValue())) {
 | 
			
		||||
                project.setCount(project.getCount() + 1);
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            projectDao.saveProject(project);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@ -156,11 +161,23 @@ public class ProjectServiceImpl implements ProjectService {
 | 
			
		||||
            t.remove(project.getName());
 | 
			
		||||
            record.setProjectList(t);
 | 
			
		||||
            recordDao.saveRecord(record);
 | 
			
		||||
            project.setCount(project.getCount() - 1);
 | 
			
		||||
            if(record.getMetaDataType().equals(MetaDataTypeEnum.RECORD_TEMP.getValue())) {
 | 
			
		||||
                project.setCount(project.getCount() - 1);
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            projectDao.saveProject(project);
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void deleteProject(DeleteProjectDTO projectDTO) {
 | 
			
		||||
        if(!projectDao.existProject(projectDTO.getName()))
 | 
			
		||||
        {
 | 
			
		||||
            throw new ProjectException(ResultEnum.NOT_EXIST_PROJECT);
 | 
			
		||||
        }
 | 
			
		||||
        projectDao.deleteProject(projectDTO.getName());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -4,11 +4,13 @@ import com.example.survey.dao.*;
 | 
			
		||||
import com.example.survey.dto.record.*;
 | 
			
		||||
import com.example.survey.entity.*;
 | 
			
		||||
import com.example.survey.entity.inner.Operation;
 | 
			
		||||
import com.example.survey.enumeration.MetaDataTypeEnum;
 | 
			
		||||
import com.example.survey.enumeration.RecordStateEnum;
 | 
			
		||||
 | 
			
		||||
import com.example.survey.enumeration.ResultEnum;
 | 
			
		||||
import com.example.survey.exception.*;
 | 
			
		||||
import com.example.survey.service.RecordService;
 | 
			
		||||
import com.example.survey.util.ThreadWordUtil;
 | 
			
		||||
import com.example.survey.util.WordUtil;
 | 
			
		||||
import com.example.survey.vo.RecordDetailVO;
 | 
			
		||||
import com.example.survey.vo.RecordVO;
 | 
			
		||||
@ -52,6 +54,9 @@ public class RecordServiceImpl implements RecordService {
 | 
			
		||||
    @Value("${file.url}")
 | 
			
		||||
    private String url;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private ThreadWordUtil threadWordUtil;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void reviewRecord(ReviewRecordDTO reviewRecordDTO) {
 | 
			
		||||
        if (!userDao.existUser(reviewRecordDTO.getReviewerPhone())) {
 | 
			
		||||
@ -255,9 +260,23 @@ public class RecordServiceImpl implements RecordService {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void record2word(String uuid, HttpServletResponse response) {
 | 
			
		||||
    public boolean record2word(String uuid) {
 | 
			
		||||
        Record record = recordDao.getRecord(uuid);
 | 
			
		||||
        if(!record.getMetaDataType().equals(MetaDataTypeEnum.REPORT_TEMP.getValue())) {
 | 
			
		||||
            throw new RecordException(ResultEnum.NOT_EXIST_RECORD);
 | 
			
		||||
        }
 | 
			
		||||
        if(record.getState().equals(RecordStateEnum.REVIEWED.getValue())) {
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        threadWordUtil.setRecord(record);
 | 
			
		||||
        threadWordUtil.setThreadName(record.getUuid() + ":" + String.valueOf(System.currentTimeMillis()));
 | 
			
		||||
        // ThreadWordUtil wordUtil = new ThreadWordUtil();
 | 
			
		||||
        // wordUtil.setRecord(record);
 | 
			
		||||
        // wordUtil.setThreadName(record.getUuid() + ":" + String.valueOf(System.currentTimeMillis()));
 | 
			
		||||
        threadWordUtil.start();
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@ -288,4 +307,10 @@ public class RecordServiceImpl implements RecordService {
 | 
			
		||||
        recordDao.saveRecord(record);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void report(String uuid, HttpServletResponse response) {
 | 
			
		||||
        // TODO Auto-generated method stub
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										111
									
								
								src/main/java/com/example/survey/util/CustomXWPFDocument.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								src/main/java/com/example/survey/util/CustomXWPFDocument.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,111 @@
 | 
			
		||||
package com.example.survey.util;
 | 
			
		||||
 | 
			
		||||
import org.apache.poi.openxml4j.opc.OPCPackage;
 | 
			
		||||
 | 
			
		||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
 | 
			
		||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
 | 
			
		||||
import org.apache.xmlbeans.XmlException;
 | 
			
		||||
import org.apache.xmlbeans.XmlToken;
 | 
			
		||||
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
 | 
			
		||||
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
 | 
			
		||||
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
 | 
			
		||||
 | 
			
		||||
import fr.opensagres.poi.xwpf.converter.core.IXWPFConverter;
 | 
			
		||||
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
 | 
			
		||||
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
 | 
			
		||||
 | 
			
		||||
import java.io.FileOutputStream;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
import java.io.OutputStream;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 
 | 
			
		||||
 * ———————————————— 版权声明:本文为CSDN博主「BlogTonycsdn」的原创文章,遵循 CC 4.0 BY-SA
 | 
			
		||||
 * 版权协议,转载请附上原文出处链接及本声明。
 | 
			
		||||
 * 原文链接:https://blog.csdn.net/liyuzhuang/article/details/78094349 支持word2007+
 | 
			
		||||
 * 插入图片
 | 
			
		||||
 * 
 | 
			
		||||
 * @author liyuzhuang
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public class CustomXWPFDocument extends XWPFDocument {
 | 
			
		||||
    public CustomXWPFDocument(InputStream in) throws IOException {
 | 
			
		||||
        super(in);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public CustomXWPFDocument(OPCPackage pkg) throws IOException {
 | 
			
		||||
        super(pkg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void createPicture(String blipId, int id, int width, int height, XWPFParagraph paragraph) {
 | 
			
		||||
        final int EMU = 9525;
 | 
			
		||||
        width *= EMU;
 | 
			
		||||
        height *= EMU;
 | 
			
		||||
 | 
			
		||||
        CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline();
 | 
			
		||||
 | 
			
		||||
        String picXml = "" + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"
 | 
			
		||||
                + "   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
 | 
			
		||||
                + "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
 | 
			
		||||
                + "         <pic:nvPicPr>" + "            <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>"
 | 
			
		||||
                + "            <pic:cNvPicPr/>" + "         </pic:nvPicPr>" + "         <pic:blipFill>"
 | 
			
		||||
                + "            <a:blip r:embed=\"" + blipId
 | 
			
		||||
                + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"
 | 
			
		||||
                + "            <a:stretch>" + "               <a:fillRect/>" + "            </a:stretch>"
 | 
			
		||||
                + "         </pic:blipFill>" + "         <pic:spPr>" + "            <a:xfrm>"
 | 
			
		||||
                + "               <a:off x=\"0\" y=\"0\"/>" + "               <a:ext cx=\"" + width + "\" cy=\""
 | 
			
		||||
                + height + "\"/>" + "            </a:xfrm>" + "            <a:prstGeom prst=\"rect\">"
 | 
			
		||||
                + "               <a:avLst/>" + "            </a:prstGeom>" + "         </pic:spPr>"
 | 
			
		||||
                + "      </pic:pic>" + "   </a:graphicData>" + "</a:graphic>";
 | 
			
		||||
 | 
			
		||||
        // CTGraphicalObjectData graphicData =
 | 
			
		||||
        // inline.addNewGraphic().addNewGraphicData();
 | 
			
		||||
        XmlToken xmlToken = null;
 | 
			
		||||
        try {
 | 
			
		||||
            xmlToken = XmlToken.Factory.parse(picXml);
 | 
			
		||||
        } catch (XmlException xe) {
 | 
			
		||||
            xe.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
        inline.set(xmlToken);
 | 
			
		||||
        // graphicData.set(xmlToken);
 | 
			
		||||
 | 
			
		||||
        inline.setDistT(0);
 | 
			
		||||
        inline.setDistB(0);
 | 
			
		||||
        inline.setDistL(0);
 | 
			
		||||
        inline.setDistR(0);
 | 
			
		||||
 | 
			
		||||
        CTPositiveSize2D extent = inline.addNewExtent();
 | 
			
		||||
        extent.setCx(width);
 | 
			
		||||
        extent.setCy(height);
 | 
			
		||||
 | 
			
		||||
        CTNonVisualDrawingProps docPr = inline.addNewDocPr();
 | 
			
		||||
        docPr.setId(id);
 | 
			
		||||
        docPr.setName("Picture " + id);
 | 
			
		||||
        docPr.setDescr("Generated");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void convertToPDF(String output) throws IOException {
 | 
			
		||||
        OutputStream out = null;
 | 
			
		||||
        try{
 | 
			
		||||
            throw(new Exception());
 | 
			
		||||
            /**
 | 
			
		||||
             * 格式错误 不能使用
 | 
			
		||||
             */
 | 
			
		||||
            // PdfOptions pdfOptions = PdfOptions.create().fontEncoding("UTF-8");
 | 
			
		||||
            // out = new FileOutputStream(output + ".pdf");
 | 
			
		||||
            // IXWPFConverter<PdfOptions> converter = PdfConverter.getInstance();
 | 
			
		||||
            // converter.convert(this, out, pdfOptions);
 | 
			
		||||
        }
 | 
			
		||||
        catch (Exception e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
            out = new FileOutputStream(output);
 | 
			
		||||
            this.write(out);
 | 
			
		||||
        }
 | 
			
		||||
        finally {
 | 
			
		||||
            if(out != null) {
 | 
			
		||||
                out.close();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								src/main/java/com/example/survey/util/DownloadUtil.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/main/java/com/example/survey/util/DownloadUtil.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
			
		||||
package com.example.survey.util;
 | 
			
		||||
 | 
			
		||||
import java.io.BufferedOutputStream;
 | 
			
		||||
import java.io.FileInputStream;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.OutputStream;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
import javax.servlet.http.HttpServletResponse;
 | 
			
		||||
 | 
			
		||||
public class DownloadUtil {
 | 
			
		||||
    public static void downloadFile(String path, HttpServletResponse response) throws IOException {
 | 
			
		||||
        response.addHeader("Content-Disposition", "attachment;filename=" + UUID.randomUUID().toString() + ".docx");
 | 
			
		||||
        FileInputStream inputStream = new FileInputStream(path);
 | 
			
		||||
        int count =0;
 | 
			
		||||
        byte[] by = new byte[1024];
 | 
			
		||||
        
 | 
			
		||||
        OutputStream out = response.getOutputStream();
 | 
			
		||||
        while((count=inputStream.read(by))!=-1){
 | 
			
		||||
            out.write(by, 0, count);//将缓冲区的数据输出到浏览器
 | 
			
		||||
        }
 | 
			
		||||
        inputStream.close();
 | 
			
		||||
        out.flush();
 | 
			
		||||
        out.close();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										60
									
								
								src/main/java/com/example/survey/util/ThreadWordUtil.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								src/main/java/com/example/survey/util/ThreadWordUtil.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,60 @@
 | 
			
		||||
package com.example.survey.util;
 | 
			
		||||
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
import com.example.survey.dao.RecordDao;
 | 
			
		||||
import com.example.survey.entity.Record;
 | 
			
		||||
import com.example.survey.enumeration.RecordStateEnum;
 | 
			
		||||
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Value;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
 | 
			
		||||
@Data
 | 
			
		||||
@Component
 | 
			
		||||
public class ThreadWordUtil implements Runnable {
 | 
			
		||||
 | 
			
		||||
    private Record record;
 | 
			
		||||
    private Thread t;
 | 
			
		||||
    private String threadName;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    @Value("${file.path}")
 | 
			
		||||
    private String path;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private RecordDao recordDao;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void run() {
 | 
			
		||||
        if(record != null) {
 | 
			
		||||
            String templatePath = this.path + record.getMetaData().getName() + ".docx";
 | 
			
		||||
            String outputPath = this.path + record.getUuid() + ".docx";
 | 
			
		||||
            try{
 | 
			
		||||
                Map<String, Object> values = record.getValues();
 | 
			
		||||
                values.put("submit", record.getOperationList().get(0).getUser().getUsername());
 | 
			
		||||
                values.put("review", record.getOperationList().get(0).getUser().getUsername());
 | 
			
		||||
                
 | 
			
		||||
                WordUtil wordUtil = new WordUtil(values, templatePath, outputPath);
 | 
			
		||||
                wordUtil.export2word();
 | 
			
		||||
                record.setState(RecordStateEnum.REVIEWED.getValue());
 | 
			
		||||
                recordDao.saveRecord(record);
 | 
			
		||||
            }
 | 
			
		||||
            catch(Exception e) {
 | 
			
		||||
                e.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public void start() {
 | 
			
		||||
        if (t == null) {
 | 
			
		||||
            t = new Thread(this, threadName);
 | 
			
		||||
            t.start();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
@ -1,81 +1,232 @@
 | 
			
		||||
package com.example.survey.util;
 | 
			
		||||
 | 
			
		||||
import com.deepoove.poi.XWPFTemplate;
 | 
			
		||||
import com.deepoove.poi.config.Configure;
 | 
			
		||||
import com.deepoove.poi.config.ConfigureBuilder;
 | 
			
		||||
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
 | 
			
		||||
import com.example.survey.entity.MetaData;
 | 
			
		||||
import com.example.survey.entity.Record;
 | 
			
		||||
 | 
			
		||||
import org.apache.commons.io.FileUtils;
 | 
			
		||||
import org.apache.poi.ooxml.POIXMLDocument;
 | 
			
		||||
import org.apache.poi.openxml4j.opc.OPCPackage;
 | 
			
		||||
import org.apache.poi.xwpf.usermodel.Document;
 | 
			
		||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
 | 
			
		||||
import org.apache.poi.xwpf.usermodel.XWPFRun;
 | 
			
		||||
import org.apache.poi.xwpf.usermodel.XWPFTable;
 | 
			
		||||
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
 | 
			
		||||
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
 | 
			
		||||
import org.apache.xmlbeans.XmlCursor;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
 | 
			
		||||
import javax.servlet.http.HttpServletResponse;
 | 
			
		||||
import java.io.BufferedOutputStream;
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.FileInputStream;
 | 
			
		||||
import java.io.FileNotFoundException;
 | 
			
		||||
import java.io.FileOutputStream;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.OutputStream;
 | 
			
		||||
import java.lang.reflect.Method;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.util.Map.Entry;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author Pope
 | 
			
		||||
 */
 | 
			
		||||
// @Component
 | 
			
		||||
public class WordUtil {
 | 
			
		||||
    public static void export2word(HttpServletResponse response, Map<String, Object> values, String templatePath) {
 | 
			
		||||
 | 
			
		||||
        response.addHeader("Content-Disposition", "attachment;filename=" + UUID.randomUUID().toString() + ".docx");
 | 
			
		||||
    private CustomXWPFDocument document;
 | 
			
		||||
    private HashMap<String, Method> key2methods;
 | 
			
		||||
    private Map<String, Object> values;
 | 
			
		||||
    private String outputPath;
 | 
			
		||||
 | 
			
		||||
        ConfigureBuilder configureBuilder = Configure.builder();
 | 
			
		||||
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
 | 
			
		||||
        for (String key : values.keySet()) {
 | 
			
		||||
            Object value = values.get(key);
 | 
			
		||||
            if (value instanceof List) {
 | 
			
		||||
                List<Object> newValue = (List<Object>) value;
 | 
			
		||||
                if(newValue.size()==0){
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
                Object o = newValue.get(0);
 | 
			
		||||
                if (o instanceof String) {
 | 
			
		||||
                    StringBuilder sb = new StringBuilder();
 | 
			
		||||
                    newValue.forEach(str -> {
 | 
			
		||||
                        sb.append(str + " ");
 | 
			
		||||
                    });
 | 
			
		||||
                    values.put(key, sb.toString());
 | 
			
		||||
                } else {
 | 
			
		||||
                    configureBuilder = configureBuilder.bind(key, policy);
 | 
			
		||||
    public WordUtil(Map<String, Object> values, String templatePath, String outputPath)
 | 
			
		||||
            throws FileNotFoundException, IOException, NoSuchMethodException, SecurityException {
 | 
			
		||||
        File temp = new File(UUID.randomUUID() +  "t.docx");
 | 
			
		||||
        this.outputPath = outputPath;
 | 
			
		||||
        FileUtils.copyFile(new File(templatePath), temp);
 | 
			
		||||
        // response.addHeader("Content-Disposition", "attachment;filename=" +
 | 
			
		||||
        // UUID.randomUUID().toString() + ".docx");
 | 
			
		||||
        OPCPackage pack = POIXMLDocument.openPackage(temp.getAbsolutePath());
 | 
			
		||||
        document = new CustomXWPFDocument(pack);
 | 
			
		||||
        this.values = values;
 | 
			
		||||
 | 
			
		||||
        key2methods = new HashMap<>();
 | 
			
		||||
 | 
			
		||||
        key2methods.put("$", getClass().getDeclaredMethod("processText", XWPFTableCell.class, String.class));
 | 
			
		||||
        key2methods.put("#", getClass().getDeclaredMethod("processImage", XWPFTableCell.class, String.class));
 | 
			
		||||
        key2methods.put("@", getClass().getDeclaredMethod("processTable", XWPFTableCell.class, String.class));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void export2word()
 | 
			
		||||
            throws FileNotFoundException, IOException, NoSuchMethodException, SecurityException {
 | 
			
		||||
 | 
			
		||||
        List<XWPFTable> tables = document.getTables();
 | 
			
		||||
        HashMap<String, XWPFTableCell> runs = new HashMap<>();
 | 
			
		||||
        for (XWPFTable table : tables) {
 | 
			
		||||
            // 遍历表格
 | 
			
		||||
            List<XWPFTableRow> rows = table.getRows();
 | 
			
		||||
            for (int i = 0; i < rows.size(); i++) {
 | 
			
		||||
                XWPFTableRow row = table.getRow(i);
 | 
			
		||||
                List<XWPFTableCell> cells = row.getTableCells();
 | 
			
		||||
                for (int j = 0; j < cells.size(); j++) {
 | 
			
		||||
                    XWPFTableCell cell = cells.get(j);
 | 
			
		||||
                    String key = cell.getText();
 | 
			
		||||
                    if (key.length() == 0) {
 | 
			
		||||
                        continue;
 | 
			
		||||
                    }
 | 
			
		||||
                    String keyHead = key.substring(0, 1);
 | 
			
		||||
                    String trueKey = key.substring(1);
 | 
			
		||||
                    if (key2methods.containsKey(keyHead)) {
 | 
			
		||||
                        cell.removeParagraph(0);
 | 
			
		||||
                    } else {
 | 
			
		||||
                        continue;
 | 
			
		||||
                    }
 | 
			
		||||
                    if (values.containsKey(trueKey)) {
 | 
			
		||||
                        runs.put(key, cell);
 | 
			
		||||
                    } else {
 | 
			
		||||
                        cell.setText("");
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        XWPFTemplate template = XWPFTemplate.compile(templatePath, configureBuilder.build()).render(values);
 | 
			
		||||
        try (BufferedOutputStream os = new BufferedOutputStream(response.getOutputStream())) {
 | 
			
		||||
            template.write(os);
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        } finally {
 | 
			
		||||
 | 
			
		||||
        for (Entry<String, XWPFTableCell> entry : runs.entrySet()) {
 | 
			
		||||
            String key = entry.getKey();
 | 
			
		||||
            XWPFTableCell cell = entry.getValue();
 | 
			
		||||
            Method method = key2methods.get(key.subSequence(0, 1));
 | 
			
		||||
            String trueKey = key.substring(1);
 | 
			
		||||
            try {
 | 
			
		||||
                template.close();
 | 
			
		||||
            } catch (IOException e) {
 | 
			
		||||
                method.invoke(this, cell, values.get(trueKey));
 | 
			
		||||
                // System.out.println(key + ":" + values.get(key));
 | 
			
		||||
            } catch (Exception e) {
 | 
			
		||||
                e.printStackTrace();
 | 
			
		||||
                cell.setText("");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        OutputStream outputStream = new FileOutputStream(outputPath);
 | 
			
		||||
        // document.
 | 
			
		||||
        document.write(outputStream);
 | 
			
		||||
        outputStream.close();
 | 
			
		||||
        document.close();
 | 
			
		||||
        
 | 
			
		||||
        // XWPFTemplate template = XWPFTemplate.compile(templatePath,
 | 
			
		||||
        // configureBuilder.build()).render(values);
 | 
			
		||||
        // try (BufferedOutputStream os = new
 | 
			
		||||
        // BufferedOutputStream(response.getOutputStream())) {
 | 
			
		||||
        // template.write(os);
 | 
			
		||||
        // } catch (IOException e) {
 | 
			
		||||
        // e.printStackTrace();
 | 
			
		||||
        // } finally {
 | 
			
		||||
        // try {
 | 
			
		||||
        // template.close();
 | 
			
		||||
        // } catch (IOException e) {
 | 
			
		||||
        // e.printStackTrace();
 | 
			
		||||
        // }
 | 
			
		||||
        // }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void export2Word(HttpServletResponse response, MetaData metaData, Record record){
 | 
			
		||||
        Map<String, Object> values = record.getValues();
 | 
			
		||||
        // String templatePath = metaData.getWordTemplate();
 | 
			
		||||
        for (Map.Entry<String, Object> entry : metaData.getForm().entrySet()) {
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    private void processText(XWPFTableCell cell, String msg) {
 | 
			
		||||
        // 执行\n换行
 | 
			
		||||
        if (msg == null) {
 | 
			
		||||
            cell.setText("");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        String[] st = msg.split("\n");
 | 
			
		||||
        for (int p = 0; p < st.length; p++) {
 | 
			
		||||
            cell.addParagraph();
 | 
			
		||||
        }
 | 
			
		||||
        List<XWPFParagraph> pglist = cell.getParagraphs();
 | 
			
		||||
        for (int pi = 0; pi < pglist.size(); pi++) {
 | 
			
		||||
            XWPFRun run = pglist.get(pi).createRun();
 | 
			
		||||
            if(pi >= st.length){
 | 
			
		||||
                System.out.println("ERROR!!!!!!!!!!!");
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            run.setText(st[pi]);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void processImage(XWPFTableCell cell, String imgInfo) {
 | 
			
		||||
        String[] infos = imgInfo.split(":");
 | 
			
		||||
        String imgPath = infos[0];
 | 
			
		||||
 | 
			
		||||
        int imgHeight = Integer.parseInt(infos[1]);
 | 
			
		||||
        int imgWidth = Integer.parseInt(infos[2]);
 | 
			
		||||
        XWPFParagraph paragraph = cell.addParagraph();
 | 
			
		||||
        int length = paragraph.getRuns().size();
 | 
			
		||||
        if (length > 0) {
 | 
			
		||||
            for (int idx = (length - 1); idx >= 0; idx--) {
 | 
			
		||||
                paragraph.removeRun(idx);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        try {
 | 
			
		||||
            String blipId = document.addPictureData(new FileInputStream(new File(imgPath)), Document.PICTURE_TYPE_PNG);
 | 
			
		||||
            document.createPicture(blipId, document.getNextPicNameNumber(Document.PICTURE_TYPE_PNG), imgHeight,
 | 
			
		||||
                    imgWidth, paragraph);
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            try {
 | 
			
		||||
                cell.removeParagraph(0);
 | 
			
		||||
                processText(cell, infos[3]);
 | 
			
		||||
            } catch (Exception e1) {
 | 
			
		||||
                cell.setText("");
 | 
			
		||||
                e1.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
            // e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void processTable(XWPFTableCell cell, String tableInfos) {
 | 
			
		||||
        String[] infos = tableInfos.split("&");
 | 
			
		||||
 | 
			
		||||
        String tableMetaInfo = infos[0];
 | 
			
		||||
        String tableContent = infos[1];
 | 
			
		||||
 | 
			
		||||
        String[] mInfos = tableMetaInfo.split(":");
 | 
			
		||||
        int rows = Integer.parseInt(mInfos[0]);
 | 
			
		||||
        int cols = Integer.parseInt(mInfos[1]);
 | 
			
		||||
 | 
			
		||||
        XmlCursor cursor = cell.addParagraph().getCTP().newCursor();
 | 
			
		||||
        // cursor
 | 
			
		||||
        XWPFTable table = cell.insertNewTbl(cursor);
 | 
			
		||||
 | 
			
		||||
        String[] rowsString = tableContent.split("\r\n");
 | 
			
		||||
 | 
			
		||||
        for (int i = 0; i < rows; i++) {
 | 
			
		||||
            if (rowsString.length <= i) {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            XWPFTableRow row = table.createRow();
 | 
			
		||||
 | 
			
		||||
            if (i == 0) {
 | 
			
		||||
                for (int j = 0; j < cols; j++) {
 | 
			
		||||
                    row.addNewTableCell();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            String[] colsString = rowsString[i].split("\t");
 | 
			
		||||
 | 
			
		||||
            for (int j = 0; j < cols; j++) {
 | 
			
		||||
                if (colsString.length <= j) {
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
                String msg = colsString[j];
 | 
			
		||||
 | 
			
		||||
                XWPFTableCell tableCell = row.getCell(j);
 | 
			
		||||
                tableCell.removeParagraph(0);
 | 
			
		||||
                try {
 | 
			
		||||
                    processText(tableCell, msg);
 | 
			
		||||
                } catch (Exception e) {
 | 
			
		||||
                    tableCell.setText("");
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void handleText(){
 | 
			
		||||
 | 
			
		||||
    public static String generateImageStr(String path, int w, int h, String alt) {
 | 
			
		||||
        return path + ":" + String.valueOf(w) + ":" + String.valueOf(h) + ":" + alt;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void handleDate(){
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void handleList(){
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user