1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
| @Component public class WordUtils {
public void exportWord(String path, Map<String,Object> params, String filename, HttpServletResponse response) throws IOException, InvalidFormatException { InputStream is=new FileInputStream(path); XWPFDocument doc=new XWPFDocument(is); iteraTable(params,doc); OutputStream os=response.getOutputStream(); response.setContentType("application/octet-stream; charset=utf-8"); response.setHeader("Content-disposition", "attachment; filename=" + filename); doc.write(os); close(os); }
private void iteraTable(Map<String,Object> params,XWPFDocument doc) throws IOException, InvalidFormatException { List<XWPFTableRow> rows=null; List<XWPFTableCell> cells=null;
List<XWPFTable> tables=doc.getTables(); for (XWPFTable table : tables) { rows=table.getRows(); for (XWPFTableRow row : rows) { cells=row.getTableCells(); for (XWPFTableCell cell: cells) { if(strMatcher(cell.getText()).find()){ replaceInStr(cell,params,doc); continue; } if(imgMatcher(cell.getText()).find()){ replaceInImg(cell,params,doc); continue; } } } } }
private Matcher imgMatcher(String imgstr){ Pattern pattern=Pattern.compile("@\\{(.+?)\\}"); Matcher matcher=pattern.matcher(imgstr); return matcher; }
private Matcher strMatcher(String str){ Pattern pattern=Pattern.compile("\\$\\{(.+?)\\}"); Matcher matcher=pattern.matcher(str); return matcher; }
private void replaceInStr(XWPFTableCell cell, Map<String,Object> params, XWPFDocument doc) { String key = cell.getText().substring(2, cell.getText().length() - 1);
Integer datatype = getMapStrDataTypeValue(params, key);
List<XWPFParagraph> parags = cell.getParagraphs(); for (int i = 0; i < parags.size(); i++) { cell.removeParagraph(i); } if (datatype.equals(0)) { return; } else if (datatype.equals(2)) { List<String> strs = (List<String>) params.get(key); Iterator<String> iterator = strs.iterator(); while (iterator.hasNext()) { XWPFParagraph para = cell.addParagraph(); XWPFRun run = para.createRun(); run.setText(iterator.next()); } } else if (datatype.equals(3)) { String str = (String) params.get(key).toString(); cell.setText(str); } }
private void replaceInImg(XWPFTableCell cell,Map<String,Object> params,XWPFDocument doc) throws IOException, InvalidFormatException { String key=cell.getText().substring(2,cell.getText().length()-1); Integer datatype=getMapImgDataType(params,key); List<XWPFParagraph> parags=cell.getParagraphs(); for (int i=0;i<parags.size();i++){ cell.removeParagraph(i); } if(datatype.equals(0)){ return; }else if(datatype.equals(1)){ XWPFParagraph parag=cell.addParagraph(); Map<String,Object> pic=(Map<String,Object>)params.get(key); insertImg(pic,parag); }else if(datatype.equals(2)) { List<Map<String,Object>> pics=(List<Map<String,Object>>)params.get(key); Iterator<Map<String, Object>> iterator = pics.iterator(); XWPFParagraph para = cell.addParagraph(); Integer count = 0; while (iterator.hasNext()) { Map<String, Object> pic = iterator.next(); if (Integer.parseInt(pic.get("style").toString()) == 1) { } if (Integer.parseInt(pic.get("style").toString()) == 2) { if (count > 0) { para = cell.addParagraph(); } } insertImg(pic, para); count++; } } }
private void insertImg(Map<String,Object> pic,XWPFParagraph para) throws IOException, InvalidFormatException { if(pic.get("imgpath").toString()==null) return; String picpath=pic.get("imgpath").toString(); InputStream is=null; BufferedImage bi=null; if(picpath.startsWith("http")) { is=HttpUtils.getFileStream(picpath); bi=ImageIO.read(HttpUtils.getFileStream(picpath)); }else { is = new FileInputStream(picpath); bi=ImageIO.read(new File(picpath)); } XWPFRun run =para.createRun(); Integer width=bi.getWidth(); Integer heigh=bi.getHeight(); Double much=80.0/width; run.addPicture(is,getPictureType(picpath.substring(picpath.lastIndexOf(".")+1)),"", Units.toEMU(80),Units.toEMU(heigh*much));
close(is); bi=null; }
private Integer getMapStrDataTypeValue(Map<String,Object> params,String key){ if(params.get(key)==null){ return 0; }else if(params.get(key) instanceof List){ return 2; }else if(params.get(key) instanceof String){ return 3; }else { throw new RuntimeException("Str data type error!"); }
}
private Integer getMapImgDataType(Map<String,Object> params,String key){ if(params.get(key)==null){ return 0; } else if(params.get(key) instanceof Map){ return 1; }else if(params.get(key) instanceof List){ return 2; }else { throw new RuntimeException("image data type error!"); } }
private int getPictureType(String picType) { int res = XWPFDocument.PICTURE_TYPE_PICT; if (picType != null) { if (picType.equalsIgnoreCase("png")) { res = XWPFDocument.PICTURE_TYPE_PNG; } else if (picType.equalsIgnoreCase("dib")) { res = XWPFDocument.PICTURE_TYPE_DIB; } else if (picType.equalsIgnoreCase("emf")) { res = XWPFDocument.PICTURE_TYPE_EMF; } else if (picType.equalsIgnoreCase("jpg") || picType.equalsIgnoreCase("jpeg")) { res = XWPFDocument.PICTURE_TYPE_JPEG; } else if (picType.equalsIgnoreCase("wmf")) { res = XWPFDocument.PICTURE_TYPE_WMF; } } return res; }
public 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; }
private void close(InputStream is) { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } }
private void close(OutputStream os) { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } }
|