Git Product home page Git Product logo

export-data-to-excel-template's Introduction

Technologies ๐ŸŽฐ

  1. Java 17
  2. maven
  3. Apache poi 5.2.3
  4. Jxls 2.13

This is the library configuration

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>5.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>5.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jxls/jxls -->
<dependency>
  <groupId>org.jxls</groupId>
  <artifactId>jxls</artifactId>
  <version>2.13.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jxls/jxls-poi -->
<dependency>
  <groupId>org.jxls</groupId>
  <artifactId>jxls-poi</artifactId>
  <version>2.13.0</version>
</dependency>

Features โญโญโญโญโญ

  1. Download template Excel( file in resource)
  2. Export data from object to excel template by Jxls -> Doc: https://jxls.sourceforge.net/reference/if_command.html
  3. Read excel data by apache poi

Details on exporting data to a sample excel file

The data we need is in the form of Map<String, Object>. For example, when you have a list of Objects, you need to put it into a Map<String, Object> Ex:

List<Server> data = initializeData(); // initializeData() return a List<Server>
Map<String, Object> servers = new HashMap<>();
servers.put("data", data);

Next, extremely important is the Excel template file Excel template with comment markup We can see that the excel template file is very similar to the normal file, the only difference is the addition of Comment markup. It's all here. You can see jx:each(...) items="data" (Required) The key "data" is what you declared in your code. It is a list object

List<Server> data = initializeData();
Map<String, Object> servers = new HashMap<>();
servers.put("data", data);

var = "item" (Required) We will use item as a temporary variable in the foreach loop

lastCell="N3" (Required) It declares column limits. As in my excel file is at position N3

varIndex="index" It represents the index of the item in the list data. For example, if your Excel file has a column named โ€œNoโ€, we will index that column. And since the index in the list starts at 0, we'll use index + 1

jx:area(lastCell ="N3") (Required) Please fill in the appropriate box in LastCell

Details about Jxls and comment markup you can see here https://jxls.sourceforge.net/reference/each_command.html

Note : How to create comment markup ๐Ÿ““

How to create comment markup

Right-click on the cell and select Insert comment (It's very complicated ๐Ÿคฃ)

Here is my code (Only two file)

  1. Controller.
@GetMapping
    public void exportDataToExcelTemplateFile(HttpServletResponse response) {
        try {
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment; filename=exportDataToExcelTemplate.xlsx");
            this.exportDataToExcelTemplateService.exportDataToExcelTemplate(response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
  1. Service
@Override
    public void exportDataToExcelTemplate(OutputStream outputStream) {
        List<Server> data = initializeData();

        Map<String, Object> servers = new HashMap<>();
        servers.put("data", data);

        try(InputStream inputStream = this.getClass().getResourceAsStream("/template_exports/template_server_list_for_export.xlsx")) {
            Context context = new Context();
            context.toMap().putAll(servers);
            JxlsHelper.getInstance().processTemplate(inputStream, outputStream, context);
        } catch(NullPointerException e) {
            throw new NotFoundException("Template not found in resources/templates_exports");
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

Done

The remaining tasks such as downloading files or reading excel files are very simple and there are many instructions from other programmers so I will not describe them here anymore. My focus is only on Exporting data to excel template

Getting Started ๐Ÿ› 

Prerequisites

Java 17

Maven

Installation

git clone https://github.com/namtruongto/Export-data-to-Excel-template.git

Contact ๐Ÿ“ง For any questions, please contact [email protected]

export-data-to-excel-template's People

Contributors

namtruongto avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.