首页 > 其他分享 >poi根据模版导出多页word,带插入图片,并压缩下载

poi根据模版导出多页word,带插入图片,并压缩下载

时间:2023-06-07 10:02:58浏览次数:38  
标签:ftp srsJbzl word String 模版 param params poi new


工具类代码

package cn.edu.nfu.jw.srs.utils;

import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;

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;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

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;
    }

    public  byte[] input2byte(InputStream inStream) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[inStream.available()];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        swapStream.close();
        return in2b;
    }

    /**
     * 将输入流中的数据写入字节数组
     *
     * @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 static 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();
    }

    //两个对象进行追加
    public static 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();
        }
    }
    /**
     * 将InputStream写入本地文件
     * @param destination 写入本地目录
     * @param input 输入流
     * @throws IOException IOException
     */
    public void writeToLocal(String destination, InputStream input)
            throws IOException {
        int index;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream(destination);
        while ((index = input.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        input.close();
        downloadFile.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;
    }
}

创建图片工具类

package cn.edu.nfu.jw.srs.utils;

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("头像");
    }
}

操作ftp文件工具类

package cn.edu.nfu.jw.srs.utils;

import java.io.*;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;

import cn.edu.nfu.jw.exception.ServiceException;
import cn.edu.nfu.jw.srs.domain.SrsJbzl;
import cn.edu.nfu.jw.srs.domain.SrsPunishment;

/**
 * 创建word文档
 * 
 * @author manolin
 *
 */
