Java code analysis back-end code to export excel spreadsheet

I. Background

1. doing the project will meet to check out the data made into excel spreadsheet downloaded to the specified location, and today we took off in terms of the specific code operation, ado, directly on the source operating.

Second, the project code

1.pom.xml

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

2.controller

@RequestMapping("/exportExcel")
@Controller
public class exportExcelController {

    @Autowired
    private UserService userService;

    @RequestMapping("/exportExcel")
    public void export(HttpServletResponse response) {
        //从数据库查询出数据
        List<Users> list = userService.selectAll();
        // 创建excel
        HSSFWorkbook wk = new HSSFWorkbook();
        // 创建一张工作表
        HSSFSheet sheet = wk.createSheet("用户表");
        // 设置工作表中的1-3列的宽度
        sheet.setColumnWidth(0, 5000);
        sheet.setColumnWidth(1, 5000);
        sheet.setColumnWidth(2, 5000);
        //创建第一行
        HSSFRow row1 = sheet.createRow(0);
        // 创建第一行的第一个单元格
        // 向单元格写值
        HSSFCell cell = row1.createCell(0);
        cell.setCellValue("用户表");
        //合并单元格CellRangeAddress构造参数依次表示起始行,截止行,起始列,截至列。
        //0表示 第一行第一列
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,2));
        //创建第二行
        HSSFRow row2 = sheet.createRow(1);
        row2.createCell(0).setCellValue("登录名");
        row2.createCell(1).setCellValue("年龄");
        row2.createCell(2).setCellValue("昵称");
        // 创建第一行
        for (int i = 0; i < list.size(); i++) {
            //创建行    一条数据一行
            HSSFRow row = sheet.createRow(i + 2);
            row.createCell(0).setCellValue(list.get(i).getName());
            row.createCell(1).setCellValue(list.get(i).getAge());
            row.createCell(2).setCellValue(list.get(i).getNickName());
        }
        try {
            /**
             * 弹出下载选择路径框
             */
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String str = sdf.format(date);
            response.setContentType("application/octet-tream");
            response.setHeader("Content-disposition", "attachment;filename=" + str + ".xls");// 默认Excel名称
            response.flushBuffer();
            //wk.write(response.getOutputStream());
            wk.write(new FileOutputStream(new File("D://daochu")));
            wk.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3.service

public interface UserService {
    //List<User> getByIds(List<Integer> ids);
    List<Users> selectAll();
}

4.serviceImpl

@Service("userService")
public class UserServiceImpl implements UserService {
    @Override
    public List<Users> selectAll() {
        ArrayList<Users> usersList = new ArrayList<>();
        Users users = new Users();
        users.setName("chenmingxu");
        users.setAge(18);
        users.setNickName("cmx");
        usersList.add(users);
        return usersList;
    }
}

5.pojo

public class Users {
    private String name;
    private  int age;
    private String nickName;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
}

Third, the test

1.postman access

Fourth, the results

1. See D: / douchu

Fifth, the end of the

1. Reference blog: https://www.cnblogs.com/zuoxh/p/9760058.html

2.Always keep the faith!!!

Published 122 original articles · won praise 64 · views 50000 +

Guess you like

Origin blog.csdn.net/chenmingxu438521/article/details/104212006