使用freemarker導出word、pdf、圖片

安裝openoffic啓動服務請參考:

https://www.cnblogs.com/warrior4236/p/5858755.html

 

maven包:

<!-- https://mvnrepository.com/artifact/freemarker/freemarker -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.jacob/jacob -->
<dependency>
    <groupId>com.jacob</groupId>
    <artifactId>jacob</artifactId>
    <version>1.18</version>
    <scope>system</scope>
    <systemPath>E:/jacob-1.18/jacob-1.18/jacob.jar</systemPath>
</dependency>
<dependency>
    <groupId>com.artofsolving</groupId>
    <artifactId>jodconverter</artifactId>
    <version>2.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.12</version>
</dependency>

直接幹活:

package com.nwpusct.csal.common.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import sun.misc.BASE64Encoder;

import java.io.*;

/**
 * freemarker + word模板 導出word
 *
 * @author bobo
 * @date 2019/4/14 17:41
 */
public class FreemarkeExportrWordUtil {

    /** 默FreeMarker配置實例 */
    private static final Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);

    /** 默認採用UTF-8編碼 */
    private static final String ENCODING = "UTF-8";

    /** buffer */
    private static final int BUFFER_SIZE = 1024;

    /**
     * 從指定freemarker模板所在文件夾
     *
     * “/”            對應 classpath
     * “/templates/”  對應 classpath下的templates/
     */
    private static final String DEFAULT_POSITION = "/";

    static {
        configuration.setDefaultEncoding(ENCODING);
    }

    /**
     * 導出excel
     *
     * @param templateFileName
     *         模板文件名(含後綴,如:abc.ftl)
     * @param resultFileAllPathName
     *         結果文件全路徑文件名 (如: C:/Users/result.doc  再如: C:/Users/result.docx)
     * @param dataObject
     *         與模板中的佔位符 對應的 數據信息(一般爲:一個專門創建的對象, 或者是Map)
     * @return 生成的word文件
     * @throws IOException
     * @throws TemplateException
     * @date 2019/4/16 10:52
     */
    public static File doExport(String templateFileName, String resultFileAllPathName, Object dataObject)
            throws IOException, TemplateException {
        return doExport(templateFileName, DEFAULT_POSITION, resultFileAllPathName, dataObject);
    }

    /**
     * 導出excel
     *
     * @param templateFileName
     *         模板文件名(含後綴,如:abc.ftl)
     * @param templateFileDir
     *         模板文件所在位置名(如: "/" 代表 classpath)
     * @param resultFileAllPathName
     *         結果文件全路徑文件名 (如: C:/Users/result.doc  再如: C:/Users/result.docx)
     * @param dataObject
     *         與模板中的佔位符 對應的 數據信息(一般爲:一個專門創建的對象, 或者是Map)
     * @return 生成的word文件
     * @throws IOException
     * @throws TemplateException
     * @date 2019/4/16 10:52
     */
    public static File doExport(String templateFileName, String templateFileDir,
                                String resultFileAllPathName, Object dataObject)
            throws IOException, TemplateException {

        // 指定模板文件所在  位置
        configuration.setClassForTemplateLoading(FreemarkeExportrWordUtil.class, templateFileDir);

        // 根據模板文件、編碼;獲取Template實例
        Template template = configuration.getTemplate(templateFileName, ENCODING);
        File resultFile = new File(resultFileAllPathName);
        // 判斷要生成的word文件所在目錄是否存在,不存在則創建
        if (!resultFile.getParentFile().exists()) {
            boolean result = resultFile.getParentFile().mkdirs();
        }
        // 寫出文件
        try (OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(resultFile),
                "UTF-8");
             Writer writer = new BufferedWriter(osw, BUFFER_SIZE)) {
            template.process(dataObject, writer);
        }
        return resultFile;
    }

    /**
     * 獲取圖片對應的base64碼
     *
     * @param imgFile
     *         圖片
     * @return 圖片對應的base64碼
     * @throws IOException
     * @date 2019/4/16 17:05
     */
    public static String getImageBase64String(File imgFile) throws IOException {
        InputStream inputStream = new FileInputStream(imgFile);
        byte[] data = new byte[inputStream.available()];
        int totalNumberBytes = inputStream.read(data);
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

}

package com.nwpusct.csal.common.exportUtil;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.List;

/**導出工具類
* pdf,圖片
* @Author hebo
* @Date 9:55 2019/4/30
*/
public class exportUtil {

