package cn.com.goldenwater.dcproj.utils; import org.apache.poi.ooxml.POIXMLDocument; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.xwpf.usermodel.*; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; public class OfficeUtil { /** * 根据指定的参数值、模板,生成 word 文档 * * @param param 需要替换的变量 * @param template 模板 */ public static CustomXWPFDocument generateWord(Map param, String template) { CustomXWPFDocument doc = null; try { OPCPackage pack = POIXMLDocument.openPackage(template); doc = new CustomXWPFDocument(pack); if (param != null && param.size() > 0) { //处理段落 List paragraphList = doc.getParagraphs(); processParagraphs(paragraphList, param, doc); //处理表格 Iterator it = doc.getTablesIterator(); while (it.hasNext()) { XWPFTable table = it.next(); List rows = table.getRows(); for (XWPFTableRow row : rows) { List cells = row.getTableCells(); for (XWPFTableCell cell : cells) { List paragraphListTable = cell.getParagraphs(); processParagraphs(paragraphListTable, param, doc); } } } } } catch (Exception e) { e.printStackTrace(); } return doc; } /** * 处理段落 * * @param paragraphList */ public static void processParagraphs(List paragraphList, Map param, CustomXWPFDocument doc) { if (paragraphList != null && paragraphList.size() > 0) { for (XWPFParagraph paragraph : paragraphList) { //poi转换过来的行间距过大,需要手动调整 if (paragraph.getSpacingBefore() >= 1000 || paragraph.getSpacingAfter() > 1000) { paragraph.setSpacingBefore(0); paragraph.setSpacingAfter(0); } //设置word中左右间距 paragraph.setIndentationLeft(0); paragraph.setIndentationRight(0); List runs = paragraph.getRuns(); //加了图片,修改了paragraph的runs的size,所以循环不能使用runs List allRuns = new ArrayList(runs); for (XWPFRun run : allRuns) { String text = run.getText(0); if (text != null) { boolean isSetText = false; for (Map.Entry entry : param.entrySet()) { String key = entry.getKey(); if (text.indexOf(key) != -1) { isSetText = true; Object value = entry.getValue(); if (value instanceof String) {//文本替换 text = text.replace(key, value.toString()); } else if (value instanceof Map) {//图片替换 text = text.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 blipId = doc.addPictureData(byteInputStream, picType); doc.createPicture(blipId, doc.getNextPicNameNumber(picType), width, height, paragraph); } catch (Exception e) { e.printStackTrace(); } } } } if (isSetText) { run.setText(text, 0); } } } } } } /** * 根据图片类型,取得对应的图片类型代码 * * @param picType * @return int */ private static int getPictureType(String picType) { int res = CustomXWPFDocument.PICTURE_TYPE_PICT; if (picType != null) { if ("png".equalsIgnoreCase(picType)) { res = CustomXWPFDocument.PICTURE_TYPE_PNG; } else if ("dib".equalsIgnoreCase(picType)) { res = CustomXWPFDocument.PICTURE_TYPE_DIB; } else if ("emf".equalsIgnoreCase(picType)) { res = CustomXWPFDocument.PICTURE_TYPE_EMF; } else if ("jpg".equalsIgnoreCase(picType) || "jpeg".equalsIgnoreCase(picType)) { res = CustomXWPFDocument.PICTURE_TYPE_JPEG; } else if ("wmf".equalsIgnoreCase(picType)) { 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) { System.out.println("关闭流失败"); } } } return byteArray; } public static byte[] getFileStream(String url) { try { url = url.replace("\\", "/"); System.out.println(url); URL httpUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(1 * 1000); InputStream inStream = conn.getInputStream();//通过输入流获取图片数据 byte[] btImg = readInputStream(inStream);//得到图片的二进制数据 return btImg; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 网络图片进行下载,本地图片直接从文件下读取 */ public static InputStream retFileStream(String url, String uploadPath) { try { if (url.startsWith("http")) { url = url.replace("\\", "/"); URL httpUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(1 * 1000); InputStream inStream = conn.getInputStream(); return inStream; } else { File file = new File(uploadPath + url); if (file.isFile()) { InputStream inStream = new FileInputStream(file); return inStream; } } } catch (Exception e) { e.printStackTrace(); } return null; } public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); } }