public class CommonUtil {
	private static final Log log = LogFactory.getLog(CommonUtil.class);
	public static void createDocumentByTemplate(String realname, HttpServletResponse response,
			String templeteName, SrsPunishment info) {
		
		OutputStream outp = null;
		InputStream in = null;
		templeteName=File.separator+"doc"+File.separator+templeteName; // 模板文件名字
		String fileName = realname; //输出文件名称
		try {
			fileName = URLEncoder.encode(fileName, "UTF-8") ;
			if(!fileName.endsWith(".doc")){
				fileName=fileName+".doc";
			}
			response.reset();
			response.setContentType("application/doc");
			response.addHeader("Content-Disposition", "attachment; filename="+fileName);
			response.setContentType("application/octet-stream;charset=UTF-8");
			outp = response.getOutputStream();
			in = CommonUtil.class.getClassLoader().getResourceAsStream(templeteName);
			//替换变量
			HWPFDocument doc = new HWPFDocument(in);
			Range range = doc.getRange();
			range.replaceText("${name}", info.getSrsJbzl().getXm());
			range.replaceText("${punishment}", info.getPunishment());
			range.replaceText("${gender}",getGender(info.getSrsJbzl()));
			range.replaceText("${xh}",info.getSrsJbzl().getXh());
			range.replaceText("${yxmc}",info.getSrsJbzl().getFesYx().getYxmc());
			range.replaceText("${zymc}",info.getSrsJbzl().getFesZy().getZymc());
			range.replaceText("${rxnj}",info.getSrsJbzl().getRxnj().toString());
			range.replaceText("${cheatTime}",formatDate(info.getCheatTime()));
			range.replaceText("${cheatSubject}",info.getCheatSubject());
			range.replaceText("${cheatDetail}",info.getCheatDetail());
			range.replaceText("${rule}",info.getRules());
			range.replaceText("${cTime}",formatDate(info.getcTime()));
			ByteArrayOutputStream ostream = new ByteArrayOutputStream();
			doc.write(ostream);
			outp.write(ostream.toByteArray());
			outp.flush();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("occur exception on get file template: "  + e.getLocalizedMessage());
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (outp != null) {
					outp.close();
				}
			} catch (Exception e) {
				log.error("occur exception on closing stream: "  + e.getLocalizedMessage());
			}
		}
	}
	private static String getGender(SrsJbzl srsJbzl){
		String gender="";
		if(srsJbzl.getXbdm().equals("1")){
			gender="男";
		}else if(srsJbzl.getXbdm().equals("2")){
			gender="女";
		}else{
			log.error("gender data error! student id="+srsJbzl.getId()); 
			throw new ServiceException(-1,"学生性别数据异常!");
		}
		return gender;
	}
	private static String formatDate(Date date){
		DateFormat dft=new SimpleDateFormat("yyyy年MM月dd日");
		return dft.format(date);
	}

	/**
	 * 上传文件
	 * @param file
	 * @param filePath
	 * @param fileName
	 */
	public static void uploadFile(byte[] file, String filePath, String fileName) {
		File targetFile = new File(filePath);
		if(!targetFile.exists()){
			targetFile.mkdirs();
		}
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(filePath + fileName);
			out.write(file);
			out.flush();
		} catch (Exception e) {

		} finally {
			try {
				if (out != null)
					out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 上传文件到ftp服务器
	 * @param host
	 * @param port
	 * @param username
	 * @param pwd
	 * @param path
	 * @param local
	 * @param filename
	 */
	public static boolean uploadFile(String host, int port, String username, String pwd,
									 String path, InputStream local, String filename){
		boolean result = false;
		//创建客户端对象
		FTPClient ftp = new FTPClient();
		try {
			//连接ftp服务器
			ftp.connect(host, port);
			//登录
			ftp.login(username, pwd);
			//检查上传路径是否存在 如果不存在返回false
			boolean flag = ftp.changeWorkingDirectory(path);
			if(!flag){
				//创建上传的路径  该方法只能创建一级目录,在这里如果/home/ftpuser存在则可创建image
				ftp.makeDirectory(path);
			}
			//指定编码
			//ftp.setControlEncoding("GBK");
			//防止数据链路不通
			ftp.enterLocalPassiveMode();
			//指定上传路径
			ftp.changeWorkingDirectory(path);
			//指定上传文件的类型  二进制文件
			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
			//第一个参数是文件名
			ftp.storeFile(new String(filename.getBytes(),"iso-8859-1"), local);
			result = true;
		} catch (IOException e) {
			log.error("occur exception on uploading green channel file to ftp server : " + e.getLocalizedMessage());
			e.printStackTrace();
		} finally {
			try {
				//关闭文件流
				local.close();
				//退出
				ftp.logout();
				//断开连接
				ftp.disconnect();
			} catch (IOException e) {
				log.error("occur exception on close stream on uploading green channel file to ftp server : " + e.getLocalizedMessage());
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 返回流
	 * @param host
	 * @param port
	 * @param username
	 * @param pwd
	 * @param path
	 * @param response
	 * @param ftpfilename
	 * @return
	 */
	public static boolean getFileIo(String host, int port, String username, String pwd,
									String path, HttpServletResponse response, String ftpfilename){
		boolean result = false;
		//创建客户端对象
		FTPClient ftp = new FTPClient();
		try {
			//连接ftp服务器
			ftp.connect(host, port);
			//登录
			ftp.login(username, pwd);
			//检查上传路径是否存在 如果不存在返回false
			boolean flag = ftp.changeWorkingDirectory(path);
			if (!flag) {
				//创建上传的路径  该方法只能创建一级目录,在这里如果/home/ftpuser存在则可创建image
				ftp.makeDirectory(path);
			}
			//指定编码
			//ftp.setControlEncoding("GBK");
			//防止数据链路不通
			ftp.enterLocalPassiveMode();
			//切换目录
			ftp.changeWorkingDirectory(path);
			//指定上传文件的类型  二进制文件
			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
			//设置文件返回类型
			response.setContentType("image/*");
			response.addHeader("Content-Disposition", "attachment; filename="+ftpfilename+".png");
			result = ftp.retrieveFile(new String(ftpfilename.getBytes(),"iso-8859-1"), response.getOutputStream());
		} catch (IOException e) {
			log.error("occur exception on get ftp file io from ftp server : " + e.getLocalizedMessage());
			e.printStackTrace();
		} finally {
			try {
				//退出
				ftp.logout();
				//断开连接
				ftp.disconnect();
			} catch (IOException e) {
				log.error("occur exception on close stream on get ftp file io from ftp server : " + e.getLocalizedMessage());
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 获取文件输入流
	 * @param host
	 * @param port
	 * @param username
	 * @param pwd
	 * @param path
	 * @param ftpfilename
	 * @return
	 */
	public static InputStream  getFileInputStream(String host, int port, String username, String pwd,
									String path, String ftpfilename){
		//创建客户端对象
		FTPClient ftp = new FTPClient();
		InputStream inputStream = null;
		try {
			//连接ftp服务器
			ftp.connect(host, port);
			//登录
			ftp.login(username, pwd);
			//检查上传路径是否存在 如果不存在返回false
			boolean flag = ftp.changeWorkingDirectory(path);
			if (!flag) {
				//创建上传的路径  该方法只能创建一级目录,在这里如果/home/ftpuser存在则可创建image
				ftp.makeDirectory(path);
			}
			//指定编码
			//ftp.setControlEncoding("GBK");
			//防止数据链路不通
			ftp.enterLocalPassiveMode();
			//切换目录
			ftp.changeWorkingDirectory(path);
			//指定上传文件的类型  二进制文件
			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
			//设置文件返回类型
			inputStream = ftp.retrieveFileStream(new String(ftpfilename.getBytes(), "iso-8859-1"));
		} catch (IOException e) {
			log.error("occur exception on get ftp file io from ftp server : " + e.getLocalizedMessage());
			e.printStackTrace();
		} finally {
			try {
				//退出
				ftp.logout();
				//断开连接
				ftp.disconnect();
			} catch (IOException e) {
				log.error("occur exception on close stream on get ftp file io from ftp server : " + e.getLocalizedMessage());
				e.printStackTrace();
			}
		}
		return inputStream;
	}

	/**
	 * 从ftp服务器上删除文件
	 * @param host
	 * @param port
	 * @param username
	 * @param pwd
	 * @param path
	 * @param ftpfilename
	 * @return
	 */
	public static boolean delFile(String host, int port, String username, String pwd,
								  String path, String ftpfilename){
		boolean result = false;
		//创建客户端对象
		FTPClient ftp = new FTPClient();
		try {
			//连接ftp服务器
			ftp.connect(host, port);
			//登录
			ftp.login(username, pwd);
			//检查上传路径是否存在 如果不存在返回false
			boolean flag = ftp.changeWorkingDirectory(path);
			if (!flag) {
				//创建上传的路径  该方法只能创建一级目录,在这里如果/home/ftpuser存在则可创建image
				ftp.makeDirectory(path);
			}
			//指定编码
			//ftp.setControlEncoding("GBK");
			//防止数据链路不通
			ftp.enterLocalPassiveMode();
			//指定上传路径
			ftp.changeWorkingDirectory(path);
			//指定上传文件的类型  二进制文件
			ftp.deleteFile(ftpfilename);
			result = true;
		} catch (IOException e) {
			log.error("occur exception on deletting green channel file from ftp server : " + e.getLocalizedMessage());
			e.printStackTrace();
		} finally {
			try {
				//退出
				ftp.logout();
				//断开连接
				ftp.disconnect();
			} catch (IOException e) {
				log.error("occur exception on close stream on deletting green channel file from ftp server : " + e.getLocalizedMessage());
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 下载模板信息
	 * 适用于windows和linux
	 * @param response
	 * @param request
	 * @param templeteName
	 * @throws IOException
	 */
	public static void downloadTemplate(String realname, HttpServletResponse response, HttpServletRequest request, String templeteName) {
		OutputStream outp = null;
		InputStream in = null;
		try {
			String fileName = templeteName; //要下载的模板文件
			if(templeteName!=null){
				if(!templeteName.endsWith(".doc")){
					fileName = templeteName + ".doc";
				}
			}
			//String filedownload = CommonUtil.class.getClassLoader().getResource("").getPath();
			//filedownload += fileName;
			fileName = URLEncoder.encode(fileName, "UTF-8") ;
			// 要下载的模板所在的绝对路径
			response.reset();
			response.addHeader("Content-Disposition", "attachment; filename="+fileName);
			response.setContentType("application/octet-stream;charset=UTF-8");
			outp = response.getOutputStream();
			in = cn.edu.nfu.jw.srs.utils.CommonUtil.class.getClassLoader().getResourceAsStream(realname);
			//new File(ResourceUtils.getURL("classpath:").getPath());
			//in = new FileInputStream(filedownload);
			byte[] b = new byte[1024];
			int i;
			while ((i = in.read(b)) > 0) {
				outp.write(b, 0, i);
			}
			outp.flush();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("occur exception on get file template: "  + e.getLocalizedMessage());
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (outp != null) {
					outp.close();
				}
			} catch (Exception e) {
				log.error("occur exception on closing stream: "  + e.getLocalizedMessage());
			}
		}
	}
}

自己实现的业务代码

public void batchPrintXjxx(HttpServletResponse response, SrsJbzl obj) {
        obj.setPageSize(Integer.MAX_VALUE);
        obj.setStart(0);
        List<SrsJbzl> userList = dao.listPage(obj);

        //创建压缩包位置
        File fileZip = new File("/wordZip");
        if (!fileZip.exists()) {
            fileZip.mkdirs();
        }
        //到处的登记表名称
        String wordName = "中山大学南方学院本科生学籍登记表";
        String filesPath = "/wordZip/" + new Date().getTime();
        //合并模板的个数就是每次要解析多少个当前的模板 ,那么就需要首先定义一个集合
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //我用的是2007版本以后poi对word进行解析的docx
        //首先定义一个XWPFDocument 集合 这个对象可以进行word 解析 合并 还有下载都离不开这个对象
        List<CustomXWPFDocument> xwpfDocuments = new ArrayList<>();
        //由于合并时将所有图片都存在第一个doc里面,所以定义方便图片传值
        CustomXWPFDocument firstDoc = null;
        //判断需要的参数
        Integer sex;
        Date playStartTime;
        List<Map<String, Object>> allZzmm = xsjbxxDao.getAllZzmm();
        String filePath = ResourceUtils.CLASSPATH_URL_PREFIX + "中山大学南方学院本科生学籍登记表" + ".docx";
        //期次我们先解析word模板 替换其中的占位符
        int index = 0;
        Integer handleTime = 1;
        try {
            for (SrsJbzl srsJbzl : userList) {
                WordUtils wordUtils = new WordUtils();
                Map<String, Object> params = new HashMap<>();
                //从ftp下载图片
                InputStream fileInputStream = CommonUtil.getFileInputStream(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_BASEPATH, srsJbzl.getXh());
                if (fileInputStream != null) {
                    Map<String, Object> header = new HashMap<>();
                    header.put("width", 100);
                    header.put("height", 150);
                    header.put("type", "jpg");
                    byte[] bytes = wordUtils.input2byte(fileInputStream);
//                    byte[] bytes1 = WordUtils.inputStream2ByteArray(fileInputStream, true);
                    header.put("content", bytes);
                    params.put("${headImage}", header);
                } else {
                    params.put("${headImage}", "");
                }
                params.put("${xm}", srsJbzl.getXm());
                params.put("${cym}", srsJbzl.getCym());
                params.put("${ywxm}", srsJbzl.getYwxm());
                params.put("${xh}", srsJbzl.getXh());
                params.put("${xb}", "1".equals(srsJbzl.getXbdm()) ? "男" : "2".equals(srsJbzl.getXbdm()) ? "女" : "");
               

# params.put("${jkzk}", srsJbzl.getSrsJkzkdm()==null?"":srsJbzl.getSrsJkzkdm().getJkzkmc());//健康状况 params.put("${sznj}", srsJbzl.getSznj() == null ? "" : srsJbzl.getSznj().toString());//年级 params.put("${csrq}", srsJbzl.getCsrq() ==null ?"":sdf.format(srsJbzl.getCsrq()));//出生时间 params.put("${gj}", srsJbzl.getFesDqGjdm() == null ? "":srsJbzl.getFesDqGjdm().getGjmc());//国籍 params.put("${mz}", srsJbzl.getFesMzdm() == null ? "":srsJbzl.getFesMzdm().getMzmc());//民族 params.put("${hf}", srsJbzl.getFesHyzkdm() == null?"":srsJbzl.getFesHyzkdm().getHyzkmc());//婚否 params.put("${cc}", srsJbzl.getCc());//层次 //to String zzmmc = ""; for (Map<String, Object> stringObjectMap : allZzmm) { if((Long)stringObjectMap.get("ID")==srsJbzl.getZzmmdm().getId().longValue()){ zzmmc = (String)stringObjectMap.get("ZZMMMC"); } } params.put("${zzmm}", zzmmc);//政治面貌 params.put("${jtcs}", srsJbzl.getSrsJtcsdm()==null?"":srsJbzl.getSrsJtcsdm().getJtcsMc());//家庭出身 params.put("${jg}", srsJbzl.getJgFesDqXsdm()==null?"":srsJbzl.getJgFesDqXsdm().getXsmc());//籍贯 params.put("${csd}", srsJbzl.getHkszd());//出生地 params.put("${lxdh}", srsJbzl.getYxlxdh());//联系电话 params.put("${sfzh}", srsJbzl.getSfzjh());//身份证号 params.put("${email}", srsJbzl.getEmail());//E-mail params.put("${yxmc}", srsJbzl.getFesYx()==null?"":srsJbzl.getFesYx().getYxmc());//系别 params.put("${zymc}", srsJbzl.getFesZy()==null?"":srsJbzl.getFesZy().getZymc());//专业 params.put("${rtsjdd}", srsJbzl.getRtsjdd());//何时何地加入共青团 params.put("${rdsjdd}", srsJbzl.getRdsjdd());//何时何地加入何党派 params.put("${txmxdz}", srsJbzl.getTxmxdz());//通讯地址 params.put("${hjdqdm1}", srsJbzl.getHjdqdm());//邮编 params.put("${hjdqdm2}", srsJbzl.getHjdqdm());//邮编 params.put("${jtzz}", srsJbzl.getJtzz());//家庭地址 params.put("${jtdh}", srsJbzl.getJtdh());//家庭电话 params.put("${chcddz}", srsJbzl.getChcddz());//乘火车到达站 params.put("${rxsj}", srsJbzl.getRxny()==null?"":sdf.format(srsJbzl.getRxny()));//入学时间 params.put("${rxfs}",srsJbzl.getSrsRxfsdm()==null?"":srsJbzl.getSrsRxfsdm().getRxfsmc());//入学方式 params.put("${rxqxl}", srsJbzl.getFesRxsfdm()==null?"":srsJbzl.getFesRxsfdm().getRxsfmc());//入学前学历 //查宿舍信息 String shByXsid = xsjbxxDao.getShByXsid(srsJbzl.getId()); params.put("${xxss}", shByXsid);//学校宿舍 params.put("${hq}", srsJbzl.getYnHq()!=null&&srsJbzl.getYnHq() ? "是" : "否");//是否华侨 params.put("${ga}", srsJbzl.getYnHq()!=null&&srsJbzl.getYnGas() ? "是" : "否");//是否港澳生 params.put("${qj}", srsJbzl.getYnHq()!=null&&srsJbzl.getYnQj() ? "是" : "否");//是否侨眷 List<SrsGzjlzl> srsGzjlzls = gzjlzlDao.listByMySelf(srsJbzl); List<SrsJtzl> srsJtzls = jtzlDao.listByMySelf(srsJbzl); if(srsGzjlzls!=null && srsGzjlzls.size()>0){ for (int i=0; i<srsGzjlzls.size() ;i++) { if (i==3) break;//个人简历不超过3条 String qsrq = sdf.format(srsGzjlzls.get(i).getQsrq()); String jsrq = sdf.format(srsGzjlzls.get(i).getQsrq()); params.put("${qzny"+(i+1)+"}", qsrq+"到"+jsrq); params.put("${szdw"+(i+1)+"}", srsGzjlzls.get(i).getSzdw()); params.put("${zw"+(i+1)+"}", srsGzjlzls.get(i).getZw()); params.put("${zmr"+(i+1)+"}",srsGzjlzls.get(i).getZmr()); } } if(srsJtzls!=null && srsJtzls.size()>0){ for (int i=0; i<srsJtzls.size() ;i++) { if (i==2) break;//监护人不超过2条 params.put("${cyxm"+(i+1)+"}", srsJtzls.get(i).getCyxm()); params.put("${ybrgx"+(i+1)+"}", srsJtzls.get(i).getYbrgx()); params.put("${sflx"+(i+1)+"}", srsJtzls.get(i).getSflx()); params.put("${cysrzh"+(i+1)+"}",srsJtzls.get(i).getCysrzh()); params.put("${lxdh"+(i+1)+"}",srsJtzls.get(i).getLxdh()); } } //学籍异动日志 Page page = new Page(); page.setCurrent(0); page.setPageSize(100); SrsRecord srsRecord = new SrsRecord(); srsRecord.setJbzl(srsJbzl); List<SrsRecord> srsRecordlist = srsRecordService.list(srsRecord, page); if(srsRecordlist!=null && srsRecordlist.size()>0){ for (int i=0; i<srsRecordlist.size() ;i++) { if (i==3) break;//最多只显示三条 params.put("${ctime"+(i+1)+"}", srsRecordlist.get(i).getCtime()!=null?sdf.format(srsRecordlist.get(i).getCtime()):""); params.put("${ydlxName"+(i+1)+"}", srsRecordlist.get(i).getYdlxName()); params.put("${reason"+(i+1)+"}", srsRecordlist.get(i).getReason()); } } params.put("${jcqk}", srsJbzl.getJcqk());//何时何地何原因受过何种奖励或处分 params.put("${ahtc}", srsJbzl.getAhtc());//有何爱好与特长 params.put("${syxwrq}", srsJbzl.getSyxwrq()!=null?sdf.format(srsJbzl.getSyxwrq()):"");//学位证书时间 params.put("${sjbysj}",srsJbzl.getSjbysj()!=null?sdf.format(srsJbzl.getSjbysj()):"");//毕业证书时间 params.put("${xwzh}", srsJbzl.getXwzh());//学位证书编号 params.put("${byzsh}", srsJbzl.getByzsh());//毕业证书编号

                //返回一个新的xwpfDocument对象
                File file = ResourceUtils.getFile(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 = WordUtils.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, "中山大学南方学院本科生学籍登记表");
                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 = WordUtils.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();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

最后查看效果图

poi根据模版导出多页word,带插入图片,并压缩下载_插入数据


标签:ftp,srsJbzl,word,String,模版,param,params,poi,new
From: https://blog.51cto.com/u_15564034/6429992

相关文章

  • 【Checkpoint】Command for log's checkpoint - SQLserver, Oracle, PostgreSQL
    文档引子最近,SQLserver环境中的SQLalwayson因事务爆满导致磁盘持续告警,通过这次事件,记载下SQLserverAG的事务日志处理的正确方式,同时也把Oracle以及PG的相关的checkpoint问题一并做个简单的总结,并且只从结果的角度给出过程,至于具体的理论,请移步官方文档查阅。SQLserver检......
  • Docker系列 | docker endpoint for “default” not found
    启动docker报错ForWindowsUsers1.Delete.dockerdirectory.WhichexistsonPATHC:\Users\your-username\.docker2.Restartdocker重新运行  ......
  • render_to_string() got an unexpected keyword argument 'context_instance'的解决方
    参考资料:render_to_string()gotanunexpectedkeywordargument‘status’TypeErrorat/post/render_to_response()gotanunexpectedkeywordargument‘context_instance’Djangoerror:render_to_response()gotanunexpectedkeywordargument‘context_instance’......
  • 第三天:环境变量搭建和运行HelloWord程序
    java开发环境搭建配置环境变量我的电脑-->右键属性点击高级系统设置选择环境变量选择系统变量,新建--》JAVA_HOME配置path变量测试是否安装成功打开cmd输入命令java-version目录含义bin:存放一些可执行程序include:JDK是由C和C++编写启动时......
  • 2_Transferring Files, Passwordless login & Managing multiple servers
     原文:https://www.codewithharry.com/blogpost/transferring-files-passwordless-login-ubuntu-20-04/ TransferringFiles,Passwordlesslogin&ManagingmultipleserversInthispost,Iwillshowyouhowtoavoidenteringpasswordsmultipletimeswhilema......
  • 2.1类神经网路训练不起来怎么办 (一):局部最小值 (local minima) 与鞍点 (saddle poin
    1.Whengradientissmall  本小节主要讨论优化器造成的训练问题.1.1CriticalPoint(临界点)  如果训练过程中经过很多个epoch后,loss还是不下降,那么可能是因为梯度(斜率)接近于0,导致参数更新的步伐接近于0,所以参数无法进一步更新,loss也就降不下去.  这时或许有很......
  • How do you display code snippets in MS Word preserving format and syntax highlig
    HowdoyoudisplaycodesnippetsinMSWordpreservingformatandsyntaxhighlighting?回答1Hereisthebestway,forme,toaddcodeinsideword:GotoInserttab,Textsection,clickObjectbutton(it'sontheright)ChooseOpenDocumentTextwhic......
  • QPointer的使用以及场景
    QPointer的使用以及场景在我们项目开发中,经常会遇到这种情况,在A中引用了B的对象,但是你却不知道B什么时候会析构,所以使用它会出现异常;所以今天的主角要登场了QPointer可以完美的解决这样的问题。先来看一下介绍 TheQPointerclassisatemplateclassthatprovidesguardedpoin......
  • word2019发布博客到博客园,解决word配置时出现的问题
    一开始按照网上的教程设置了博客URL,类似https://www.cnblogs.com/xxxxxxx/services/metaweblog.aspx这种,没有成功,再去查发现2021年之后就不支持这种URL地址了。改用设置-基本设置中的MetaWeblog访问地址,但还是提示word无法注册您的账户,也检查了用户名和密码就是登录时的用户名密......
  • aspose word与pdf互转
    asposeword与pdf互转packagecom.example.core.mydemo.aspose;importcom.aspose.words.Document;importcom.aspose.words.License;importcom.aspose.words.SaveFormat;importjava.io.ByteArrayInputStream;importjava.io.File;importjava.io.FileOutputStream;......