    public static String realWord(String source, String target) {
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", false);
            Dispatch documents = app.getProperty("Documents").toDispatch();
            // 打開FreeMarker生成的Word文檔
            doc = Dispatch.call(documents, "Open", source, false, true).toDispatch();
            File tofile = new File(target);
            if (tofile.exists()) {
                tofile.delete();
            }
            // 另存爲新的Word文檔
            Dispatch.call(doc, "SaveAs", target, 12);
            Dispatch.call(doc, "Close", false);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (app != null) {
                app.invoke("Quit", new Variant[]{});
            }
            ComThread.Release();
            String command = "taskkill /f /im WINWORD.EXE";
            try {
                Runtime.getRuntime().exec(command);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return target;
    }

    public  static String wordTurnPdf(String doc, String pdf) {
        File outFile = null;
        File inputFile = new File(doc);
        outFile = new File(pdf);
        outFile.getParentFile().mkdirs();
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
        try {
            connection.connect();
        } catch (ConnectException e) {
            e.printStackTrace();
        }
        DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
        converter.convert(inputFile, outFile);
        connection.disconnect();
        return pdf;
    }

    public static String pdf2multiImage(String pdfFile, String outpath) {
        try {
            InputStream is = new FileInputStream(pdfFile);
            PDDocument pdf = PDDocument.load(is);
            int actSize  = pdf.getNumberOfPages();
            List<BufferedImage> piclist = new ArrayList<BufferedImage>();
            for (int i = 0; i < actSize; i++) {
                BufferedImage  image = new PDFRenderer(pdf).renderImageWithDPI(i,130, ImageType.RGB);
                piclist.add(image);
            }
            yPic(piclist, outpath);
            is.close();
            return outpath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void pdf2multiImage(InputStream is, String outpath) {
        try {
            PDDocument pdf = PDDocument.load(is);
            int actSize  = pdf.getNumberOfPages();
            List<BufferedImage> piclist = new ArrayList<BufferedImage>();
            for (int i = 0; i < actSize; i++) {
                BufferedImage  image = new PDFRenderer(pdf).renderImageWithDPI(i,130,ImageType.RGB);
                piclist.add(image);
            }
            yPic(piclist, outpath);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 將寬度相同的圖片,豎向追加在一起 ##注意:寬度必須相同
     * @param piclist
     *            文件流數組
     * @param outPath
     *            輸出路徑
     */
    public static void yPic(List<BufferedImage> piclist, String outPath) {// 縱向處理圖片
        if (piclist == null || piclist.size() <= 0) {
            System.out.println("圖片數組爲空!");
            return;
        }
        try {
            int height = 0, // 總高度
                    width = 0, // 總寬度
                    _height = 0, // 臨時的高度 , 或保存偏移高度
                    __height = 0, // 臨時的高度,主要保存每個高度
                    picNum = piclist.size();// 圖片的數量
            int[] heightArray = new int[picNum]; // 保存每個文件的高度
            BufferedImage buffer = null; // 保存圖片流
            List<int[]> imgRGB = new ArrayList<int[]>(); // 保存所有的圖片的RGB
            int[] _imgRGB; // 保存一張圖片中的RGB數據
            for (int i = 0; i < picNum; i++) {
                buffer = piclist.get(i);
                heightArray[i] = _height = buffer.getHeight();// 圖片高度
                if (i == 0) {
                    width = buffer.getWidth();// 圖片寬度
                }
                height += _height; // 獲取總高度
                _imgRGB = new int[width * _height];// 從圖片中讀取RGB
                _imgRGB = buffer.getRGB(0, 0, width, _height, _imgRGB, 0, width);
                imgRGB.add(_imgRGB);
            }
            _height = 0; // 設置偏移高度爲0
            // 生成新圖片
            BufferedImage imageResult = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for (int i = 0; i < picNum; i++) {
                __height = heightArray[i];
                if (i != 0) _height += __height; // 計算偏移高度
                imageResult.setRGB(0, _height, width, __height, imgRGB.get(i), 0, width); // 寫入流中
            }
            File outFile = new File(outPath);
            ImageIO.write(imageResult, "jpg", outFile);// 寫圖片
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
控制層:

package com.nwpusct.csal.controller.manage;

import com.nwpusct.csal.common.exportUtil.exportUtil;
import com.nwpusct.csal.common.util.FreemarkeExportrWordUtil;
import com.nwpusct.csal.common.util.RestResult;
import com.nwpusct.csal.common.util.RestResultUtil;
import com.nwpusct.csal.mapper.NdeReportInfoMapper;
import com.nwpusct.csal.model.NdeReportInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

/**
 * @Author hebo
 * @Date 14:09 2019/3/27
 */
@Api(tags = {"導出文件"})
@CrossOrigin
@RestController
@RequestMapping(value = "/export")
public class NdeExportFile {


    @Autowired
    private NdeReportInfoMapper ndeReportInfoMapper;

    @Value("${image.http-path}")
    private String httpPath;

    @Value("${export.path-pdf}")
    private String pathPdf;

    @Value("${export.path-word}")
    private String pathWord;
    @Value("${export.path-jpg}")
    private String pathJpg;

    @ApiOperation(value = "word文檔導出")
    @RequestMapping(value = "getExportWord", method = RequestMethod.GET)
    public RestResult<String> getExportWord(@RequestParam(value = "infoId") String infoId) {
        try {
            NdeReportInfo ndeReportInfo = ndeReportInfoMapper.selectById(infoId);
            String reportParmeter = ndeReportInfo.getReportParmeter();
            // 模板文件名
            String templateFileName = "templates/4-18.ftl";
            // 要生成的文件 全路徑文件名
            String fileName =
                    pathWord + new SimpleDateFormat("yyyyMMDDHHmmss").format(new Date()) + ".doc";
            JSONArray jsonArray = new JSONArray(reportParmeter);
            Map<String, Object> datas = new HashMap<>();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                JSONArray tables = jsonObject.getJSONArray("tables");
                List<Map<String, Object>> list = new ArrayList<>();
                datas.put("table" + i, list);
                for (int j = 0; j < tables.length(); j++) {
                    JSONObject jsonObject1 = tables.getJSONObject(j);
                    JSONObject tbs = jsonObject1.getJSONObject("tbody");
                    JSONArray trsTbs = tbs.getJSONArray("trs");
                    for (int k = 0; k < trsTbs.length(); k++) {
                        JSONObject jsonTrs = trsTbs.getJSONObject(k);
                        JSONArray tds = jsonTrs.getJSONArray("tds");
                        Map<String, Object> tempMap = new HashMap<>();
                        Integer string = jsonTrs.getInt("loop");
                        if (string == 1) {
                            int line = 0;
                            for (int l = 0; l < tds.length(); l++) {
                                JSONObject jsonTds = tds.getJSONObject(l);
                                String type = jsonTds.getString("type");
                                if (type.equals("input")) {
                                    String value = jsonTds.getString("value");
                                    tempMap.put("Td" + line, value);
                                    line++;
                                } else if (type.equals("label")) {
                                    String value1 = jsonTds.getString("label_chn");
                                    String value2 = jsonTds.getString("label_en");
                                    tempMap.put("Td" + line, value1);
                                    line++;
                                    tempMap.put("Td" + line, value2);
                                    line++;
                                }
                            }
                            list.add(tempMap);
                        }
                        if (string == 0) {
                            for (int l = 0; l < tds.length(); l++) {
                                JSONObject jsonTds = tds.getJSONObject(l);
                                if (jsonTds.length() != 0) {
                                    String type = jsonTds.getString("type");
                                    if (type.equals("input")) {
                                        if (jsonTds.getString("filed").equals("examinationTo")) {
                                            String value = jsonTds.getString("value");
                                            datas.put("examinationTo", value);
                                        }
                                        if (jsonTds.getString("filed").equals("commissionNo")) {
                                            String value = jsonTds.getString("value");
                                            datas.put("commissionNo", value);
                                        }
                                        if (jsonTds.getString("filed").equals("reportNo")) {
                                            String value = jsonTds.getString("value");
                                            datas.put("reportNo", value);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (ndeReportInfo.getSketch() != null && ndeReportInfo.getSketch() != "") {
                try {
                    File imageFile = new File(ndeReportInfo.getSketch());
                    datas.put("myImage",
                            FreemarkeExportrWordUtil.getImageBase64String(imageFile));
                } catch (
                        IOException e) {
                    e.printStackTrace();
                }
            }
            FreemarkeExportrWordUtil.doExport(templateFileName, fileName, datas);
            String doc = exportUtil.realWord(fileName, pathWord + ndeReportInfo.getInfoName() + ".doc");
            File fileDelete = new File(fileName);
            fileDelete.delete();
            return RestResultUtil.genSuccessResult(httpPath + doc.replaceAll("E:", ""));
        } catch (
                Exception e) {
            e.printStackTrace();
        }
        return RestResultUtil.failed("導出失敗");
    }

    @ApiOperation(value = "pdf文檔導出")
    @RequestMapping(value = "getExportPdf", method = RequestMethod.GET)
    public RestResult<String> getExportPdf(@RequestParam(value = "infoId") String infoId) {
        try {
            NdeReportInfo ndeReportInfo = ndeReportInfoMapper.selectById(infoId);
            String reportParmeter = ndeReportInfo.getReportParmeter();
            // 模板文件名
            String templateFileName = "templates/4-18.ftl";
            // 要生成的文件 全路徑文件名E:\learn\SecurityTest\src\main\resources\importWord
            File file1 = new File(pathWord);
            if (!file1.exists()) {
                file1.mkdir();
            }
            String fileName =
                    pathWord + new SimpleDateFormat("yyyyMMDDHHmmss").format(new Date()) + ".doc";
            JSONArray jsonArray = new JSONArray(reportParmeter);
            Map<String, Object> datas = new HashMap<>();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                JSONArray tables = jsonObject.getJSONArray("tables");
                List<Map<String, Object>> list = new ArrayList<>();
                datas.put("table" + i, list);
                for (int j = 0; j < tables.length(); j++) {
                    JSONObject jsonObject1 = tables.getJSONObject(j);
                    JSONObject tbs = jsonObject1.getJSONObject("tbody");
                    JSONArray trsTbs = tbs.getJSONArray("trs");
                    for (int k = 0; k < trsTbs.length(); k++) {
                        JSONObject jsonTrs = trsTbs.getJSONObject(k);
                        JSONArray tds = jsonTrs.getJSONArray("tds");
                        Map<String, Object> tempMap = new HashMap<>();
                        Integer string = jsonTrs.getInt("loop");
                        if (string == 1) {
                            int line = 0;
                            for (int l = 0; l < tds.length(); l++) {
                                JSONObject jsonTds = tds.getJSONObject(l);
                                String type = jsonTds.getString("type");
                                if (type.equals("input")) {
                                    String value = jsonTds.getString("value");
                                    tempMap.put("Td" + line, value);
                                    line++;
                                } else if (type.equals("label")) {
                                    String value1 = jsonTds.getString("label_chn");
                                    String value2 = jsonTds.getString("label_en");
                                    tempMap.put("Td" + line, value1);
                                    line++;
                                    tempMap.put("Td" + line, value2);
                                    line++;
                                }
                            }
                            list.add(tempMap);
                        }
                        if (string == 0) {
                            for (int l = 0; l < tds.length(); l++) {
                                JSONObject jsonTds = tds.getJSONObject(l);
                                if (jsonTds.length() != 0) {
                                    String type = jsonTds.getString("type");
                                    if (type.equals("input")) {
                                        if (jsonTds.getString("filed").equals("examinationTo")) {
                                            String value = jsonTds.getString("value");
                                            datas.put("examinationTo", value);
                                        }
                                        if (jsonTds.getString("filed").equals("commissionNo")) {
                                            String value = jsonTds.getString("value");
                                            datas.put("commissionNo", value);
                                        }
                                        if (jsonTds.getString("filed").equals("reportNo")) {
                                            String value = jsonTds.getString("value");
                                            datas.put("reportNo", value);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (ndeReportInfo.getSketch() != null && ndeReportInfo.getSketch() != "") {
                try {
                    File imageFile = new File(ndeReportInfo.getSketch());
                    datas.put("myImage",
                            FreemarkeExportrWordUtil.getImageBase64String(imageFile));
                } catch (
                        IOException e) {
                    e.printStackTrace();
                }
            }
            FreemarkeExportrWordUtil.doExport(templateFileName, fileName, datas);
            try {
                File file2 = new File(pathPdf);
                if (!file2.exists()) {
                    file2.mkdir();
                }
                String doc = exportUtil.realWord(fileName,
                        pathWord + ndeReportInfo.getInfoName() + ".doc");
                String pdf = exportUtil.wordTurnPdf(doc, pathPdf + ndeReportInfo.getInfoName() +
                        ".pdf");
                File fileDelete = new File(fileName);
                fileDelete.delete();
                File fileDoc = new File(doc);
                fileDoc.delete();
                fileDelete.delete();
                return RestResultUtil.genSuccessResult(httpPath + pdf.replaceAll("E:", ""));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RestResultUtil.failed("導出失敗");
    }

    @ApiOperation(value = "圖片導出")
    @RequestMapping(value = "getExportJPG", method = RequestMethod.GET)
    public RestResult<String> getExportJPG(@RequestParam(value = "infoId") String infoId) {
        try {
            NdeReportInfo ndeReportInfo = ndeReportInfoMapper.selectById(infoId);
            String reportParmeter = ndeReportInfo.getReportParmeter();
            // 模板文件名
            String templateFileName = "templates/4-18.ftl";
            // 要生成的文件 全路徑文件名E:\learn\SecurityTest\src\main\resources\importWord
            File file1 = new File(pathWord);
            if (!file1.exists()) {
                file1.mkdir();
            }
            String fileName =
                    pathWord + new SimpleDateFormat("yyyyMMDDHHmmss").format(new Date()) + ".doc";
            JSONArray jsonArray = new JSONArray(reportParmeter);
            Map<String, Object> datas = new HashMap<>();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                JSONArray tables = jsonObject.getJSONArray("tables");
                List<Map<String, Object>> list = new ArrayList<>();
                datas.put("table" + i, list);
                for (int j = 0; j < tables.length(); j++) {
                    JSONObject jsonObject1 = tables.getJSONObject(j);
                    JSONObject tbs = jsonObject1.getJSONObject("tbody");
                    JSONArray trsTbs = tbs.getJSONArray("trs");
                    for (int k = 0; k < trsTbs.length(); k++) {
                        JSONObject jsonTrs = trsTbs.getJSONObject(k);
                        JSONArray tds = jsonTrs.getJSONArray("tds");
                        Map<String, Object> tempMap = new HashMap<>();
                        Integer string = jsonTrs.getInt("loop");
                        if (string == 1) {
                            int line = 0;
                            for (int l = 0; l < tds.length(); l++) {
                                JSONObject jsonTds = tds.getJSONObject(l);
                                String type = jsonTds.getString("type");
                                if (type.equals("input")) {
                                    String value = jsonTds.getString("value");
                                    tempMap.put("Td" + line, value);
                                    line++;
                                } else if (type.equals("label")) {
                                    String value1 = jsonTds.getString("label_chn");
                                    String value2 = jsonTds.getString("label_en");
                                    tempMap.put("Td" + line, value1);
                                    line++;
                                    tempMap.put("Td" + line, value2);
                                    line++;
                                }
                            }
                            list.add(tempMap);
                        }
                        if (string == 0) {
                            for (int l = 0; l < tds.length(); l++) {
                                JSONObject jsonTds = tds.getJSONObject(l);
                                if (jsonTds.length() != 0) {
                                    String type = jsonTds.getString("type");
                                    if (type.equals("input")) {
                                        if (jsonTds.getString("filed").equals("examinationTo")) {
                                            String value = jsonTds.getString("value");
                                            datas.put("examinationTo", value);
                                        }
                                        if (jsonTds.getString("filed").equals("commissionNo")) {
                                            String value = jsonTds.getString("value");
                                            datas.put("commissionNo", value);
                                        }
                                        if (jsonTds.getString("filed").equals("reportNo")) {
                                            String value = jsonTds.getString("value");
                                            datas.put("reportNo", value);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (ndeReportInfo.getSketch() != null && ndeReportInfo.getSketch() != "") {
                try {
                    File imageFile = new File(ndeReportInfo.getSketch());
                    datas.put("myImage",
                            FreemarkeExportrWordUtil.getImageBase64String(imageFile));
                } catch (
                        IOException e) {
                    e.printStackTrace();
                }
            }
            FreemarkeExportrWordUtil.doExport(templateFileName, fileName, datas);
            try {
                File file2 = new File(pathPdf);
                if (!file2.exists()) {
                    file2.mkdir();
                }
                File file3 = new File(pathJpg);
                if (!file3.exists()) {
                    file3.mkdir();
                }
                String doc = exportUtil.realWord(fileName,
                        pathWord + ndeReportInfo.getInfoName() + ".doc");
                String pdf = exportUtil.wordTurnPdf(doc, pathPdf + ndeReportInfo.getInfoName() +
                        ".pdf");
                String jpg = exportUtil.pdf2multiImage(pdf, pathJpg + ndeReportInfo.getInfoName() +
                        ".jpg");
                File fileDoc = new File(fileName);
                File Doc = new File(doc);
                File filePdf = new File(pdf);
                Doc.delete();
                fileDoc.delete();
                filePdf.delete();
                return RestResultUtil.genSuccessResult(httpPath + jpg.replaceAll("E:", ""));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RestResultUtil.failed("導出失敗");
    }
}


初學者,大神莫怪。互相學習

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章