Writing in the front

Whether it is a traditional software enterprise or an Internet enterprise, whether it is management software or C-oriented Internet applications. Inevitably involves reporting operations, and exporting data to Excel is an important function for reporting business. If we embed a lot of logic to export Excel in our business code, our code becomes bloated and unmaintainable, and the core logic to export Excel is basically the same. Can we encapsulate the core logic of exporting Excel into a tool that simply passes data to the tool when we need to export Excel? Thus, MyKit-Excel was born!

Mykit-excel is available on github at github.com/sunshinelyz… Welcome to Star and Fork source code, also welcome to pr your awesome code, let’s fatten it up!

Framework is briefly

Mykit-excel plug-in is a general excel import and export framework, designed to provide a general Excel import and export function, support to annotate the selection of JavaBean parts of the field export, and provide annotations to specify excel column headings and sorting functions.

Frame structure

  • Mykit-excel – Annotation: An annotation module of the Mykit-Excel framework that provides annotations to identify which fields in a class need to be exported to Excel
  • Mykit-excel -common: mykit-Excel framework generic utility class, provides common tool templates
  • Mykit-excel servlet: Mykit-Excel framework provides a Web module, can support Web requests to export Excel
  • Mykit-excel – SpringMVC: Mykit-Excel framework provides springMVC module, can support Web requests to export Excel
  • Mykit-excel: A regular test module provided by the Mykit-Excel framework
  • Mykit-excel: Springboot test module provided by mykit-Excel framework

The test case

(1) test routine export Excel tools Java classes as: IO. Mykit. Excel. Springboot. Normal. Export. TestExportExcelUtils, this class can be run directly.

(2) test annotations Java class category for export Excel tools: IO. Mykit. Excel. Springboot. The annotation. Export. TestAnnotationExportExcelUtils, this class can be run directly.

(3) test for SpringMVC export Excel Java classes for the IO. Mykit. Excel. Springboot. Normal. For SpringMVC. NormalExportExcelContorller, Run SpringBoot startup class IO. Mykit. Excel. SpringBoot. After MykitExcelCoreApplication, ExportExcel using normalExportExcel. HTML in the resources/ HTML directory. If the SPECIFIED IP address and port are different from the mykit-Excel – Springboot module, modify the IP address and port in normalExportExcel.

(4) test annotation-based export Java classes for the IO. Mykit. Excel. Springboot. The annotation. For springmvc. AnnotationExportExcelController, Run SpringBoot startup class IO. Mykit. Excel. SpringBoot. After MykitExcelCoreApplication, Using the resources/HTML directory annotationExportExcel. HTML file export Excel. If set the IP and port, unlike mykit – excel – springboot module, the modified annotationExportExcel. HTML files of the IP and port.

Note that

If you export Excel using annotations, you need to add the @ExcelColumn annotation to the JavaBean property field. This annotation has three attributes, as follows:

  • IsExport: indicates whether to export the current field to Excel. True: indicates yes. False: no
  • Title: The current title when exporting to Excel;
  • Sort: the position of the current field in Excel when exported to the Excel column. The smaller the value, the higher the current column is.

use

Export Excel in common mode

If it’s a normal Java project that just exports the Excel file to local disk, you just need to add the following configuration to the project’s POM.xml file

<dependency>
    <groupId>io.mykit.excel</groupId>
    <artifactId>mykit-excel-common</artifactId>
    <version>1.0.0 - the SNAPSHOT</version>
</dependency>
Copy the code

Create the test Javabeans

@Data
public class Student implements Serializable {
    private static final long serialVersionUID = -2987207599880734028L;
    private int id;
    private String name;
    private String sex;

    public Student(a){}public Student(int id, String name, String sex){
        this.id = id;
        this.name = name;
        this.sex = sex; }}Copy the code

Next, export the Excel file in the program as follows

public static void main(String[] args) throws Exception{
    ExportExcelUtils<Student> utils = new ExportExcelUtils<Student>();
    List<Student> list = new ArrayList<Student>();
    for (int i = 0; i < 10; i++) {
        list.add(new Student(111."Zhang"."Male"));
        list.add(new Student(111."Bill"."Male"));
        list.add(new Student(111."Fifty"."Female"));
    }
    String[] columnNames = { "ID"."Name"."Gender" };
    utils.exportExcel("User export", columnNames, list, new FileOutputStream("E:/test.xls"), ExportExcelUtils.EXCEL_FILE_2003);
}
Copy the code

