java aspose.words implements Word to generate form and download to local

1. Maven adds aspose.words dependency

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>20.2</version>
</dependency>

Second, the background form is generated and transmitted to the local

1. Write js in the foreground so that it can jump to the Controller

location.href="/wordExport/exportScoreList?username=sa&readFlag=1"

2. Create Controller class and processing method

@RequestMapping("/wordExport")
@RestController
public class WordExportController{
    @Autowired
    private TestRecordService testRecordService;
    
    @GetMapping("/exportScoreList")
    public void exportScoreList(HttpServletResponse response,String username,
                                String testType,String testName,String readFlag){
        username=username==null?"":username;
        testType=testType==null?"":testType;
        testName=testName==null?"":testName;
        readFlag=readFlag==null?"":readFlag;

        List<TestRecordExt> testRecordExtList=testRecordService.getTestRecordExtList("",username,readFlag,testName,testType);
        try {
            String fileName="考试成绩单"+ DateUtil.getCurrentTimeByDay()+".doc";
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"),"ISO-8859-1"));
            OutputStream outputStream=response.getOutputStream();
            Document document=new Document();
            DocumentBuilder builder = new DocumentBuilder(document);
            //单元格水平居中对齐
            builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
            //段落居中对齐
            builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
            builder.writeln("考试成绩单");
            //段落左对齐
            builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
            String pickname="";
            if(testRecordExtList!=null&&testRecordExtList.size()>0){
                pickname=testRecordExtList.get(0).getPickname();
            }
            builder.writeln("姓名:"+pickname);
            builder.startTable();
            //表头
            builder.insertCell();
            builder.write("序号");
            builder.insertCell();
            builder.write("考试");
            builder.insertCell();
            builder.write("考试时长");
            builder.insertCell();
            builder.write("成绩");
            builder.insertCell();
            builder.write("排名");
            builder.insertCell();
            builder.write("考试类型");
            builder.endRow();
            //表格内容
            for(int i=0;i<testRecordExtList.size();i++){
                TestRecordExt testRecordExt=testRecordExtList.get(i);
                builder.insertCell();
                builder.write((i+1)+"");
                builder.insertCell();
                builder.write(testRecordExt.getTestName());
                builder.insertCell();
                int testRecordLength=testRecordExt.getTestRecordLength();
                String testLengthStr="";
                int testHour=testRecordLength/3600;
                if(testHour>0){
                    testLengthStr+=testHour+" 小时";
                }
                int testMinutes=testRecordLength%3600/60;
                if(testMinutes>0){
                    testLengthStr+=testMinutes+" 分钟";
                }
                int testSecond=testRecordLength%60;
                if(testSecond>0){
                    testLengthStr+=testSecond+" 秒";
                }
                builder.write(testLengthStr);
                builder.insertCell();
                builder.write(testRecordExt.getTestScore()+"分");
                builder.insertCell();
                builder.write(testRecordExt.getRankNo()+"");
                builder.insertCell();
                builder.write(testRecordExt.getTestTypeName());
                builder.endRow();
            }
            builder.endTable();
            document.save(outputStream, SaveFormat.DOC);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
}

Final renderings

3. Problems encountered

1. Maven added aspose.words dependency and there was a situation where the jar package could not be downloaded

Please refer to the following blog

maven cannot download aspose solution

2. When the file is downloaded to the local, the Chinese in the file name cannot be displayed

Java download file, the file name cannot be displayed in Chinese

3. There are watermarks in the Word document, and there are three watermarks

4. After Maven plus aspose.words dependency, Spring Boot takes 10 minutes to start.

Anyone who has solved the above two problems, please be sure to tell me

If you are interested in using the poi component to generate Word and download it to the local, you can go to another blog of mine

Java poi realizes Word to generate form and download to local

Published 34 original articles · received 1 · views 1946

Guess you like

Origin blog.csdn.net/qq_38974638/article/details/104790242