poi根據模版生成多頁word,並壓縮下載


前端時間公司有個項目,需求大致是這樣的——根據word模版,生成帶學生照片的信息表格。如果是批量打印,則生成一個word文檔,每個學生占用一頁。

在實現時,參考了兩位老哥的代碼:

使用poi根據模版生成word文檔,支持插入數據和圖片

poi替換word模板內容 並且合並生成多頁的word 實現分頁

先上工具類的代碼:

import org.apache.poi.xwpf.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordUtils {
    /**
     * 根據模板生成word
     *
     * @param path      模板的路徑
     * @param params    需要替換的參數
     * @param tableList 需要插入的參數
     * @param fileName  生成word文件的文件名
     * @param response
     */
    public void getWord(String path, Map<String, Object> params, List<String[]> tableList, String fileName, HttpServletResponse response) throws Exception {
        File file = new File(path);
        InputStream is = new FileInputStream(file);
        CustomXWPFDocument doc = new CustomXWPFDocument(is);
        this.replaceInPara(doc, doc, params);    //替換文本里面的變量
        this.replaceInTable(doc, doc, params, tableList); //替換表格里面的變量
        OutputStream os = response.getOutputStream();
        response.setHeader("Content-disposition", "attachment; filename=" + fileName);
        doc.write(os);
        this.close(os);
        this.close(is);
    }

    /**
     * 替換段落里面的變量
     *
     * @param doc    要替換的文檔
     * @param params 參數
     */
    public void replaceInPara(CustomXWPFDocument firstDoc, CustomXWPFDocument doc, Map<String, Object> params) {
        Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
        XWPFParagraph para;
        while (iterator.hasNext()) {
            para = iterator.next();
            this.replaceInPara(firstDoc, para, params, doc);
        }
    }

    /**
     * 替換段落里面的變量
     *
     * @param para   要替換的段落
     * @param params 參數
     */
    private void replaceInPara(CustomXWPFDocument firstDoc, XWPFParagraph para, Map<String, Object> params, CustomXWPFDocument doc) {
        List<XWPFRun> runs;
        Matcher matcher;
        if (this.matcher(para.getParagraphText()).find()) {
            runs = para.getRuns();
            int start = -1;
            int end = -1;
            String str = "";
            for (int i = 0; i < runs.size(); i++) {
                XWPFRun run = runs.get(i);
                String runText = run.toString();
                if ('$' == runText.charAt(0) && '{' == runText.charAt(1)) {
                    start = i;
                }
                if ((start != -1)) {
                    str += runText;
                }
                if ('}' == runText.charAt(runText.length() - 1)) {
                    if (start != -1) {
                        end = i;
                        break;
                    }
                }
            }

            for (int i = start; i <= end; i++) {
                para.removeRun(i);
                i--;
                end--;
            }

            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String key = entry.getKey();
                if (str.indexOf(key) != -1) {
                    Object value = entry.getValue();
                    if (value instanceof String) {
                        str = str.replace(key, value.toString());
                        para.createRun().setText(str, 0);
                        break;
                    } else if (value instanceof Map) {
                        str = str.replace(key, "");
                        Map pic = (Map) value;
                        int width = Integer.parseInt(pic.get("width").toString());
                        int height = Integer.parseInt(pic.get("height").toString());
                        int picType = getPictureType(pic.get("type").toString());
                        byte[] byteArray = (byte[]) pic.get("content");
                        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(byteArray);
                        try {
                            String ind = firstDoc.addPictureData(byteInputStream, picType);
                            int id = firstDoc.getNextPicNameNumber(picType);
                            firstDoc.createPicture(ind, id, width, height, para);
                            // 由於圖片重復則顯示錯誤,重寫create方法
                            // firstDoc.addPictureData(byteInputStream, picType);
                            // firstDoc.createPicture(firstDoc.getAllPictures().size() - 1, width, height, para, firstDoc);
                            para.createRun().setText(str, 0);
                            break;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }


    /**
     * 為表格插入數據,行數不夠添加新行
     *
     * @param table     需要插入數據的表格
     * @param tableList 插入數據集合
     */
    private static void insertTable(XWPFTable table, List<String[]> tableList) {
        //創建行,根據需要插入的數據添加新行,不處理表頭
        for (int i = 0; i < tableList.size(); i++) {
            XWPFTableRow row = table.createRow();
        }
        //遍歷表格插入數據
        List<XWPFTableRow> rows = table.getRows();
        int length = table.getRows().size();
        for (int i = 1; i < length - 1; i++) {
            XWPFTableRow newRow = table.getRow(i);
            List<XWPFTableCell> cells = newRow.getTableCells();
            for (int j = 0; j < cells.size(); j++) {
                XWPFTableCell cell = cells.get(j);
                String s = tableList.get(i - 1)[j];
                cell.setText(s);
            }
        }
    }

    /**
     * 替換表格里面的變量
     *
     * @param doc    要替換的文檔
     * @param params 參數
     */
    public void replaceInTable(CustomXWPFDocument firstDoc, CustomXWPFDocument doc, Map<String, Object> params, List<String[]> tableList) {
        Iterator<XWPFTable> iterator = doc.getTablesIterator();
        XWPFTable table;
        List<XWPFTableRow> rows;
        List<XWPFTableCell> cells;
        List<XWPFParagraph> paras;
        while (iterator.hasNext()) {
            table = iterator.next();
            if (table.getRows().size() > 1) {
                //判斷表格是需要替換還是需要插入,判斷邏輯有$為替換,表格無$為插入
                if (this.matcher(table.getText()).find()) {
                    rows = table.getRows();
                    for (XWPFTableRow row : rows) {
                        cells = row.getTableCells();
                        for (XWPFTableCell cell : cells) {
                            paras = cell.getParagraphs();
                            for (XWPFParagraph para : paras) {
                                this.replaceInPara(firstDoc, para, params, doc);
                            }
                        }
                    }
                } else {
                    insertTable(table, tableList);  //插入數據
                }
            }
        }
    }

    /**
     * 正則匹配字符串
     *
     * @param str
     * @return
     */
    private Matcher matcher(String str) {
        Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        return matcher;
    }

    /**
     * 根據圖片類型,取得對應的圖片類型代碼
     *
     * @param picType
     * @return int
     */
    public int getPictureType(String picType) {
        int res = CustomXWPFDocument.PICTURE_TYPE_PICT;
        if (picType != null) {
            if (picType.equalsIgnoreCase("png")) {
                res = CustomXWPFDocument.PICTURE_TYPE_PNG;
            } else if (picType.equalsIgnoreCase("dib")) {
                res = CustomXWPFDocument.PICTURE_TYPE_DIB;
            } else if (picType.equalsIgnoreCase("emf")) {
                res = CustomXWPFDocument.PICTURE_TYPE_EMF;
            } else if (picType.equalsIgnoreCase("jpg") || picType.equalsIgnoreCase("jpeg")) {
                res = CustomXWPFDocument.PICTURE_TYPE_JPEG;
            } else if (picType.equalsIgnoreCase("wmf")) {
                res = CustomXWPFDocument.PICTURE_TYPE_WMF;
            }
        }
        return res;
    }

    /**
     * 將輸入流中的數據寫入字節數組
     *
     * @param in
     * @return
     */
    public static byte[] inputStream2ByteArray(InputStream in, boolean isClose) {
        byte[] byteArray = null;
        try {
            int total = in.available();
            byteArray = new byte[total];
            in.read(byteArray);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (isClose) {
                try {
                    in.close();
                } catch (Exception e2) {
                    e2.getStackTrace();
                }
            }
        }
        return byteArray;
    }

    /**
     * 關閉輸入流
     *
     * @param is
     */
    public void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 關閉輸出流
     *
     * @param os
     */
    public void close(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @Description: 保存圖片(用來插入)
     * @Param:
     * @return:
     * @Author: lgc
     * @Date: 2019/4/24
     */
    public byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //創建一個Buffer字符串
        byte[] buffer = new byte[1024];
        //每次讀取的字符串長度,如果為-1,代表全部讀取完畢
        int len = 0;
        //使用一個輸入流從buffer里把數據讀取出來
        while ((len = inStream.read(buffer)) != -1) {
            //用輸出流往buffer里寫入數據,中間參數代表從哪個位置開始讀,len代表讀取的長度
            outStream.write(buffer, 0, len);
        }
        //關閉輸入流
        inStream.close();
        //把outStream里的數據寫入內存
        return outStream.toByteArray();
    }
}

下面是很多人都提到的poi無法顯示bug的解決:

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlToken;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;

import java.io.IOException;
import java.io.InputStream;

public class CustomXWPFDocument extends XWPFDocument {

    public CustomXWPFDocument(InputStream in) throws IOException {
        super(in);
    }

    public CustomXWPFDocument() {
        super();
    }

    public CustomXWPFDocument(OPCPackage pkg) throws IOException {
        super(pkg);
    }

    /**
     * @param id
     * @param width     寬
     * @param height    高
     * @param paragraph 段落
     */
    public void createPicture(int id, int width, int height, XWPFParagraph paragraph, CustomXWPFDocument doc) {
        final int EMU = 9525;
        width *= EMU;
        height *= EMU;
        String blipId = doc.getAllPictures().get(id).getPackageRelationship().getId();
        CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline();
        String picXml = ""
                + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"
                + "   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
                + "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
                + "         <pic:nvPicPr>" + "            <pic:cNvPr id=\""
                + id
                + "\" name=\"Generated\"/>"
                + "            <pic:cNvPicPr/>"
                + "         </pic:nvPicPr>"
                + "         <pic:blipFill>"
                + "            <a:blip r:embed=\""
                + blipId
                + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"
                + "            <a:stretch>"
                + "               <a:fillRect/>"
                + "            </a:stretch>"
                + "         </pic:blipFill>"
                + "         <pic:spPr>"
                + "            <a:xfrm>"
                + "               <a:off x=\"0\" y=\"0\"/>"
                + "               <a:ext cx=\""
                + width
                + "\" cy=\""
                + height
                + "\"/>"
                + "            </a:xfrm>"
                + "            <a:prstGeom prst=\"rect\">"
                + "               <a:avLst/>"
                + "            </a:prstGeom>"
                + "         </pic:spPr>"
                + "      </pic:pic>"
                + "   </a:graphicData>" + "</a:graphic>";

        inline.addNewGraphic().addNewGraphicData();
        XmlToken xmlToken = null;
        try {
            xmlToken = XmlToken.Factory.parse(picXml);
        } catch (XmlException xe) {
            xe.printStackTrace();
        }
        inline.set(xmlToken);

        CTPositiveSize2D extent = inline.addNewExtent();
        extent.setCx(width);
        extent.setCy(height);

        CTNonVisualDrawingProps docPr = inline.addNewDocPr();
        docPr.setId(id);
        docPr.setName("圖片" + blipId);
        docPr.setDescr("頭像");
    }

    public void createPicture(String blipId, int id, int width, int height, XWPFParagraph paragraph) {
        final int EMU = 9525;
        width *= EMU;
        height *= EMU;
        //String blipId = getAllPictures().get(id).getPackageRelationship().getId();
        CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline();

        String picXml = "" +
                "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
                "   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
                "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
                "         <pic:nvPicPr>" +
                "            <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>" +
                "            <pic:cNvPicPr/>" +
                "         </pic:nvPicPr>" +
                "         <pic:blipFill>" +
                "            <a:blip r:embed=\"" + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +
                "            <a:stretch>" +
                "               <a:fillRect/>" +
                "            </a:stretch>" +
                "         </pic:blipFill>" +
                "         <pic:spPr>" +
                "            <a:xfrm>" +
                "               <a:off x=\"0\" y=\"0\"/>" +
                "               <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" +
                "            </a:xfrm>" +
                "            <a:prstGeom prst=\"rect\">" +
                "               <a:avLst/>" +
                "            </a:prstGeom>" +
                "         </pic:spPr>" +
                "      </pic:pic>" +
                "   </a:graphicData>" +
                "</a:graphic>";

        inline.addNewGraphic().addNewGraphicData();
        XmlToken xmlToken = null;
        try {
            xmlToken = XmlToken.Factory.parse(picXml);
        } catch (XmlException xe) {
            xe.printStackTrace();
        }
        inline.set(xmlToken);

        CTPositiveSize2D extent = inline.addNewExtent();
        extent.setCx(width);
        extent.setCy(height);

        CTNonVisualDrawingProps docPr = inline.addNewDocPr();
        docPr.setId(id);
        docPr.setName("圖片" + id);
        docPr.setDescr("頭像");
    }
}

最后是我自己的實現類代碼:

import com.edu.model.InterviewRoomPlay;
import com.edu.model.InterviewRoomUser;
import com.edu.service.EduInterviewService;
import com.yz.controller.BaseController;
import com.yz.service.annotation.LoginRequired;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


@Controller
@RequestMapping("/exportWord")
@LoginRequired
public class ExportWordController extends BaseController {

    @Resource
    private EduInterviewService service;

    private String ImageHandle = "?imageView2/2/w/120/h/141/q/80";

    @RequestMapping("/batch")
    @ResponseBody
    public void batch(@RequestParam Integer playId, @RequestParam Integer schoolId,
                      HttpServletResponse response, HttpSession session) throws Exception {
        //創建壓縮包位置
        File fileZip = new File("/wordZip");
        if (!fileZip.exists()) {
            fileZip.mkdirs();
        }
        //獲得面試間和場次名稱
        InterviewRoomPlay play = service.getPlayById(playId);
        String wordName = play.getConfigName() + "-" + play.getName() + "的學生打印表";
        String filesPath = "/wordZip/" + new Date().getTime();
        //合並模板的個數就是每次要解析多少個當前的模板 ,那么就需要首先定義一個集合
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        List<InterviewRoomUser> userList = service.getUserByPlayId(playId);
        //我用的是2007版本以后poi對word進行解析的docx
        //首先定義一個XWPFDocument 集合 這個對象可以進行word 解析 合並 還有下載都離不開這個對象
        List<CustomXWPFDocument> xwpfDocuments = new ArrayList<>();
        //由於合並時將所有圖片都存在第一個doc里面,所以定義方便圖片傳值
        CustomXWPFDocument firstDoc = null;
        //判斷需要的參數
        Integer sex;
        Date playStartTime;
        String filePath = session.getServletContext().getRealPath("/") + "doc/interviewDoc/" + schoolId + ".docx";
        //期次我們先解析word模板 替換其中的占位符
        int index = 0;
        Integer handleTime = 1;
        for (InterviewRoomUser user : userList) {
            WordUtils wordUtils = new WordUtils();
            Map<String, Object> params = new HashMap<>();
            playStartTime = user.getPlayStartTime();
            sex = user.getSex();
            String headImage = user.getHeadImage();
            if (StringUtils.isNotEmpty(headImage)) {
                //替換的圖片
                String headimg = session.getServletContext().getRealPath("/") + "doc/interviewDoc/head.png";
                //new一個URL對象
                URL url = new URL(headImage + ImageHandle);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5 * 1000);
                InputStream inStream = conn.getInputStream();
                byte[] data = wordUtils.readInputStream(inStream);
                File imageFile = new File(headimg);
                FileOutputStream outStream = new FileOutputStream(imageFile);
                outStream.write(data);
                outStream.close();
                //圖片
                InputStream murl = new URL(headImage + ImageHandle).openStream();
                BufferedImage sourceImg = ImageIO.read(murl);
                Map<String, Object> header = new HashMap<>();
                header.put("width", sourceImg.getWidth());
                header.put("height", sourceImg.getHeight());
                header.put("type", wordUtils.getPictureType(headimg));
                header.put("content", wordUtils.inputStream2ByteArray(new FileInputStream(headimg), true));
                params.put("${headImage}", header);
            } else {
                params.put("${headImage}", "");
            }
            params.put("${name}", user.getName());
            String idcard = user.getIdcard();
            params.put("${idcard}", idcard);
            if (sex != null) {
                if (sex == 1) {
                    params.put("${sex}", "男");
                } else if (sex == 2) {
                    params.put("${sex}", "女");
                } else {
                    params.put("${sex}", "");
                }
            } else {
                if (!StringUtils.isEmpty(idcard)) {
                    params.put("${sex}", checkSex(idcard));
                } else {
                    params.put("${sex}", "");
                }
            }
            params.put("${phone}", user.getPhone());
            params.put("${fromSchool}", user.getFromSchool());
            params.put("${fromSpecialty}", user.getFromSpecialty());
            params.put("${specialtyName}", user.getSpecialtyName());
            if (playStartTime != null) {
                params.put("${playStartTime}", sdf.format(playStartTime));
            } else {
                params.put("${playStartTime}", "");
            }
            params.put("${playName}", user.getPlayName());
            //返回一個新的xwpfDocument對象
            File file = new File(filePath);
            InputStream is = new FileInputStream(file);
            CustomXWPFDocument doc = new CustomXWPFDocument(is);
            if (index == 0 || (index != 1 && index % 49 == 1)) {
                firstDoc = doc;
            }
            wordUtils.replaceInPara(firstDoc, doc, params);
            wordUtils.replaceInTable(firstDoc, doc, params, null);
            xwpfDocuments.add(doc);
            is.close();
            //每50條輸出一次
            if (userList.size() > 50 && index != 0 && (index == userList.size() - 1 || index % 49 == 0)) {
                File files = new File(filesPath);
                if (!files.exists()) {
                    files.mkdirs();
                }
                //這樣第一步將所有word內容替換之后生成多個   xwpfDocument
                //現在將多個xwpfDocument 進行合並 追加 生成word文件
                CustomXWPFDocument xwpfDocument = xwpfDocuments.get(0);
                for (int i = 0; i < xwpfDocuments.size(); i++) {
                    //每次的追加為了避免樣式和格式混亂 加上分頁符
                    //當是只有一條數據的時候 直接輸出
                    if (i == 0) {
                        xwpfDocument = xwpfDocuments.get(0);
                        continue;
                    } else {
                        //當存在多條時候
                        xwpfDocument = mergeWord(xwpfDocument, xwpfDocuments.get(i));
                    }
                }
                //合並之后返回XWPFDocument對象 寫出就可以了
                String fileName = wordName + handleTime + ".docx";
                FileOutputStream fout = new FileOutputStream(filesPath + "/" + new File(fileName));
                xwpfDocument.write(fout);
                fout.close();
                handleTime++;

                xwpfDocuments.clear();
            }
            index++;
        }
        if (userList.size() > 50) {
            createZipPath(filesPath, response, play.getConfigName() + "-" + play.getName() + "的學生打印表");
            File files = new File(filesPath);
            if (files.exists()) {
                delFolder(files.getPath());
            }
        } else {
            if (xwpfDocuments.size() > 0) {
                //這樣第一步將所有word內容替換之后生成多個   xwpfDocument
                //現在將多個xwpfDocument 進行合並 追加 生成word文件
                CustomXWPFDocument xwpfDocument = xwpfDocuments.get(0);
                for (int i = 0; i < xwpfDocuments.size(); i++) {
                    //每次的追加為了避免樣式和格式混亂 加上分頁符
                    //當是只有一條數據的時候 直接輸出
                    if (i == 0) {
                        xwpfDocument = xwpfDocuments.get(0);
                        continue;
                    } else {
                        //當存在多條時候
                        xwpfDocument = mergeWord(xwpfDocument, xwpfDocuments.get(i));
                    }
                }
                //合並之后返回XWPFDocument對象 寫出就可以了
                OutputStream os = response.getOutputStream();
                String fileName = new String((wordName + ".docx").getBytes("UTF-8"), "iso-8859-1");
                response.setHeader("Content-disposition", "attachment; filename=" + fileName);
                xwpfDocument.write(os);
                os.close();
            }
        }
    }

    //兩個對象進行追加
    public CustomXWPFDocument mergeWord(CustomXWPFDocument document, CustomXWPFDocument doucDocument2) throws Exception {
        CustomXWPFDocument src1Document = document;
        XWPFParagraph p = src1Document.createParagraph();
        //設置分頁符
        p.setPageBreak(true);
        CTBody src1Body = src1Document.getDocument().getBody();
        CustomXWPFDocument src2Document = doucDocument2;
        CTBody src2Body = src2Document.getDocument().getBody();
        XWPFParagraph p2 = src2Document.createParagraph();
        XmlOptions optionsOuter = new XmlOptions();
        optionsOuter.setSaveOuter();
        String appendString = src2Body.xmlText(optionsOuter);
        String srcString = src1Body.xmlText();
        String prefix = srcString.substring(0, srcString.indexOf(">") + 1);
        String mainPart = srcString.substring(srcString.indexOf(">") + 1, srcString.lastIndexOf("<"));
        String sufix = srcString.substring(srcString.lastIndexOf("<"));
        String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
        CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + sufix);
        src1Body.set(makeBody);
        return src1Document;
    }

    //壓縮輸出
    public static void createZipPath(String path, HttpServletResponse response, String zipName) throws IOException {
        ZipOutputStream zipOutputStream = null;
        OutputStream output = response.getOutputStream();
        response.reset();
        String fileNameZip = new String((zipName + ".zip").getBytes("UTF-8"), "iso-8859-1");
        response.setHeader("Content-disposition", "attachment; filename=" + fileNameZip);
        response.setContentType("application/msword");
        zipOutputStream = new ZipOutputStream(output);
        File[] files = new File(path).listFiles();
        FileInputStream fileInputStream = null;
        byte[] buf = new byte[1024];
        int len = 0;
        if (files != null && files.length > 0) {
            for (File wordFile : files) {
                String fileName = wordFile.getName();
                fileInputStream = new FileInputStream(wordFile);
                //放入壓縮zip包中;
                zipOutputStream.putNextEntry(new ZipEntry(fileName));
                //讀取文件;
                while ((len = fileInputStream.read(buf)) > 0) {
                    zipOutputStream.write(buf, 0, len);
                }
                //關閉;
                zipOutputStream.closeEntry();
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            }
        }

        if (zipOutputStream != null) {
            zipOutputStream.close();
        }
    }

    /***
     * 刪除文件夾
     *
     * @param folderPath 文件夾完整絕對路徑
     */
    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); // 刪除完里面所有內容
            String filePath = folderPath;
            filePath = filePath.toString();
            java.io.File myFilePath = new java.io.File(filePath);
            myFilePath.delete(); // 刪除空文件夾
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 刪除指定文件夾下所有文件
     *
     * @param path 文件夾完整絕對路徑
     * @return
     */
    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);// 先刪除文件夾里面的文件
                delFolder(path + "/" + tempList[i]);// 再刪除空文件夾
                flag = true;
            }
        }
        return flag;
    }

    public String checkSex(String idcard) {
        Integer sex = 0;
        if (idcard.length() == 15) {
            //15位身份證最后一位奇男偶女
            sex = oddOrEven(Integer.valueOf(idcard.substring(idcard.length() - 1, idcard.length())));
        } else if (idcard.length() == 18) {
            //18位身份證第17位奇男偶女
            sex = oddOrEven(Integer.valueOf(idcard.substring(idcard.length() - 2, idcard.length() - 1)));
        } else {
            return "";
        }
        if (sex == 1) {
            return "男";
        } else if (sex == 2) {
            return "女";
        }
        return "";
    }

    //判斷奇偶數
    public Integer oddOrEven(int a) {
        if ((a & 1) == 1) {
            return 1;
        } else {
            return 2;
        }
    }
}

