当前位置:   article > 正文

Java读取Hbase数据,使用POI操作Excel模板,并定时发送带有Excel附件的邮件_java定时发送excel邮件(2)

Java读取Hbase数据,使用POI操作Excel模板,并定时发送带有Excel附件的邮件_java定时发送excel邮件(2)

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

//读取excel模板
ClassPathResource resource = new ClassPathResource(template);
InputStream in = resource.getInputStream();
XSSFWorkbook wb = new XSSFWorkbook(in);

  • 1
  • 2
  • 3
  • 4
  • 5

二、读取Hbase数据

1.创建Hbase连接和查询条件

代码如下:需求查询两个hbase表数据,可根据实际需求进行调整

        String tomorrowDateStr = tomorrowDateStr();
        String splitPrefix = tomorrowDateStr.substring(tomorrowDateStr.length() - 1);
        String rowKey = splitPrefix + "-" + tomorrowDateStr;
        try {
            Connection conn = null;
            Table inbndHTable = null;
            Table transHTable = null;

            conn = hBaseDataSource.getConnection();
            inbndHTable = HTableUtil.getTable(conn, HbaseName1);
            transHTable = HTableUtil.getTable(conn, HbaseName2);

            Scan inbndScan = new Scan();
            inbndScan.setCaching(conn.getConfiguration().getInt(HConstants.HBASE_CLIENT_SCANNER_CACHING, 10000));
            inbndScan.setCacheBlocks(true);
            inbndScan.setRowPrefixFilter(Bytes.toBytes(rowKey));

            Scan transScan = new Scan();
            transScan.setCaching(conn.getConfiguration().getInt(HConstants.HBASE_CLIENT_SCANNER_CACHING, 10000));
            transScan.setCacheBlocks(true);
            transScan.setRowPrefixFilter(Bytes.toBytes(rowKey));        

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

2.获取数据

代码如下(示例):

Map<String, Map<String, byte[]>> inbndckiResults = HTableUtil.scan(inbndHTable, inbndScan);
Map<String, Map<String, byte[]>> transckiResults = HTableUtil.scan(transHTable, transScan);

  • 1
  • 2
  • 3

三、向Excel模板写入相关数据,并生成本地文件

public void exportTemplateSendEmail(List platinumForecastLists, List platinumTeansForecastLists) throws Exception {
        Date date = new Date();//取时间
        String todayStr = sdf1.format(date);
        //读取excel模板
        ClassPathResource resource = new ClassPathResource(template);
        InputStream in = resource.getInputStream();
        XSSFWorkbook wb = new XSSFWorkbook(in);
        XSSFSheet indndSheet = wb.getSheetAt(0);
        XSSFSheet transSheet = wb.getSheetAt(1);

        XSSFCellStyle style = wb.createCellStyle();
        //单元格边框属性设置
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);

        indndSheet.createRow(1).createCell(0).setCellValue("日期:" + todayStr);
        transSheet.createRow(1).createCell(0).setCellValue("日期:" + todayStr);
        indndSheet.getRow(1).getCell(0).setCellStyle(style);
        transSheet.getRow(1).getCell(0).setCellStyle(style);
        indndSheet.getRow(1).createCell(7).setCellStyle(style);
        transSheet.getRow(1).createCell(10).setCellStyle(style);

        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setAlignment(HorizontalAlignment.CENTER);

        for (int i = 0; i < platinumForecastLists.size(); i++) {
            PlatinumInbndForecastBean platinumForecastBean = (PlatinumInbndForecastBean) platinumForecastLists.get(i);
            // row从0开始  cell 从0开始的
            indndSheet.createRow(i + 3).createCell(0).setCellValue(i + 1);
            indndSheet.getRow(i + 3).createCell(1).setCellValue(platinumForecastBean.getFullName());
            indndSheet.getRow(i + 3).createCell(2).setCellValue(platinumForecastBean.getDepartureTime());
            indndSheet.getRow(i + 3).createCell(3).setCellValue(platinumForecastBean.getFlightNum());
            indndSheet.getRow(i + 3).createCell(4).setCellValue(platinumForecastBean.getFQTVNumber());
            indndSheet.getRow(i + 3).createCell(6).setCellValue(platinumForecastBean.getCheckInInfoStatus());
            indndSheet.getRow(i + 3).createCell(7).setCellValue(platinumForecastBean.getTerminal());
            indndSheet.getRow(i + 3).getCell(0).setCellStyle(style);
            indndSheet.getRow(i + 3).getCell(1).setCellStyle(style);
            indndSheet.getRow(i + 3).getCell(2).setCellStyle(style);
            indndSheet.getRow(i + 3).getCell(3).setCellStyle(style);
            indndSheet.getRow(i + 3).getCell(4).setCellStyle(style);
            indndSheet.getRow(i + 3).createCell(5).setCellStyle(style);
            indndSheet.getRow(i + 3).getCell(6).setCellStyle(style);
            indndSheet.getRow(i + 3).getCell(7).setCellStyle(style);
        }

        for (int i = 0; i < platinumTeansForecastLists.size(); i++) {
            PlatinumTransForecastBean platinumForecastBean = (PlatinumTransForecastBean) platinumTeansForecastLists.get(i);
            // row从0开始  cell 从0开始的
            transSheet.createRow(i + 3).createCell(0).setCellValue(i + 1);
            transSheet.getRow(i + 3).createCell(1).setCellValue(platinumForecastBean.getInbndFltNum());
            transSheet.getRow(i + 3).createCell(2).setCellValue(platinumForecastBean.getArrTime());
            transSheet.getRow(i + 3).createCell(3).setCellValue(platinumForecastBean.getLastSeg());
            transSheet.getRow(i + 3).createCell(4).setCellValue(platinumForecastBean.getFullName());
            transSheet.getRow(i + 3).createCell(5).setCellValue(platinumForecastBean.getTransFltNum());
            transSheet.getRow(i + 3).createCell(6).setCellValue(platinumForecastBean.getDepTime());
            transSheet.getRow(i + 3).createCell(7).setCellValue(platinumForecastBean.getNextSeg());
            transSheet.getRow(i + 3).createCell(9).setCellValue(platinumForecastBean.getFQTVNumber());
            transSheet.getRow(i + 3).createCell(10).setCellValue(platinumForecastBean.getTelNum());


            transSheet.getRow(i + 3).getCell(0).setCellStyle(style);
            transSheet.getRow(i + 3).getCell(1).setCellStyle(style);
            transSheet.getRow(i + 3).getCell(2).setCellStyle(style);
            transSheet.getRow(i + 3).getCell(3).setCellStyle(style);
            transSheet.getRow(i + 3).getCell(4).setCellStyle(style);
            transSheet.getRow(i + 3).getCell(5).setCellStyle(style);
            transSheet.getRow(i + 3).getCell(6).setCellStyle(style);
            transSheet.getRow(i + 3).getCell(7).setCellStyle(style);
            transSheet.getRow(i + 3).createCell(8).setCellStyle(style);
            transSheet.getRow(i + 3).getCell(9).setCellStyle(style);
            transSheet.getRow(i + 3).getCell(10).setCellStyle(style);
        }
        indndSheet.createFreezePane(0, 3, 0, 3);
        transSheet.createFreezePane(0, 3, 0, 3);

        //写入磁盘保存文件
        FileOutputStream output = new FileOutputStream(patinumFileName);
        wb.write(output);
        output.close();

        //作为输出流发邮件
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        wb.write(byteArrayOutputStream);
        List<String> emailList = new ArrayList<>();
        emailList.add("自定义邮箱");


        for (String email : emailList) {
            boolean rst = sendMail(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()), "白金卡预报", email);
            if (rst) {
                LOGGER.info("向" + email + "邮件发送成功!");
            } else {
                LOGGER.info("向" + email + "邮件发送失败!");
            }
        }


    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101

四、将新生成的Excel以流的方式,进行邮件发送

public boolean sendMail(InputStream is, String fileName, String toMail) {
        Transport transport = null;
        try {
            System.setProperty("mail.mime.splitlongparameters", "false");
            Properties props = new Properties();
            // 设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", port);
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.ssl.trust", "\*");
            // 需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)
            props.put("mail.smtp.auth", "true");
            // 用刚刚设置好的props对象构建一个session
            Session session = Session.getDefaultInstance(props);
            // 有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使
            // 用(你可以在控制台(console)上看到发送邮件的过程)
            session.setDebug(false);


![img](https://img-blog.csdnimg.cn/img_convert/4b0f5c2b673435aad3488994fd9aeb7c.png)
![img](https://img-blog.csdnimg.cn/img_convert/93ccbc339a11b3883e159e576ccfdd34.png)
![img](https://img-blog.csdnimg.cn/img_convert/d87a953ad6348563463ac74c0dd1178b.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**

)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/591720
推荐阅读
相关标签
  

闽ICP备14008679号