The exported file is as follows

Export Excel by annotations

If it’s a normal Java project that annotates Excel files to local disk, you just need to add the following configuration to the project’s POM.xml file

<dependency>
    <groupId>io.mykit.excel</groupId>
    <artifactId>mykit-excel-common</artifactId>
    <version>1.0.0 - the SNAPSHOT</version>
</dependency>
Copy the code

Create the test Javabeans

(1) Create the parent JavaBean class

@Data
public class Person implements Serializable {
    private static final long serialVersionUID = 3251965335162340137L;

    @excelcolumn (isExport = true, title = "id ", sort = 2)
    private String id ;

    @excelcolumn (isExport = true, title = "name ", sort = 3)
    private String name;
    
    public Person(a){}public Person(String id, String name){
        this.id = id;
        this.name = name; }}Copy the code

(2) Create a subclass JavaBean

@Data
public class Student extends Person{
    private static final long serialVersionUID = -6180523202831503132L;

    @excelcolumn (isExport = false, title = "class id ", sort = 1)
    private String classNo;

    private Integer score;

    @excelcolumn (isExport = true, title = "类 ", sort = 5)
    private String hobby;
    
    public Student(a){}public Student(String id, String name, String classNo, Integer score, String hobby){
        super(id, name);
        this.classNo = classNo;
        this.score = score;
        this.hobby = hobby; }}Copy the code

Next, export the Excel file in the program as follows

public class TestAnnotationExportExcelUtils {
    public static void main(String[] args) throws FileNotFoundException {
        // Prepare data
        List<Student> list = new ArrayList<Student>();
        for (int i = 1; i <= 10; i++) {
            list.add(new Student("00" + i, "Zhang"."001".100."Basketball"));
        }
        AnnotationExcelExportUtils<Student> utils = new AnnotationExcelExportUtils<Student>();
        utils.exportExcel("User export", list, new FileOutputStream("e:/E:/test.xls"), Student.class, AnnotationExcelExportUtils.EXCEL_FILE_2003); }}Copy the code

The exported file is as follows

Export Excel in Web mode

If the Project is based on Java Web or Spring MVC and needs to export Excel, add the following configuration to the pom. XML file of the project

<dependency>
    <groupId>io.mykit.excel</groupId>
    <artifactId>mykit-excel-servlet</artifactId>
    <version>1.0.0 - the SNAPSHOT</version>
</dependency>
Copy the code

Create the test Javabeans

@Data
public class Student implements Serializable {
    private static final long serialVersionUID = -2987207599880734028L;
    private int id;
    private String name;
    private String sex;

    public Student(a){}public Student(int id, String name, String sex){
        this.id = id;
        this.name = name;
        this.sex = sex; }}Copy the code

Next, export the Excel file in the program as follows

@RequestMapping("/excel")
public void getExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // Prepare data
    List<Student> list = new ArrayList<Student>();
    for (int i = 0; i < 10; i++) {
        list.add(new Student(111."Zhang"."Male"));
        list.add(new Student(111."Bill"."Male"));
        list.add(new Student(111."Fifty"."Female"));
    }
    String[] columnNames = { "ID"."Name"."Gender"};
    String fileName = "springboot_excel";
    ExportExcelWrapper<Student> util = new ExportExcelWrapper<Student>();
    util.exportExcel(fileName, fileName, columnNames, list, response, ExportExcelUtils.EXCEL_FILE_2003);
}
Copy the code

The exported file is as follows

Export Excel through Annotation-based Web

If the Project is based on Java Web or Spring MVC and needs to export Excel based on annotations, add the following configuration to the pom. XML file of the project

<dependency>
    <groupId>io.mykit.excel</groupId>
    <artifactId>mykit-excel-servlet</artifactId>
    <version>1.0.0 - the SNAPSHOT</version>
</dependency>
Copy the code

Create the test Javabeans

(1) Create the parent JavaBean class

