Use EasyPoi to realize the simplest export function....

In my work, I met a need to export excel, looked at the basic POI used by my colleagues, and wrote more than 100 lines of code emmm for tool class, business class and new object!

I believe that the young partners should encounter this problem more or less in their work, or they who feel interested can also refer to it; There are many tools for exporting poi. In today's issue, I will use EasyPoi, which can be done in a few lines of code...

First, open Idea (this is not nonsense) cough (seriously) the first step is to import dependencies..

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>3.2.0</version>
</dependency>

I don't know if you have ever encountered this problem. Sometimes the idea will report red if you rely on import.. emmm.. I don't know how to solve it. Anyway, my solution is to restart idea If it's a micro service, you must look at the maven on the right side of the idea. Where they put some dependencies, you follow the big army. Just look at whether there are references between services.

The second step is the entity class. Because it is difficult to understand in my project, let's simulate it

//This annotation is equal to the get set method. If you want to know, you can check it
@Data
public class User(){

//The name = "" in this annotation is the list of excel you want
@excel(name="full name")
private Sting name;

@excel(name="Gender")
private Sting sex;

@excel(name="Age")
private Sting age;

}

The third step is to write dao. Some are also called mapper In fact, a bird; You can take the conditions to query. The front-end value transfer is OK. I don't write the conditions here.

public interface UserDao extends BaseMapper<User> {

List<User> selectAll();
}

If you query with conditions, don't forget to add @ Param("") before the conditions Don't ask me why I remind you (a friend of mine forgot to write it, not me, not me...)

The fourth part is to write mapper XML Mapping file..

<resultMap id="UserMap" type="com.dao.UserDao">
        <result column="Name" property="name"/>
        <result column="Age" property="age"/>
        <result column="Sex" property="sex"/>

    </resultMap>
  <select id="selectAll" resultMap="UsreMap">
    select * from User

    </select>

The fifth step must be service, which will be completed soon...

public interface UserService{


List<User> selectUser();

}

Step 6 business class. If you want to write some functional logic, it's up to you

//Don't forget to add this note here
@Service
public class UserServiceImpl implements UserServie {

//This annotation is to find a class of type UserDao in the spring container and inject it.
@Autowired
    private UserDao userdao;


 @Override
    public List<User> electUser(){
    
    reuter userdao.selectAll();
}



}

The seventh part, the most important step.

@RestController
//The path for the front end to access this controller
@RequestMapping(value = "User")
public class PricePlanExportController {

//It's said above, so I won't say it
@Autowired
    private UserService userService;

//This is the path to access this interface
@GetMapping(value = "excel")

//response is a servlet A parameter of the service method. Family members who want to know more about it can check it.
//We throw IO exceptions here
public void UserExcle(HttpServletResponse response)throws IOException{

        //System.out.println("set file name, format and code set");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("Write here excel Name of.xls", "utf-8"));
        response.setContentType("application/vnd.ms-excel");

        //new timestamps, easy to see how long it took
        Date start = new Date();

        //Get the data you want to check here    
        List<User> userList=userService.selectSser();

        ExportParams exportParams = new ExportParams("Yours excle Table information name", "userList(Return values of all the above data)");
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, User(Entity class).class, userList);
        workbook.write(response.getOutputStream());

        System.out.println(new Date().getTime() - start.getTime()+"Timing end");

    }
}

OK, this simple but not simple poi export excel is finished. I hope it can help people at home...

Keywords: Java

Added by RamboJustRambo on Sun, 16 Jan 2022 02:43:11 +0200