Java poi realizes Word to generate form and download to local

First of all, let me talk about the required classes:

XWPFDocument represents a docx document, which can be used to read docx documents, and can also be used to write docx documents.
XWPFParagraph represents various paragraphs such as documents, tables, and titles. It is composed of multiple XWPFRuns.
XWPFRun represents a section of text with the same style.
XWPFTable represents a table
XWPFTableRow represents a row of the table
XWPFTableCell represents a cell of the table

1. Maven joins poi dependency

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</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

@RestController
@RequestMapping("/wordExport")
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();

            XWPFDocument document =getTestScoreListWord(testRecordExtList);
            document.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
   //生成考试成绩单Word文档
    public XWPFDocument getTestScoreListWord(List<TestRecordExt> testRecordExtList){
        XWPFDocument document = new XWPFDocument();
        //第一行,文字居中对齐
        XWPFParagraph titleParagraph = document.createParagraph();
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
        titleParagraph.createRun().setText("考试成绩单");
        //第二行
        String pickname="";
        if(testRecordExtList!=null&&testRecordExtList.size()>0){
            pickname=testRecordExtList.get(0).getPickname();
        }
        document.createParagraph().createRun().setText("姓名:"+pickname);
        //生成表格
        XWPFTable table = document.createTable(testRecordExtList.size()+1, 6);
        List<XWPFTableRow> rowList = table.getRows();
        //表头下标
        final int COL_NO=0;//序号
        final int COL_TESTNAME=1;//考试
        final int COL_TESTRECORDLENGTH=2;//考试时长
        final int COL_TESTSCORE=3;//成绩
        final int COL_RANKNO=4;//排名
        final int COL_TESTTYPENAME=5;//考试类型
        //设置表头
        List<XWPFTableCell> cellList=rowList.get(0).getTableCells();
        //序号
        XWPFParagraph parapraph=cellList.get(COL_NO).getParagraphArray(0);
        parapraph.setAlignment(ParagraphAlignment.CENTER);
        parapraph.createRun().setText("序号");
        //考试
        parapraph= cellList.get(COL_TESTNAME).getParagraphArray(0);
        parapraph.setAlignment(ParagraphAlignment.CENTER);
        parapraph.createRun().setText("考试");
        //考试时长
        parapraph=cellList.get(COL_TESTRECORDLENGTH).getParagraphArray(0);
        parapraph.setAlignment(ParagraphAlignment.CENTER);
        parapraph.createRun().setText("考试时长");
        //成绩
        parapraph=cellList.get(COL_TESTSCORE).getParagraphArray(0);
        parapraph.setAlignment(ParagraphAlignment.CENTER);
        parapraph.createRun().setText("成绩");
        //排名
        parapraph=cellList.get(COL_RANKNO).getParagraphArray(0);
        parapraph.setAlignment(ParagraphAlignment.CENTER);
        parapraph.createRun().setText("排名");
        //考试类型
        parapraph=cellList.get(COL_TESTTYPENAME).getParagraphArray(0);
        parapraph.setAlignment(ParagraphAlignment.CENTER);
        parapraph.createRun().setText("考试类型");
        //设置表格内容
        for(int i = 0; i < testRecordExtList.size(); i++) {
            TestRecordExt testRecordExt = testRecordExtList.get(i);
            cellList = rowList.get(i+1).getTableCells();
            //序号
            parapraph=cellList.get(COL_NO).getParagraphArray(0);
            parapraph.setAlignment(ParagraphAlignment.CENTER);
            parapraph.createRun().setText((i+1)+"");
            //考试
            parapraph=cellList.get(COL_TESTNAME).getParagraphArray(0);
            parapraph.setAlignment(ParagraphAlignment.CENTER);
            parapraph.createRun().setText(testRecordExt.getTestName());
            //考试时长
            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+" 秒";
            }
            parapraph=cellList.get(COL_TESTRECORDLENGTH).getParagraphArray(0);
            parapraph.setAlignment(ParagraphAlignment.CENTER);
            parapraph.createRun().setText(testLengthStr);
            //成绩
            parapraph=cellList.get(COL_TESTSCORE).getParagraphArray(0);
            parapraph.setAlignment(ParagraphAlignment.CENTER);
            parapraph.createRun().setText(testRecordExt.getTestScore()+"分");
            //排名
            parapraph=cellList.get(COL_RANKNO).getParagraphArray(0);
            parapraph.setAlignment(ParagraphAlignment.CENTER);
            parapraph.createRun().setText(testRecordExt.getRankNo()+"");
            //考试类型
            parapraph=cellList.get(COL_TESTTYPENAME).getParagraphArray(0);
            parapraph.setAlignment(ParagraphAlignment.CENTER);
            parapraph.createRun().setText(testRecordExt.getTestTypeName());
        }
        return document;
    }
}

Final renderings

        In terms of use, it is still too cumbersome compared to aspose.words. If you are interested, you can go to another blog of mine. Aspose.words is used to implement Word to generate a form and download it to the local.

java aspose.words implements 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/104826799