@Data
public class Person implements Serializable {
    private static final long serialVersionUID = 3251965335162340137L;

    @excelcolumn (isExport = true, title = "id ", sort = 2)
    private String id ;

    @excelcolumn (isExport = true, title = "name ", sort = 3)
    private String name;
    
    public Person(a){}public Person(String id, String name){
        this.id = id;
        this.name = name; }}Copy the code

(2) Create a subclass JavaBean

@Data
public class Student extends Person{
    private static final long serialVersionUID = -6180523202831503132L;

    @excelcolumn (isExport = false, title = "class id ", sort = 1)
    private String classNo;

    private Integer score;

    @excelcolumn (isExport = true, title = "类 ", sort = 5)
    private String hobby;
    
    public Student(a){}public Student(String id, String name, String classNo, Integer score, String hobby){
        super(id, name);
        this.classNo = classNo;
        this.score = score;
        this.hobby = hobby; }}Copy the code

Next, export the Excel file in the program as follows

@Controller
@RequestMapping(value = "/annotation/export")
public class AnnotationExportExcelController {

    @RequestMapping("/excel")
    public void getExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // Prepare data
        List<Student> list = new ArrayList<Student>();
        for (int i = 1; i <= 10; i++) {
            list.add(new Student("00" + i, "Zhang"."001".100."Basketball"));
        }
        String fileName = "springboot_excel";
        ExportExcelWrapper<Student> wrapper = newExportExcelWrapper<Student>(); wrapper.annotationExportExcel(fileName, fileName, list, Student.class, response, ExportExcelWrapper.EXCEL_FILE_2003); }}Copy the code

The exported file is as follows

Front-end test code

The front-end test code is placed in the SRC /main/resources/ HTML directory of the Mykit-Excel – Springboot module. After modifying the connection address in the HTML file, put it in Tomcat or other Web containers for testing.

The test way

Routine test

Run directly mykit – excel – springboot project of IO. Mykit. Excel. Springboot. Normal. Export. TestExportExcelUtils class

Annotation-based general testing

Run directly mykit – excel – springboot project of IO. Mykit. Excel. Springboot. The annotation. Export. TestAnnotationExportExcelUtils class

Web testing

(1) start mykit – excel – springboot projects, namely running mykit – excel – springboot project of IO. Mykit. Excel. Springboot. MykitExcelCoreApplication class

(2) Publish normalExportExcel. HTML file under SRC /main/resources/ HTML of mykit-excel- Springboot project to Web container such as Tomcat to access normalExportExcel Open the page and click “Submit” button.

Annotation-based Web testing

(1) start mykit – excel – springboot projects, namely running mykit – excel – springboot project of IO. Mykit. Excel. Springboot. MykitExcelCoreApplication class.

(2) will be mykit – excel – springboot project of SRC/main/resources/HTML annotationExportExcel. HTML documents published to visit annotationExportExce Tomcat Web container etc Link address of L.HTML file, open the page and click “Submit” button.

Big welfare

WeChat search the ice technology WeChat 】 the public, focus on the depth of programmers, daily reading of hard dry nuclear technology, the public, reply within [PDF] have I prepared a line companies interview data and my original super hardcore PDF technology document, and I prepared for you more than your resume template (update), I hope everyone can find the right job, Learning is a way of unhappy, sometimes laugh, come on. If you’ve worked your way into the company of your choice, don’t slack off. Career growth is like learning new technology. If lucky, we meet again in the river’s lake!

In addition, I open source each PDF, I will continue to update and maintain, thank you for your long-term support to glacier!!

Write in the last

If you think glacier wrote good, please search and pay attention to “glacier Technology” wechat public number, learn with glacier high concurrency, distributed, micro services, big data, Internet and cloud native technology, “glacier technology” wechat public number updated a large number of technical topics, each technical article is full of dry goods! Many readers have read the articles on the wechat public account of “Glacier Technology” and succeeded in job-hopping to big factories. There are also many readers to achieve a technological leap, become the company’s technical backbone! If you also want to like them to improve their ability to achieve a leap in technical ability, into the big factory, promotion and salary, then pay attention to the “Glacier Technology” wechat public account, update the super core technology every day dry goods, so that you no longer confused about how to improve technical ability!