【Aspose-words】导出html到word

1、由于Mavenzh中央仓库中对于com.aspose.words jar包的缺乏,小编本地maven集成下载的 aspose-words-16.4.0-jdk16.jar

2、

package com.xw.ssm.util.word;

import com.alibaba.fastjson.JSONObject;
import com.aspose.words.*;
import com.xw.ssm.util.UUIDUtil;
import com.xw.ssm.util.resultObj.RespMsg;
import org.apache.commons.lang.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

public class WordUtil {

    private static String filePath = "/paper";

    public static JSONObject exportWord(HttpServletRequest request, HttpServletResponse response, String html, String titleName) {
        OutputStream outputStream = null;
        InputStream inputStream = null;
        File filePaper = null;
        try {
            // 判断文件夹是否存在;不存在则创建
            File file1 = new File(filePath);
            if (!file1.exists()) {
                file1.mkdirs();
            }
            File outputFile = null;
            String fileName = UUIDUtil.getUUID() + ".doc";
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            outputFile = new File(filePath + "/" + fileName);
            // 判断文件是否存在;不存在则创建
            if (!outputFile.exists()) {
                outputFile.createNewFile();
            }
            // 定义输出文档
            Document doc = new Document();
            DocumentBuilder docBuilder = new DocumentBuilder(doc);

            // 设置文档属性
            BuiltInDocumentProperties pro = doc.getBuiltInDocumentProperties();
            pro.setTitle("测试文档");
            pro.setSubject("测试文档");
            pro.setComments("");
            pro.setVersion(1);

            // 装订线
            docBuilder.startBookmark("装订线");

            docBuilder.endBookmark("装订线");

            String paperSize = "A4";
            SectionCollection sections = doc.getSections();
            switch (paperSize) {
//                case "A4H":
//                    for (Section section : sections) {
//                        PageSetup setup = section.getPageSetup();
//                        setup.setPaperSize(PaperSize.A4);
//                        setup.setOrientation(Orientation.LANDSCAPE);
//                        TextColumnCollection column = setup.getTextColumns();
//                        column.setCount(2);
//                        column.setEvenlySpaced(true);
//                        column.setLineBetween(true);
//                        // setup.setLeftMargin(3.35);
//                        // setup.setRightMargin(0.99);
//                    }
//                    break;
                case "A4":
                    for (Section section : sections) {
                        PageSetup setup = section.getPageSetup();
                        setup.setPaperSize(PaperSize.A4);
                        setup.setOrientation(Orientation.PORTRAIT);
                        setup.setVerticalAlignment(PageVerticalAlignment.TOP);
                    }
                    break;

//                case "A3H":
//                    for (Section section : sections) {
//                        PageSetup setup = section.getPageSetup();
//                        setup.setPaperSize(PaperSize.A3);
//                        setup.setOrientation(Orientation.LANDSCAPE);
//                        TextColumnCollection column = setup.getTextColumns();
//                        column.setCount(2);
//                        column.setEvenlySpaced(true);
//                        column.setLineBetween(true);
//                    }
//                    break;
//                case "A3":
//                    for (Section section : sections) {
//                        PageSetup setup = section.getPageSetup();
//                        setup.setPaperSize(PaperSize.A4);
//                        setup.setOrientation(Orientation.PORTRAIT);
//                    }
//                    break;
//                case "KH8":
//                    for (Section section : sections) {
//                        PageSetup setup = section.getPageSetup();
//                        setup.setPageWidth(36.4);
//                        setup.setPageHeight(25.7);
//                        setup.setOrientation(Orientation.LANDSCAPE);
//                        TextColumnCollection column = setup.getTextColumns();
//                        column.setCount(2);
//                        column.setEvenlySpaced(true);
//                        column.setLineBetween(true);
//                        // setup.setLeftMargin(3.35);
//                        // setup.setRightMargin(0.99);
//                    }
//                    break;
//                case "B4":
//                    for (Section section : sections) {
//                        PageSetup setup = section.getPageSetup();
//                        setup.setPaperSize(PaperSize.B4);
//                        setup.setOrientation(Orientation.PORTRAIT);
//                    }
//                    break;
//                case "K16":
//                    for (Section section : sections) {
//                        PageSetup setup = section.getPageSetup();
//                        setup.setPaperSize(PaperSize.B5);
//                        setup.setOrientation(Orientation.PORTRAIT);
//                    }
//                    break;
            }
            // 试卷标题
            docBuilder.getPageSetup().setVerticalAlignment(PageVerticalAlignment.CENTER);
            docBuilder.insertHtml(html);
            outputStream = new FileOutputStream(outputFile);
            doc.save(outputStream, SaveFormat.DOC);
            filePaper = new File(filePath + "/" + fileName);
            if (!filePaper.exists()) {
                return RespMsg.FAIL("文件生成失败,请联系管理员");
            }
            inputStream = new FileInputStream(filePaper);
            titleName = titleName + ".doc";
            downloadFile(request, response, titleName, inputStream);
            return RespMsg.SUCCESS();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(outputStream);
            IOUtils.closeQuietly(inputStream);
            if(filePaper != null){
                filePaper.delete();
            }
        }
        return RespMsg.FAIL();
    }


    /**
     * 下载
     *
     * @param request
     * @param response
     * @param filename
     * @param inputStream
     * @throws IOException
     */
    public static void downloadFile(HttpServletRequest request,
                                    HttpServletResponse response,
                                    String filename,
                                    InputStream inputStream) throws IOException {
        OutputStream toClient = null;
        try {
            String userAgent = request.getHeader("USER-AGENT");
            if (StringUtils.contains(userAgent, "MSIE")) {//IE浏览器
                filename = URLEncoder.encode(filename, "UTF-8");
            } else if (StringUtils.contains(userAgent, "Mozilla")) {//google,火狐浏览器
                filename = new String(filename.getBytes(), "ISO8859-1");
            } else {
                filename = URLEncoder.encode(filename, "UTF-8");//其他浏览器
            }
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + filename);
            response.addHeader("Content-Length", "" + inputStream.available());
            toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");

            // IOUtils.copy(inputStream, toClient);
            byte[] buff = new byte[1024];
            while (true) {
                int read = inputStream.read(buff);
                if (read == -1)
                    break;
                toClient.write(buff, 0, read);
            }
            toClient.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(toClient);
            IOUtils.closeQuietly(inputStream);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值