{"id":356641,"date":"2024-05-20T23:34:06","date_gmt":"2024-05-20T23:34:06","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=356641"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-05-20T23:34:06","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=356641","title":{"rendered":"<span>\u0423\u043f\u0440\u043e\u0449\u0430\u0435\u043c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 Excel \u0441 \u043f\u043e\u043c\u043e\u0448\u044c\u044e Apache POI + Jxls: \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\u0414\u043e\u0431\u0440\u043e\u0435 \u0432\u0440\u0435\u043c\u044f!<\/p>\n<p>  \u0412 \u0440\u0430\u043c\u043a\u0430\u0445 \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0432\u0435\u0436\u0435\u0433\u043e \u043a\u0443\u0440\u0441\u0430 <b>\u00ab\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u0432 Java\u00bb<\/b> \u0435\u0441\u0442\u044c \u0442\u0435\u043c\u0430: \u00ab\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 Excel \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/poi.apache.org\/\" rel=\"nofollow noopener noreferrer\">Apache POI<\/a>\u00bb. \u0425\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0437\u0434\u0435\u0441\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c\u0438 \u043c\u043e\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043a\u043e\u0434\u0430 \u044d\u0442\u043e\u0433\u043e \u0437\u0430\u043d\u044f\u0442\u0438\u044f \u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c\u0441\u044f.<\/p>\n<p>  \u041f\u043e\u0448\u0430\u0433\u043e\u0432\u043e\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0448\u0430\u0431\u043b\u043e\u043d\u0430 <b><a href=\"https:\/\/javaops.ru\/static\/docjava\/poiTemplate.xlsx\" rel=\"nofollow noopener noreferrer\">poiTemplate.xlsx<\/a><\/b> \u0432 <b><a href=\"https:\/\/javaops.ru\/static\/docjava\/poi.xlsx\" rel=\"nofollow noopener noreferrer\">Excel \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 <\/a><\/b> \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e\u0439 \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446. \u0422\u0430\u043a\u0436\u0435 \u043a\u043e\u0434 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0441\u043b\u0443\u0436\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f <a href=\"https:\/\/habr.com\/ru\/articles\/552278\/\">\u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f<\/a>.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h3>\u0412\u044b\u0432\u043e\u0434 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u043a\u043b\u0430\u0434\u043a\u0438 (excel sheet)<\/h3>\n<p>  <\/p>\n<ul>\n<li>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c <a href=\"https:\/\/poi.apache.org\/\" rel=\"nofollow noopener noreferrer\">Apache POI<\/a> \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u00a0<i>pom.xml<\/i>. \u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u043e\u0432\u044b\u043c\u00a0<i>xlsx<\/i>\u00a0\u0444\u043e\u0440\u043c\u0430\u0442\u043e\u043c \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c\u00a0\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c <code>poi-ooxml<\/code>:\n<pre><code class=\"plaintext\">&lt;!-- Apache POI -->  &lt;dependency>     &lt;groupId>org.apache.poi&lt;\/groupId>     &lt;artifactId>poi&lt;\/artifactId>     &lt;version>${apache-poi.version}&lt;\/version> &lt;\/dependency> &lt;!--    For xlsx format --> &lt;dependency>     &lt;groupId>org.apache.poi&lt;\/groupId>     &lt;artifactId>poi-ooxml&lt;\/artifactId>     &lt;version>${apache-poi.version}&lt;\/version> &lt;\/dependency> <\/code><\/pre>\n<p>  <\/li>\n<li>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0448\u0430\u0431\u043b\u043e\u043d\u00a0 <i><a href=\"https:\/\/javaops.ru\/static\/docjava\/poiTemplate.xlsx\" rel=\"nofollow noopener noreferrer\">poiTemplate.xlsx<\/a><\/i>, \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0443\u0442 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b, \u0441 \u0434\u0432\u0443\u043c\u044f \u0442\u0438\u043f\u0430\u043c\u0438 \u043f\u043b\u044d\u0439\u0441\u0445\u043e\u043b\u0434\u0435\u0440\u043e\u0432:\u00a0 <i>${\u043f\u0440\u043e\u0441\u0442\u0430\u044f_\u043f\u043e\u0434\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430_\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f}<\/i> \u0438<br \/>  <i>\\{\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f_\u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f_\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b}<\/i>. \u0414\u0430\u043d\u043d\u044b\u043c\u0438 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 \u0431\u0443\u0434\u0443\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 <code>User<\/code> \u0441 \u0435\u0434\u043e\u0439 <code>User.meals<\/code>.  <\/li>\n<li>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043e\u0431\u0449\u0438\u0439 <code>ExcelPoiConverter<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 <i>templateFile<\/i> \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u0432 <i>outputFile<\/i>. \u041e\u043d \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f <b>\u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0448\u0430\u0431\u043b\u043e\u043d<\/b> \u0438 \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0432\u044b\u0432\u043e\u0434\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430. \u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 <code>XSSFWorkbook<\/code> \u0432\u043c\u0435\u0441\u0442\u043e <code>File<\/code> <a href=\"https:\/\/stackoverflow.com\/a\/54695626\/548473\" rel=\"nofollow noopener noreferrer\"><code>\u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u0430\u0432\u0430\u0442\u044c FileInputStream<\/code><\/a>, \u0438\u043d\u0430\u0447\u0435 \u043f\u0440\u0438 \u0432\u044b\u0432\u043e\u0434\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430 <i>Unexpected end of ZLIB input stream<\/i>. \u0412 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u043e\u043c <code>workbookComposer<\/code> \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u043d\u0430\u0448\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430.\n<pre><code class=\"java\">public class ExcelPoiConverter {     private final File templateFile;      public ExcelPoiConverter(File templateFile) {         this.templateFile = templateFile;     }      public void convert(File outputFile, Consumer&lt;Workbook> workbookComposer) throws IOException {         Files.copy(templateFile.toPath(), outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING);         \/\/ https:\/\/stackoverflow.com\/a\/54695626\/548473         try (Workbook workbook = new XSSFWorkbook(new FileInputStream(outputFile))) {             workbookComposer.accept(workbook);             try (OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(outputFile.toPath()))) {                 workbook.write(outputStream);             }         }     } } <\/code><\/pre>\n<\/li>\n<li>\u0412\u044b\u0432\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c <i><a href=\"https:\/\/javaops.ru\/static\/docjava\/poi.xlsx\" rel=\"nofollow noopener noreferrer\">\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c<\/a><\/i>: \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0432 \u0441\u0432\u043e\u0435\u0439 \u0432\u043a\u043b\u0430\u0434\u043a\u0435 \u0441 \u0438\u043c\u0435\u043d\u0435\u043c <code>user.email<\/code>, \u043a\u043e\u043f\u0438\u0440\u0443\u044f \u043f\u0435\u0440\u0432\u0443\u044e \u0441\u0442\u0430\u0440\u043d\u0438\u0446\u0443 \u0448\u0430\u0431\u043b\u043e\u043d\u0430. \u041c\u0435\u0442\u043e\u0434\u0430 <code>sheet.setName<\/code> \u043d\u0435\u0442, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432\u0435\u0441\u0442\u0438 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>workbook.setSheetName<\/code>\n<pre><code class=\"java\">        List&lt;User> users = ...;         ExcelPoiConverter converter = new ExcelPoiConverter.of(new File(\"poiTemplate.xlsx\"));          converter.convert(outputFile, workbook -> {             for (int i = 0; i &lt; users.size(); i++) {                 User user = users.get(i);                 workbook.cloneSheet(0);                 workbook.setSheetName(i + 1, user.getEmail());             }             workbook.removeSheetAt(0);         }); <\/code><\/pre>\n<\/li>\n<\/ul>\n<p>  <\/p>\n<h3>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 <i>${\u043f\u0440\u043e\u0441\u0442\u0430\u044f_\u043f\u043e\u0434\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430_\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f}<\/i><\/h3>\n<p>  <\/p>\n<ul>\n<li>\u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 <code>ExcelPoiConverter<\/code> \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u044f\u0447\u0435\u0439\u043a\u0438 <i>poiTemplate.xlsx<\/i> \u043d\u0430 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u044f \u0441 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u043c <code>PLACEHOLDER_PATTERN<\/code> \u0438 \u0434\u0435\u043b\u0430\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a <code>List&lt;Placeholder> placeholders<\/code>. \u0427\u0442\u043e\u0431\u044b \u0441\u0430\u043c \u0444\u0430\u0439\u043b \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u043d\u0435 \u043f\u043e\u0440\u0442\u0438\u043b\u0441\u044f, \u0435\u0433\u043e <a href=\"https:\/\/stackoverflow.com\/a\/54695626\/548473\" rel=\"nofollow noopener noreferrer\"><code>\u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 FileInputStream<\/code><\/a>\n<pre><code class=\"java\">public class ExcelPoiConverter {     private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile(\"\\\\$\\\\{(\\\\w+)}\");     private final File templateFile;     private final List&lt;Placeholder> placeholders = new ArrayList&lt;>();      public record Placeholder(Matcher matcher, CellAddress cellAddress) {     }      public ExcelPoiConverter(File templateFile) throws IOException {         this.templateFile = templateFile;         try (Workbook workbook = new XSSFWorkbook(new FileInputStream((templateFile)))) {             \/\/ find all placeholders on sheet 0             workbook.getSheetAt(0).forEach(row ->                     row.forEach(cell -> {                         Matcher matcher = PLACEHOLDER_PATTERN.matcher(cell.getStringCellValue());                         if (matcher.find()) {                             placeholders.add(new Placeholder(matcher, cell.getAddress()));                         }                     }));         }     } <\/code><\/pre>\n<p>  <\/li>\n<li>\u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043b\u044d\u0439\u0441\u0445\u043e\u043b\u0434\u0435\u0440\u043e\u0432 \u0432 <code>ExcelPoiConverter.resolvePlaceholders<\/code>, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0437\u0430\u043c\u0435\u043d\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 <code>Map&lt;String,Supplier&lt;Object>> supplierMap<\/code>. \u0421\u043e\u0441\u0442\u0430\u0432\u043d\u044b\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u044b (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <i>Processing ${name} with email ${email} successfully<\/i>) \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c <a href=\"https:\/\/stackoverflow.com\/questions\/38376584\/548473\" rel=\"nofollow noopener noreferrer\"><code>\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u044f\u044f \u0432\u0441\u0435 \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f Matcher<\/code><\/a>\n<pre><code class=\"java\">        ....         converter.resolvePlaceholders(sheet,              Map.of(\"name\", user::getName,                         \"enabled\", () -> user.isEnabled() ? \"enabled\" : \"disabled\",                         \"email\", user::getEmail,                         \"caloriesPerDay\", user::getCaloriesPerDay,                         \"registered\", () -> DATE_TIME_FORMATTER.format(user.getRegistered()),                         \"roles\", () -> user.getRoles().toString())              );      public void resolvePlaceholders(Sheet sheet, Map&lt;String, Supplier&lt;Object>> supplierMap) {         placeholders.forEach(p -> {             p.matcher.reset();             \/\/   https:\/\/stackoverflow.com\/questions\/38376584\/548473             StringBuilder sb = new StringBuilder();             while (p.matcher.find()) {                 String key = p.matcher.group(1);                 Object obj = supplierMap.get(key).get();                 p.matcher.appendReplacement(sb, obj.toString());             }             p.matcher.appendTail(sb);             setCell(sheet, p.cellAddress, sb.toString());         });     }      public static Cell getCell(Sheet sheet, CellAddress cellAddress) {         return sheet.getRow(cellAddress.getRow()).getCell(cellAddress.getColumn());     }      public static void setCell(Sheet sheet, CellAddress cellAddress, Object value) {         getCell(sheet, cellAddress).setCellValue(value.toString());     } <\/code><\/pre>\n<p>  <\/li>\n<\/ul>\n<p>  <\/p>\n<p>PS: \u0435\u0441\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u0437\u0430\u043c\u0435\u043d\u044b \u043c\u043d\u043e\u0433\u043e, \u0435\u0441\u0442\u044c \u0441\u043c\u044b\u0441\u043b \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043e\u0434\u043d\u0443 \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a Expression Language: <a href=\"https:\/\/docs.spring.io\/spring-framework\/reference\/core\/expressions.html\" rel=\"nofollow noopener noreferrer\">Spring Expression Language (SpEL)<\/a>, <a href=\"https:\/\/commons.apache.org\/proper\/commons-jexl\/\" rel=\"nofollow noopener noreferrer\">Java Expression Language (JEXL)<\/a> \u0438\u043b\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 <code>User<\/code> \u0432 Map \u0447\u0435\u0440\u0435\u0437 <a href=\"https:\/\/github.com\/FasterXML\/jackson\" rel=\"nofollow noopener noreferrer\">Jackson<\/a>:<\/p>\n<pre><code class=\"xml\"> &lt;dependency>             &lt;groupId>com.fasterxml.jackson.core&lt;\/groupId>             &lt;artifactId>jackson-databind&lt;\/artifactId>         &lt;\/dependency>         &lt;dependency>             &lt;groupId>com.fasterxml.jackson.datatype&lt;\/groupId>             &lt;artifactId>jackson-datatype-jsr310&lt;\/artifactId>         &lt;\/dependency> <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"java\">public class JsonUtil {       private static final ObjectMapper MAPPER = new ObjectMapper();        static {            MAPPER.registerModule(new JavaTimeModule());  \/\/ support java.time            MAPPER.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);       }        public static &lt;T> Map&lt;String, Object> asMap(T obj) {           return MAPPER.convertValue(obj, new TypeReference&lt;>() {});       } } <\/code><\/pre>\n<h3>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 <i>\\{\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f_\u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f_\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b}<\/i><\/h3>\n<p>  <\/p>\n<ul>\n<li>\u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 <code>ExcelPoiConverter<\/code> \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0430\u043d\u0430\u043b\u0438\u0437 <code>PROCESSOR_PATTERN<\/code><\/li>\n<li>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a <code>ExcelPoiConverter.doProcessors<\/code>, \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0432 <code>Map&lt;String,Consumer&lt;CellAddress>> consumerMap<\/code>\n<pre><code class=\"java\">    private static final Pattern PROCESSOR_PATTERN = Pattern.compile(\"\\\\\\\\\\\\{(\\\\w+)}\");      private final List&lt;Processor> processors = new ArrayList&lt;>();     public record Processor(String key, CellAddress cellAddress) {     }      public ExcelPoiConverter(File templateFile) throws IOException {             ...             matcher = PROCESSOR_PATTERN.matcher(value);             if (matcher.find()) {                  processors.add(new Processor(matcher.group(1), cell.getAddress()));             }             ...       public void doProcessors(Map&lt;String, Consumer&lt;CellAddress>> consumerMap) {            processors.forEach(p -> consumerMap.get(p.key).accept(p.cellAddress));      } <\/code><\/pre>\n<p>  <\/li>\n<li>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b <a href=\"https:\/\/www.tabnine.com\/code\/java\/methods\/org.apache.poi.hssf.usermodel.HSSFSheet\/removeRow\" rel=\"nofollow noopener noreferrer\">\u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f<\/a> \u0438 <a href=\"https:\/\/stackoverflow.com\/a\/49153445\/548473\" rel=\"nofollow noopener noreferrer\">\u0432\u0441\u0442\u0430\u0432\u043a\u0438<\/a> \u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u0438 \u0441\u0434\u0432\u0438\u0433\u0430 \u0430\u0434\u0440\u0435\u0441\u0430 \u044f\u0447\u0435\u0439\u043a\u0438:\n<pre><code class=\"java\">    public static void insertRow(Sheet sheet, int rowNum) {         Row templateRow = sheet.getRow(rowNum);         if (sheet.getLastRowNum() > rowNum) {             sheet.shiftRows(rowNum + 1, sheet.getLastRowNum(), 1);         }         Row newRow = sheet.createRow(rowNum + 1);         templateRow.cellIterator().forEachRemaining(                 cell -> newRow.createCell(cell.getColumnIndex()).setCellStyle(cell.getCellStyle())         );     }      public static void removeRow(Sheet sheet, int rowNum) {         int lastRowNum = sheet.getLastRowNum();         if (rowNum >= 0 &amp;&amp; rowNum &lt; lastRowNum) {             sheet.shiftRows(rowNum + 1, lastRowNum, -1);         } else if (rowNum == lastRowNum) {             sheet.removeRow(sheet.getRow(rowNum));         }     }      public static CellAddress nextRow(CellAddress ca, int shift) {         return new CellAddress(ca.getRow() + shift, ca.getColumn());     }      public static CellAddress nextCell(CellAddress ca, int shift) {         return new CellAddress(ca.getRow(), ca.getColumn() + shift);     } <\/code><\/pre>\n<p>  <\/li>\n<li>\u0412 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0435\u0434\u044b \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0439 \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a <code>CellAddress<\/code>:\n<pre><code class=\"java\">         ...          converter.doProcessors(Map.of(                \"meals\", ca -> {                     if (user.getMeals() == null) {                         removeRow(sheet, ca.getRow());                     } else {                         Iterator&lt;Meal> iterator = user.getMeals().iterator();                         Meal first = iterator.next();                         writeMeal(first, sheet, ca);                         while (iterator.hasNext()) {                              insertRow(sheet, ca.getRow());                              ca = nextRow(ca, 1);                              writeMeal(iterator.next(), sheet, ca);                          }                     }          }));      private void writeMeal(Meal meal, Sheet sheet, CellAddress ca) {         setCell(sheet, ca, DATE_TIME_FORMATTER.format(meal.getDateTime()));         setCell(sheet, nextCell(ca, 1), meal.getValue());         setCell(sheet, nextCell(ca, 2), meal.getCalories());         setCell(sheet, nextCell(ca, 3), meal.getExcess());     } <\/code><\/pre>\n<p>  <\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0418\u0442\u043e\u0433: \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 <a href=\"https:\/\/poi.apache.org\/\" rel=\"nofollow noopener noreferrer\">Excel Apache POI<\/a> \u043c\u043d\u0435 \u043f\u043e\u043d\u0440\u0430\u0432\u0438\u043b\u0430\u0441\u044c \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0441 \u0448\u0430\u0431\u043b\u043e\u043d\u0430\u043c\u0438 <a href=\"https:\/\/community.jaspersoft.com\/wiki\/excel-export-settings\" rel=\"nofollow noopener noreferrer\">JasperReports<\/a> (\u0438 \u0432\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u044b \u0442\u043e\u043b\u044c\u043a\u043e API \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 <code>Workbook<\/code>), \u0445\u043e\u0442\u044f \u043a \u0438\u0445 API \u0435\u0441\u0442\u044c \u043f\u0440\u0435\u0442\u0435\u043d\u0437\u0438\u0438 \u2014 \u0438\u043d\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043e\u0432\u043e\u0437\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0439\u0442\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438 \u043d\u0443\u0436\u043d\u044b \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u0443\u0442\u0438\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u043e\u0441\u044f\u0442\u0441\u044f \u0432 \u0440\u043e\u0434\u043d\u043e\u0435 API. \u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f Apache POI:  <\/p>\n<ul>\n<li><a href=\"https:\/\/www.baeldung.com\/java-microsoft-excel\" rel=\"nofollow noopener noreferrer\">Working with Microsoft Excel in Java<\/a><\/li>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/how-to-write-data-into-excel-sheet-using-java\" rel=\"nofollow noopener noreferrer\">Excel Write into Sheet using Java<\/a><\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u043a\u043e\u0434 \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f \u0432\u0430\u043c \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c, \u043d\u0430\u043f\u0438\u0448\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445, \u044f \u0441\u0434\u0435\u043b\u0430\u044e \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u2014 \u0440\u0430\u0431\u043e\u0442\u0430 \u0432 Apache POI \u0441 \u0446\u0432\u0435\u0442\u043e\u043c (\u0430 \u0442\u0430\u043a\u0436\u0435 \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0432\u0438\u0434\u0430\u043c\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u0443\u0440\u0441\u0430).<\/p>\n<p>  <\/p>\n<h3>UPDATE: \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0432 \u043a\u0443\u0440\u0441 <b>\u00ab\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u0432 Java\u00bb<\/b> \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 <a href=\"https:\/\/jxls.sourceforge.net\/\" rel=\"nofollow noopener noreferrer\">Jxls<\/a><\/h3>\n<p>  \u0428\u0430\u0431\u043b\u043e\u043d \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 <a href=\"https:\/\/javaops.ru\/static\/docjava\/jxlsTemplate.xlsx\" rel=\"nofollow noopener noreferrer\">jxlsTemplate.xlsx<\/a> (\u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0437\u0430\u0434\u0430\u044e\u0442\u0441\u044f \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u044f\u0447\u0435\u0435\u043a) \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u0432\u0441\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f, \u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0446\u0432\u0435\u0442\u0430 \u0434\u043b\u044f \u0435\u0434\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0410\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043c\u043e\u0436\u043d\u043e \u043c\u0435\u043d\u044f\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <a href=\"https:\/\/jxls.sourceforge.net\/samples\/area_listener.html\" rel=\"nofollow noopener noreferrer\">Area Listener<\/a>, \u043f\u0440\u0438\u0447\u0435\u043c \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0441\u043a\u0440\u0435\u0441\u0442\u0438\u0442\u044c <a href=\"https:\/\/stackoverflow.com\/a\/77251671\/548473\" rel=\"nofollow noopener noreferrer\">Excel mark-up c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c<\/a>. \u041f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u044e \u043d\u0430 \u043a\u0443\u0440\u0441!<\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0438 \u0443\u0441\u043f\u0435\u0445\u043e\u0432 \u0432 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438!<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/765332\/\"> https:\/\/habr.com\/ru\/articles\/765332\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\u0414\u043e\u0431\u0440\u043e\u0435 \u0432\u0440\u0435\u043c\u044f!<\/p>\n<p>  \u0412 \u0440\u0430\u043c\u043a\u0430\u0445 \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0432\u0435\u0436\u0435\u0433\u043e \u043a\u0443\u0440\u0441\u0430 <b>\u00ab\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u0432 Java\u00bb<\/b> \u0435\u0441\u0442\u044c \u0442\u0435\u043c\u0430: \u00ab\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 Excel \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/poi.apache.org\/\" rel=\"nofollow noopener noreferrer\">Apache POI<\/a>\u00bb. \u0425\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0437\u0434\u0435\u0441\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c\u0438 \u043c\u043e\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043a\u043e\u0434\u0430 \u044d\u0442\u043e\u0433\u043e \u0437\u0430\u043d\u044f\u0442\u0438\u044f \u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c\u0441\u044f.<\/p>\n<p>  \u041f\u043e\u0448\u0430\u0433\u043e\u0432\u043e\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0448\u0430\u0431\u043b\u043e\u043d\u0430 <b><a href=\"https:\/\/javaops.ru\/static\/docjava\/poiTemplate.xlsx\" rel=\"nofollow noopener noreferrer\">poiTemplate.xlsx<\/a><\/b> \u0432 <b><a href=\"https:\/\/javaops.ru\/static\/docjava\/poi.xlsx\" rel=\"nofollow noopener noreferrer\">Excel \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 <\/a><\/b> \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e\u0439 \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446. \u0422\u0430\u043a\u0436\u0435 \u043a\u043e\u0434 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0441\u043b\u0443\u0436\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f <a href=\"https:\/\/habr.com\/ru\/articles\/552278\/\">\u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f<\/a>.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-356641","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/356641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=356641"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/356641\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=356641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=356641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=356641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}