java aspose.wordsはWordを実装してフォームを生成し、ローカルにダウンロードします

1. Mavenがaspose.words依存関係を追加

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

次に、背景フォームが生成され、ローカルに送信されます

1. jsをフォアグラウンドで書き込んで、コントローラーにジャンプできるようにします。

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

2. Controllerクラスと処理メソッドを作成する

@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();
        }
}

最終レンダリング

3.発生した問題

1. Mavenがaspose.words依存関係を追加し、jarパッケージをダウンロードできない状況がありました

下記ブログをご参照ください

mavenはasposeソリューションをダウンロードできません

2.ファイルをローカルにダウンロードすると、ファイル名の中国語が表示されない

Javaダウンロードファイル、ファイル名を中国語で表示できない

3. Word文書に透かしがあり、3つの透かしがある

4. Mavenとaspose.wordsの依存関係の後、Spring Bootの起動に10分かかります。

上記2つの問題を解決した方は必ず教えてください

poiコンポーネントを使用してWordを生成し、ローカルにダウンロードすることに興味がある場合は、私の別のブログにアクセスできます

Java poiはWordを実現してフォームを生成し、ローカルにダウンロードします

公開された34元の記事 ウォンの賞賛1 ビュー1946

おすすめ

転載: blog.csdn.net/qq_38974638/article/details/104790242