Location>code7788 >text

EasyExcel populates data through templates

Popularity:411 ℃/2025-02-11 11:10:11

EasyExcel fills data through templates in two ways: 1. Fill directly through templates. 2. Fill through IO stream.

Template example
Note: Only the field name is written when filling a single field, and the dataset fill needs to be added before the field.

{title}
Name Nick name Phone number
{.username} {.nickname} {.phone}

Code example:

package ;

 import ;
 import ;
 import ;
 import ;
 import ;
 import ;
 import ;

 import .*;
 import ;
 import ;
 import ;
 import ;
 import ;
 import ;
 import ;
 import ;

 /**
  * @Author: Yixiu
  * @Date: 2025/2/10
  */
 public class ExcelTemplate {

     public static void main(String[] args) throws IOException {
         // Prepare data
         List<SystemUser> users = new ArrayList<>();
         SystemUser user1 = new SystemUser();
         ("Zhang San");
         ("zhangsan");
         ("11111");
         (user1);
         SystemUser user2 = new SystemUser();
         ("Zhang San");
         ("zhangsan");
         ("11111");
         (user2);
         // Single field fill, using map format
         Map<String, Object> otherData = new HashMap<>();
         ("title", "system user table");
         // Template file path
         String templateFileName = "src/main/resources/";
         DateTimeFormatter formatter = ("yyyyMMddHHmmss");
         String formattedDateTime = ().format(formatter);
         // Output file path
         String outputFileName = "src/main/resources/filled_template_" + formattedDateTime + ".xlsx";
         // Output file path
         String outputFileName1 = "src/main/resources/filled_template1_" + formattedDateTime + ".xlsx";

         // 1. Fill directly through the file template
         try (ExcelWriter excelWriter = (outputFileName).withTemplate(templateFileName).build();) {
             // Create a write worksheet
             WriteSheet writeSheet = ().build();
             // Create a new line every time the list parameter is used.
             FillConfig fillConfig = ().forceNewRow(true).build();
             // Fill for a field
             (otherData, fillConfig, writeSheet);
             // Fill the collection
             (users, fillConfig, writeSheet);
         }


         // 2. Fill through IO stream
         try (
                 // Use try-with-resources to manage input flow
                 InputStream inputStream = ((templateFileName));
                 InputStream templateInputStream = new BufferedInputStream(inputStream);
                 ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
         ) {
             try (
                     // Create ExcelWriter object
                     ExcelWriter excelWriter = (outputStream).excelType().withTemplate(templateInputStream).build()
             ) {
                 WriteSheet writeSheet = ().build();
                 FillConfig fillConfig = ().forceNewRow().build();
                 // Fill for a field
                 (otherData, fillConfig, writeSheet);
                 // Fill the collection
                 (users, fillConfig, writeSheet);
             }

             try (
                     FileOutputStream fileOutputStream = new FileOutputStream(outputFileName1)
             ) {
                 (fileOutputStream);
                 // Refresh the FileOutputStream buffer to ensure that all data is written to the file
                 ();
                 ("File generation successfully: " + outputFileName1);
             }
         }
     }
 }