還有就是poi的版本(版本不重要,只要不是太老的就行):

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
</dependency>

下面是一些詳細個人的代碼說明:

  由於我在多次生成時發現,poi的圖片處理是根據模版存放的。也就是說,當CustomXWPFDocument的對象不一樣時,每個對象的第一張圖片的rid都是rid8(我也沒有細究為啥是從8開始),但是由於后期我需要將多個CustomXWPFDocument整合成為一個輸出,就造成明明圖片的對象不一樣,但是由於除了第一個CustomXWPFDocument的圖片對象外,其他的圖片並沒有將數據代入第一個CustomXWPFDocument,但是圖片對應的rid又能找到對應對象,所以模版的圖片就成了同一張。所有后續拼接的圖片根據id只能在第一個中找,一旦沒找到,就會造成圖片不顯示,這也就是我為什么對每次輸出循環的第一個CustomXWPFDocument進行重復添加圖片的操作。

  而兩個createPicture的區別在於,當poi遇到完全一樣的照片時,將不會執行重復寫入圖片數據的操作,而是使用相同的rid以作對應。我所使用的那個,可以將圖片對應的rid返回,這樣就不會造成圖片錯亂。而注釋掉的那個,當發現圖片重復,返回的只有CustomXWPFDocument中最后一個圖片的信息,這樣當重復圖片不是連續時,圖片的顯示就出錯了。

  在實際操作中,我將50個及以下的操作選擇用單個word直接輸出,而50個以上則分開用zip輸出。原因在於,在實際使用時,我發現當將多個CustomXWPFDocument合並形成一個時,數據量過大會導致卡死,影響后續的輸出。在我的項目中,70個以上處理速度開始變慢翻倍,大概在76個時直接卡死。每個人可能因模版的大小及復雜程度有所不同。

  當模版中有圖片時,本地圖片可以直接獲取文件信息。鏈接圖片則需要訪問鏈接,並將其寫入本地的一個文件中,以便直接操作使用。

   以上皆為個人理解和查詢結果,歡迎批評指正~~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM