springboot里 运用 easyexcel 导出

引入pom

<dependency>
			<groupId>com.alibabagroupId>
			<artifactId>easyexcelartifactId>
			<version>2.2.6version>
dependency>

运用

import com.alibaba.excel.EasyExcel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/easy/excel")
public class EasyExcelController {

    @GetMapping("/tttt")
    public void tttt(HttpServletResponse response) throws Exception
    {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        // 表头
        List<List<String>> heads = new ArrayList<List<String>>();

        List<String> head0 = new ArrayList<>();
        head0.add("字符id");
        List<String> head1 = new ArrayList<>();
        head1.add("合并1");
        head1.add("合并2");
        List<String> head2 = new ArrayList<>();
        head2.add("数量1");
        List<String> head3 = new ArrayList<>();
        head3.add("小数");
        List<String> head4 = new ArrayList<>();
        head4.add("    时    间    ");
        heads.add(head0);
        heads.add(head1);
        heads.add(head2);
        heads.add(head3);
        heads.add(head4);



        List<List<Object>> rt = new ArrayList<List<Object>>();

        List<Object> row =new ArrayList<>();
        row.add("UID1");
        row.add("name1");
        row.add(7);
        row.add(1.2);
        row.add(new Date());
        rt.add(row);

        List<Object> row2 =new ArrayList<>();
        row2.add("UID2");
        row2.add("name2");
        row2.add(1);
        row2.add(3.4);
        row2.add(new Date());
        rt.add(row2);
        EasyExcel.write(response.getOutputStream()).head(heads).sheet("sheet名").doWrite(rt);
    }
}

官网API

效果

springboot里 运用 easyexcel 导出_第1张图片

你可能感兴趣的:(Java项目,spring,boot,后端,java)