Easypoi导出数据 时间类型为时间戳 显示时间为年月日时分秒 最终版

先看一下数据库和时间戳的转换
在这里插入图片描述
java代码

@Test
    public void test2() throws Exception {
        List<Ceshi> ceshiAll = sysCeshiService.getCeshiAll();

        ceshiAll.forEach(e->e.setUpdateTime(FormatDateUtil.stampToTime(Long.parseLong(e.getUpdateTime()))));

        //标题  表名  导出类型  HSSF xls  XSSF xlsx
        ExportParams exportParams = new ExportParams("测试表","ceshi", ExcelType.XSSF);
        //1.导出参数对象 1.普通Java类的类对象(要导出的实体类的类对象) 3.一个集合 查询全部的用户信息
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Ceshi.class,ceshiAll);
        workbook.write(new FileOutputStream(new File("E://easypoi.xlsx")));
    }

工具类

	/* //时间戳转换成String日期 */
    public static String stampToTime(long stamp) {
        String sd = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sd = sdf.format(new Date(stamp)); // 时间戳转换日期
        return sd;
    }

实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Table(name = "ceshi")
public class Ceshi {

    @Id
    @Excel(name = "主键")
    private Integer id;
    @Excel(name = "姓名")
    private String name;
    @Excel(name = "密码")
    private String password;
//    @Excel(name = "创建时间",exportFormat="yyyy-MM-dd HH:mm:ss",importFormat = "yyyy-MM-dd HH:mm:ss",width = 30)
    @Excel(name = "创建时间")
    private String updateTime;

}

运行后 打开.xlsx文件
在这里插入图片描述
下面粘贴一下企业级导出数据代码应用示例

@Controller
@RequestMapping("/ceshi")
public class SysCeshiController {

    @Autowired
    private SysCeshiService sysCeshiService;

    @RequestMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        //存储的本地磁盘的路径  在已创建的文件夹中创建文件
        List<Ceshi> ceshiAll = sysCeshiService.getCeshiAll();

        ceshiAll.forEach(e->e.setUpdateTime(FormatDateUtil.stampToTime(Long.parseLong(e.getUpdateTime()))));

        //标题  表名  导出类型  HSSF xls  XSSF xlsx
        ExportParams exportParams = new ExportParams("测试表","ceshi", ExcelType.XSSF);
        //1.导出参数对象 1.普通Java类的类对象(要导出的实体类的类对象) 3.一个集合 查询全部的用户信息
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Ceshi.class,ceshiAll);

        /*String fileName="ceshi.xlsx";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream;charset=UTF-8");
        fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); // 为了解决中文名称乱码问题
        response.setHeader("Content-Disposition", "attachment;filename="+ fileName);

        workbook.write(response.getOutputStream());
        response.getOutputStream().flush();
        response.getOutputStream().close();*/


        String path="E://";
        String fileName="ceshi.xlsx";

        File filePath = new File(path);
        if (!filePath.exists()){
            filePath.mkdirs();
        }
        try {
            File file = new File(filePath, fileName);
            FileOutputStream fos = new FileOutputStream(file);
            workbook.write(fos);
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

工作中可能还会遇到其他的业务,比如说导出图片。这里我还没有遇到该业务,只能遇到时候在解决。今天就先写到这里。
这辈子坚持与不坚持都不可怕,怕的是独自走在坚持的道路上!

欢迎加入技术群聊

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/taiguolaotu/article/details/107089610
今日推荐