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
| @SneakyThrows(value = Exception.class) private void testWord(HttpServletResponse response) { String fontFamily = "宋体"; XWPFDocument document = WordUtil.createDocument(); WordUtil.handlePaperSize(document); WordUtil.handlePagerMargin(document); XWPFParagraph title = WordUtil.handleAddParagraph(document, "Word功能测试报告", fontFamily, 22, ParagraphAlignment.CENTER, true); WordUtil.handleMargins(title, 120, 120); XWPFParagraph title1 = WordUtil.handleAddParagraph(document, "1.段落摘录", fontFamily, 18, ParagraphAlignment.LEFT, true); WordUtil.handleMargins(title1, 120, 120); String content = "阳春三月,风和日暖;信步城外,看阡陌之上杨柳依依,野花绚烂,身心不由得轻爽而浪漫。漫步陌上,只因陌上花开;花是自然的那种,朴素而恬淡,不落尘俗。“三月风情陌上花”,是花在其中生命得以璀璨,人在其心心情得以畅然的一种意境。这意境,枝繁叶茂,从古代长到现代,不枯不衰;又如水,岁岁年年,流淌在阡陌之上,不知迷醉过古今几个王公贵族、粉黛佳丽、骚人墨客、凡男俗女。三月陌上花,让人爱让人痴,恍惚人的骨子里都沉淀了花的影子,花的风韵。"; XWPFParagraph paragraph = WordUtil.handleAddParagraph(document, content, fontFamily, 14, ParagraphAlignment.LEFT, false); WordUtil.handleMargins(paragraph, 120, 120); WordUtil.handleFirstLineIndentation(paragraph, 16); WordUtil.handleBlankRow(document); XWPFParagraph title2 = WordUtil.handleAddParagraph(document, "2.表格处理", fontFamily, 18, ParagraphAlignment.LEFT, true); WordUtil.handleMargins(title2, 120, 120); XWPFTable table = WordUtil.createTable(document, 4, 5); WordUtil.handleRowSpacing(table, 454); List<XWPFTableRow> rows = table.getRows(); for (int i = 0; i < rows.size(); i++) { XWPFTableRow row = rows.get(i); List<XWPFTableCell> tableCells = row.getTableCells(); if (i == 0) { String[] headers = {"序号", "姓名", "年龄", "爱好", "职业"}; for (int j = 0; j < tableCells.size(); j++) { XWPFTableCell cell = tableCells.get(j); WordUtil.handleCellContent(cell, headers[j], fontFamily, 12); } } if (i == 1) { String[] contents = {"1", "汐海", "24", "Java编程", "后端工程师"}; for (int j = 0; j < tableCells.size(); j++) { XWPFTableCell cell = tableCells.get(j); WordUtil.handleCellContent(cell, contents[j], fontFamily, 12); } } if (i == 2) { String[] contents = {"2", "悦彬", "23", "炉石传说", "自由职业者"}; for (int j = 0; j < tableCells.size(); j++) { XWPFTableCell cell = tableCells.get(j); WordUtil.handleCellContent(cell, contents[j], fontFamily, 12); } } if (i == 3) { WordUtil.handleCellHeight(row, 4540); XWPFTableCell cell = row.getCell(0); String imageUrl = "https://b0.bdstatic.com/0e4821a924ce3a31d40da2e5265a4cea.jpg@h_1280"; URL url = new URL(imageUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(10000); connection.setReadTimeout(10000); try (InputStream inputStream = connection.getInputStream()) { byte[] imageBytes = IOUtils.toByteArray(inputStream); java.awt.image.BufferedImage image = javax.imageio.ImageIO.read(new ByteArrayInputStream(imageBytes)); int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); double desiredHeightCm = 11.12; double desiredWidthCm = desiredHeightCm * imageWidth / imageHeight; int heightEMU = (int) desiredHeightCm * 360000; ByteArrayInputStream reusableInputStream = new ByteArrayInputStream(imageBytes); WordUtil.handleCellImage(cell.addParagraph().createRun(), reusableInputStream, "本人照片.png", widthEMU, heightEMU); } finally { connection.disconnect(); } WordUtil.handleCellStyle(cell, STVerticalJc.CENTER, ParagraphAlignment.CENTER); } HashMap<Integer, Integer> map = new HashMap<>(); map.put(0, 1000); WordUtil.handleCellWidth(row, map); } XWPFTableRow row = rows.get(rows.size() - 1); WordUtil.handleMergeCell(row, 0, rows.size()); @Cleanup ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); WordUtil.exportWord(document, outputStream); response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode("测试报告.docx", "UTF-8")); response.setContentType("application/octet-stream; charset=UTF-8"); response.getOutputStream().write(outputStream.toByteArray()); }
|