需求

后台生成周报月报季报年报Excel,将文件下载链接推送给对应客户

开发思路:

1.根据选定日期生成周报,月报,季报,年报数据
2.将这些数据报告生成Excel表格
3.把生成的文件上传到腾讯云对象存储服务器
4.将服务器返回的url存储到数据库

工具

poi-3.14-20160307.jar(点击可下载)

数据

获取数据部分省略了

代码

主方法

public boolean addReportExcelToCloud(ReportResult rr) {OutputStream out = new ByteArrayOutputStream();ExcelProjectUtils eu = new ExcelProjectUtils();eu.exportExcel(rr, out);   //<1>ConvertUtil cu = new ConvertUtil();try {ByteArrayInputStream byteInput = cu.parse(out);String rs = PicUploadToYun.uploadExcel(SysContent.getFileRename("案场数据报.xls"), byteInput);  //<2>addReportExcelToDB(rr, rs);  //<3>return true;} catch (Exception e) {e.printStackTrace();}return false;}

<1> 将数据生成二进制Excel文件 (方法详细见下面代码)
<2> 将生成的二进制文件上传到腾讯云对象存储服务器 (方法详细见下面代码)
<3> 将服务器返回的url存储到数据库 (方法详细见下面代码)

/*** 周报年报生成excel* * @param report* @param out*/public void exportExcel(ReportResult report, OutputStream out) {// 判断传入的时间间隔String dateStr = "";String reportName = "";List<String> dateCount = DateUtil.getTwoDateEveryDay(report.getStartTime(), report.getEndTime());if (dateCount.size() <= 7) {dateStr += "本周";reportName += "案场周报";} else if (dateCount.size() >= 28 && dateCount.size() <= 31) {dateStr += "本月";reportName += "案场月报";} else if (dateCount.size() >= 85 && dateCount.size() <= 100) {dateStr += "本季度";reportName += "案场季报";} else if (dateCount.size() >= 180 && dateCount.size() <= 185) {dateStr += "本半年度";reportName += "案场半年报";} else if (dateCount.size() >= 360 && dateCount.size() <= 367) {dateStr += "本年度";reportName += "案场年报";} else {dateStr += "时间段内";reportName += "案场阶段报";}report.setReportName(reportName);// 声明一个工作薄HSSFWorkbook workbook = new HSSFWorkbook();// 生成一个表格HSSFSheet sheet = workbook.createSheet(report.getReportName() + report.getStartTime() + " - " + report.getEndTime());// 设置表格默认列宽度为100个字节sheet.setDefaultColumnWidth((short) 100);/** ----------样式一:标题 ------------ **/HSSFCellStyle style = workbook.createCellStyle();// 设置这些样式style.setBorderLeft(HSSFCellStyle.BORDER_THIN);style.setBorderRight(HSSFCellStyle.BORDER_THIN);//style.setBorderTop(HSSFCellStyle.BORDER_THIN);style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 生成一个字体HSSFFont font = workbook.createFont();font.setFontName("宋体");//font.setColor(HSSFColor.VIOLET.index);font.setFontHeightInPoints((short) 14);font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 把字体应用到当前的样式style.setFont(font);/***---------样式二:小标题---------***/HSSFCellStyle style2 = workbook.createCellStyle();style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);style2.setBorderRight(HSSFCellStyle.BORDER_THIN);//style2.setBorderTop(HSSFCellStyle.BORDER_THIN);style2.setAlignment(HSSFCellStyle.ALIGN_LEFT);//style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 生成另一个字体HSSFFont font2 = workbook.createFont();//font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);font2.setFontName("宋体");font2.setFontHeightInPoints((short) 11);font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 把字体应用到当前的样式style2.setFont(font2);/***    样式三:右侧日期       ***/HSSFCellStyle style3 = workbook.createCellStyle();//样式style3.setBorderLeft(HSSFCellStyle.BORDER_THIN);style3.setBorderRight(HSSFCellStyle.BORDER_THIN);style3.setAlignment(HSSFCellStyle.ALIGN_RIGHT);style3.setBorderBottom(HSSFCellStyle.BORDER_THIN);//字体HSSFFont font3 = workbook.createFont();font3.setFontName("宋体");font3.setFontHeightInPoints((short) 11);style3.setFont(font3);/**       样式四:主内容        ***/HSSFCellStyle style4 = workbook.createCellStyle();//样式style4.setBorderLeft(HSSFCellStyle.BORDER_THIN);style4.setBorderRight(HSSFCellStyle.BORDER_THIN);style4.setAlignment(HSSFCellStyle.ALIGN_LEFT);//字体HSSFFont font4 = workbook.createFont();font4.setFontName("宋体");font4.setFontHeightInPoints((short) 11);style4.setFont(font4);/**       样式五:底侧空内容       ***/HSSFCellStyle style5 = workbook.createCellStyle();//样式style5.setBorderLeft(HSSFCellStyle.BORDER_THIN);style5.setBorderRight(HSSFCellStyle.BORDER_THIN);style5.setAlignment(HSSFCellStyle.ALIGN_LEFT);style5.setBorderBottom(HSSFCellStyle.BORDER_THIN);//字体HSSFFont font5 = workbook.createFont();font5.setFontName("宋体");font5.setFontHeightInPoints((short) 11);style5.setFont(font5);// 声明一个画图的顶级管理器HSSFPatriarch patriarch = sheet.createDrawingPatriarch();// 定义注释的大小和位置,详见文档HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));// 设置注释内容comment.setString(new HSSFRichTextString("数据报"));// 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.comment.setAuthor("saas");// 产生表格标题行 -- 项目名称HSSFRow row = sheet.createRow(0);createCellAndRow(style4, report.getProjectName(), row);// 产生表格标题行 -- 周报名称row = sheet.createRow(1);createCellAndRow(style, report.getReportName(), row);// 产生表格标题行 -- 起始时间-终止时间row = sheet.createRow(2);String startTime = DateUtil.format(DateUtil.parse(report.getStartTime(), DateUtil.PATTERN_CLASSICAL_SIMPLE),DateUtil.PATTERN_CLASSICAL_SIMPLE_YMD);String endTime = DateUtil.format(DateUtil.parse(report.getEndTime(), DateUtil.PATTERN_CLASSICAL_SIMPLE),DateUtil.PATTERN_CLASSICAL_SIMPLE_YMD);String date = "日期:" + startTime + " - " + endTime;createCellAndRow(style3, date, row);// 接访情况标题row = sheet.createRow(3);createCellAndRow(style2, "·接访情况", row);// 接访客户组数row = sheet.createRow(4);Integer visitCount = report.getVisitCount();String visitNum = "1、" + dateStr + "共计接访客户" + visitCount + "组,来访量";if (visitCount < 40) {visitNum += "较少,有待提升";} else if (visitCount >= 41 && visitCount <= 99) {visitNum += "尚可,还有提高空间";} else if (visitCount >= 100 && visitCount <= 139) {visitNum += "很多";} else if (visitCount > 140) {visitNum += "火爆";}createCellAndRow(style4, visitNum, row);// 有效接访率row = sheet.createRow(5);Double visitRate = new Double(report.getValidVisitRate());String visitRateStr = "2、有效接访率为" + visitRate + "%,接访成效";if (visitRate < 50) {visitRateStr += "较低,有待提升";} else if (visitRate >= 50 && visitRate <= 65) {visitRateStr += "尚可,还有提高空间";} else if (visitRate >= 65 && visitRate <= 80) {visitRateStr += "很高";} else if (visitRate > 80) {visitRateStr += "极高";}createCellAndRow(style4, visitRateStr, row);// 首访有效率row = sheet.createRow(6);Double newVisitRate = new Double(report.getValidNewCuVisitRate());String newVisitStr = "3、首访有效率为" + newVisitRate + "%,来访转储客的概率";if (newVisitRate < 40) {newVisitStr += "较差,有待提升";} else if (newVisitRate >= 40 && newVisitRate <= 60) {newVisitStr += "尚可,还有提高空间";} else if (newVisitRate >= 60 && newVisitRate <= 75) {newVisitStr += "很高";} else if (newVisitRate > 75) {newVisitStr += "极高";}createCellAndRow(style4, newVisitStr, row);// 老客户接访占比row = sheet.createRow(7);Double oldVisitRate = new Double(report.getOldCuVisitRate());String oldVisitStr = "4、老客户接访比为" + oldVisitRate + "%,老客户接访的占比";if (oldVisitRate < 20) {oldVisitStr += "较低";} else if (oldVisitRate >= 20 && oldVisitRate <= 40) {oldVisitStr += "尚可";} else if (oldVisitRate >= 40 && oldVisitRate <= 60) {oldVisitStr += "很高";} else if (oldVisitRate > 60) {oldVisitStr += "极高";}createCellAndRow(style4, oldVisitStr, row);//空行row = sheet.createRow(8);createCellAndRow(style4, "", row);// 储客情况row = sheet.createRow(9);createCellAndRow(style2, "·储客情况", row);// 新增储客row = sheet.createRow(10);Integer newCuCount = report.getNewCuCount();String newCuStr = "1、" + dateStr + "新增储客" + newCuCount + "组,新增量";if (newCuCount < 30) {newCuStr += "较少,有待提升";} else if (newCuCount >= 31 && newCuCount <= 60) {newCuStr += "尚可,还有提高空间";} else if (newCuCount >= 61 && newCuCount <= 79) {newCuStr += "很多";} else if (newCuCount > 80) {newCuStr += "爆满";}createCellAndRow(style4, newCuStr, row);// 累计老客户row = sheet.createRow(11);Integer oldCuCount = report.getTotalOldCuCount();Integer totalCuCount = report.getTotalCuCount();Double oldCuRate = new Double(SysContent.getTwoNumberForValue(oldCuCount, totalCuCount));String oldCuStr = "2、累计老客户总量为" + oldCuCount + "组,老客户占比为" + oldCuRate + "%,显示老客户关注度";if (oldCuRate < 15) {oldCuStr += "较低,有待提升";} else if (oldCuRate >= 15 && oldCuRate <= 25) {oldCuStr += "尚可,还有提高空间";} else if (oldCuRate >= 25 && oldCuRate <= 40) {oldCuStr += "很高";} else if (oldCuRate > 40) {oldCuStr += "极高";}createCellAndRow(style4, oldCuStr, row);// 累计总储客row = sheet.createRow(12);String totalOldCuStr = "3、累计总储客" + totalCuCount + "组";createCellAndRow(style4, totalOldCuStr, row);// 成交情况(周报没有,其他有)if (report.getSubscribeHouseCount() != null) {//空行row = sheet.createRow(13);createCellAndRow(style4, "", row);row = sheet.createRow(14);createCellAndRow(style2, "·成交情况", row);// 新增认购套数row = sheet.createRow(15);Integer subscribeHouseCount = report.getSubscribeHouseCount();Double subscribeHouseRate = new Double(report.getSubscribeHouseRate());String subscribeHouseStr = "1、" + dateStr + "新增认购套数" + subscribeHouseCount + "套,较" + dateStr + "同期";if (subscribeHouseRate < 0) {subscribeHouseStr += "减少";} else {subscribeHouseStr += "增长";}subscribeHouseStr += Math.abs(subscribeHouseRate) + "%";createCellAndRow(style4, subscribeHouseStr, row);// 新增认购金额row = sheet.createRow(16);Long subscribeMoney = report.getSubscribeMoney();Double subscribeMoneyRate = new Double(report.getSubscribeMoneyRate());String subscribeMoneyStr = "   新增认购金额" + subscribeMoney + "万元,较" + dateStr + "同期";if (subscribeHouseRate < 0) {subscribeMoneyStr += "减少";} else {subscribeMoneyStr += "增长";}subscribeMoneyStr += Math.abs(subscribeMoneyRate) + "%";createCellAndRow(style4, subscribeMoneyStr, row);// 新增签约套数row = sheet.createRow(17);Integer signCount = report.getSignCount();Double signRate = new Double(report.getSignRate());String signStr = "2、新增签约套数" + signCount + "套,较" + dateStr + "同期";if (signRate < 0) {signStr += "减少";} else {signStr += "增长";}signStr += Math.abs(signRate) + "%";createCellAndRow(style4, signStr, row);// 新增签约金额row = sheet.createRow(18);Long signHouseMoney = report.getSignHouseMoney();Double signHouseMoneyRate = new Double(report.getSignHouseMoneyRate());String signHouseMoneyStr = "   新增签约金额" + signHouseMoney + "万元,较" + dateStr + "同期";if (signHouseMoneyRate < 0) {signHouseMoneyStr += "减少";} else {signHouseMoneyStr += "增长";}signHouseMoneyStr += Math.abs(signHouseMoneyRate) + "%";createCellAndRow(style4, signHouseMoneyStr, row);// 新接访签约率row = sheet.createRow(19);Double newCustomerSignedRate = new Double(report.getNewCustomerSignedRate());String newCustomerSignedStr = "3、" + dateStr + "新客户接访签约率" + newCustomerSignedRate + "%,接访签约概率";if (newCustomerSignedRate < 4) {newCustomerSignedStr += "较低,与理想值差距大";} else if (newCustomerSignedRate >= 4 && newCustomerSignedRate <= 6) {newCustomerSignedStr += "尚可,还有提高空间";} else if (newCustomerSignedRate >= 6 && newCustomerSignedRate <= 7) {newCustomerSignedStr += "很高";} else if (newCustomerSignedRate > 7) {newCustomerSignedStr += "非常高";}createCellAndRow(style4, newCustomerSignedStr, row);// 储客签约率row = sheet.createRow(20);Double momeryCustomerSignedRate = new Double(report.getMomeryCustomerSignedRate());String momeryCustomerSignedStr = "4、储客签约率" + momeryCustomerSignedRate + "%,储备客户签约概率";if (momeryCustomerSignedRate < 7) {momeryCustomerSignedStr += "较低,与理想值差距大";} else if (momeryCustomerSignedRate >= 7 && momeryCustomerSignedRate <= 12) {momeryCustomerSignedStr += "尚可,还有提高空间";} else if (momeryCustomerSignedRate >= 12 && momeryCustomerSignedRate <= 15) {momeryCustomerSignedStr += "很高";} else if (momeryCustomerSignedRate > 15) {momeryCustomerSignedStr += "非常高";}createCellAndRow(style4, momeryCustomerSignedStr, row);// 老客户签约率row = sheet.createRow(21);Double oldCustomerSignedRate = new Double(report.getOldCustomerSignedRate());String oldCustomerSignedStr = "5、老客户签约率为23.2%,高意向客户签约概率";if (oldCustomerSignedRate < 25) {oldCustomerSignedStr += "较低,与理想值差距大";} else if (oldCustomerSignedRate >= 25 && oldCustomerSignedRate <= 35) {oldCustomerSignedStr += "尚可,还有提高空间";} else if (oldCustomerSignedRate >= 35 && oldCustomerSignedRate <= 50) {oldCustomerSignedStr += "很高";} else if (oldCustomerSignedRate > 50) {oldCustomerSignedStr += "非常高";}createCellAndRow(style4, oldCustomerSignedStr, row);// 认购客户签约率row = sheet.createRow(22);Double contratCuSignedRate = new Double(report.getContratCuSignedRate());String contratCuSignedStr = "6、认购客户签约率为92%,已认购客户签约率";if (contratCuSignedRate < 95) {contratCuSignedStr += "不高,较多退订或拒签";} else if (contratCuSignedRate >= 95 && contratCuSignedRate <= 97) {contratCuSignedStr += "尚可,一定数量退订或拒签";} else if (contratCuSignedRate >= 97 && contratCuSignedRate <= 99) {contratCuSignedStr += "很高";} else if (contratCuSignedRate > 99) {contratCuSignedStr += "非常高";}createCellAndRow(style4, contratCuSignedStr, row);//空行row = sheet.createRow(23);createCellAndRow(style4, "", row);//底侧row = sheet.createRow(24);createCellAndRow(style5, "", row);}else{row = sheet.createRow(13);createCellAndRow(style5, "", row);}try {workbook.write(out);} catch (IOException e) {e.printStackTrace();}}private void createCellAndRow(HSSFCellStyle style, String text, HSSFRow row) {HSSFCell cell = row.createCell(0);cell.setCellStyle(style);HSSFRichTextString rs = new HSSFRichTextString(text);cell.setCellValue(rs);}
/*** 上传Excel* @param fileNewName* @param uploadFile* @return*/public static String uploadExcel(String fileNewName,ByteArrayInputStream uploadFile){// 设置用户属性, 包括appid, secretId和SecretKey// 这些属性可以通过cos控制台获取(https://console.qcloud.com/cos)String version = PropertiesUtil.getValue("version");long appId = "你的appId";String secretId = "你的secretId ";String secretKey = "你的secretKey ";// 设置要操作的bucketString bucketName = "root";// 初始化客户端配置ClientConfig clientConfig = new ClientConfig();// 设置bucket所在的区域,比如广州(gz), 天津(tj)clientConfig.setRegion("sh");// 初始化秘钥信息Credentials cred = new Credentials(appId, secretId, secretKey);// 初始化cosClientCOSClient cosClient = new COSClient(clientConfig, cred);// 文件操作 //// 1. 上传文件(默认不覆盖)// 将本地的local_file_1.txt上传到bucket下的根分区下,并命名为sample_file.txt// 默认不覆盖, 如果cos上已有文件, 则返回错误String cosFilePath = "/report/" + fileNewName;byte[] localFilePath1 = null;try {localFilePath1 = ConvertUtil.toByteArray(uploadFile);} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, cosFilePath, localFilePath1);uploadFileRequest.setEnableShaDigest(false);String uploadFileRet = cosClient.uploadFile(uploadFileRequest);System.out.println("upload file ret:" + uploadFileRet);//获取保存路径ObjectMapper om = new ObjectMapper();HashMap map = new HashMap<>();try {map = om.readValue(uploadFileRet, HashMap.class);} catch (JsonParseException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (JsonMappingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}HashMap<String, String> value = (HashMap<String, String>) map.get("data");return value.get("source_url");}
public boolean addReportExcelToDB(ReportResult rr, String url) {if(StringUtils.isEmpty(url)){return false;}if(rr == null){return false;}ProjectReportRecord prr = new ProjectReportRecord();prr.setCreateTime(DateUtil.format(new Date()));prr.setProjectId(rr.getProjectId());prr.setProjectName(rr.getProjectName());prr.setStartTime(rr.getStartTime());prr.setEndTime(rr.getEndTime());prr.setUrl(url);String report = "";if("案场周报".equals(rr.getReportName())){report = "week";}else if("案场月报".equals(rr.getReportName())){report = "month";}else if("案场季报".equals(rr.getReportName())){report = "quarter";}else if("案场半年报".equals(rr.getReportName())){report = "half";}else if("案场年报".equals(rr.getReportName())){report = "year";}else{report = "other";}prr.setReportName(report);baseDao.save(prr);return true;}

生成的文件示例

周报或者其他报告都是后台自动根据时间进行判断的

周报

季报

以上

Java导出Excel文档(poi),并上传到腾讯云对象存储服务器相关推荐

  1. 使用node上传到腾讯云对象存储cos---转载

    虽然是2017年发布的,但今天依然很好用感谢作者 原作者地址 遇到的小坑: MulterError: Unexpected fieldat wrappedFileFilter (C:\Users\Ad ...

  2. 腾讯云对象存储的完整教程,java将文件上传到腾讯云上后返回可以访问的连接

    先前使用的是七牛云,是挺好用的,但是七牛对于测试域名有一个月的时间限制.测试域名失效后上传的图片等文件就不能访问了.但是腾讯云每个注册的账户有50G的对象存储的空间.而且没有时间限制.所以就采用腾讯的 ...

  3. 【Java用法】使用Java导出word文档的解决方案(适用于从服务器上下载到本地电脑)

    本文目录 一.Controller 二.Service 接口类 三.ServiceImpl 实现类 四.Content-Type 类型与MIME Type类型对照表 最近在做一个word导出功能,需求 ...

  4. Java web--利用java操作excel文档

    在web应用程序的开发中,如果需要将Excel文档中的信息导入数据库或将数据库的信息导出到Excel文档中,需要应用程序访问Excel文件.目前,操作Excel文档的java组件主要有Jxl和POI两 ...

  5. 【Java用法】使用Java导出word文档的解决方案(适用于Windows电脑)

    目录 实现方式一.通过原生的POI 实现方式二.通过Hutool工具包 步骤1.添加pom依赖 步骤2.编写几行代码 步骤3.启动项目,大功告成 实现的效果 最近在做一个word导出功能,需求非常简单 ...

  6. npoi把xlsx文件转为html,C# NPOI 导入与导出Excel文档 兼容xlsx, xls(xf13中已经引用了xlsx的npoi)...

    这里使用的NPOI版本为: 2.1.3.1 版本内包含.Net 2.0 与.Net 4.0 .Net 4.0中包含文件 使用时需引用需要引用所有5个dll 使用到的引用 using NPOI.HSSF ...

  7. JAVA导出Word文档工具EasyWord

    介绍 基于Apache poi封装,在上层做了模型转换的封装,让使用者更加简单方便 只支持docx的导出,不支持doc 下面废话少说 让我们以最快的方式学会用java导出word文档 组件依赖 依赖 ...

  8. nodejs导出Excel文档

    node中导出Excel文档. 安装excel-export node中导出excel的模板有很多,这里我使用的是excel-export来进行的导出功能的实现. 新建一个node项目之后,在目录中找 ...

  9. [sharepoint]rest api文档库文件上传,下载,拷贝,剪切,删除文件,创建文件夹,修改文件夹属性,删除文件夹,获取文档列表...

    写在前面 最近对文档库的知识点进行了整理,也就有了这篇文章,当时查找这些接口,并用在实践中,确实废了一些功夫,也为了让更多的人走更少的弯路. 系列文章 sharepoint环境安装过程中几点需要注意的 ...

最新文章

  1. idea使用ant将wsdl文件_文件曝光:奔驰GLC将使用E级同款1.5T发动机
  2. c语言字符串数组的合并,C语言实现合并字符串
  3. qa dataset
  4. 巧用TensorFlow实现图像处理
  5. Java启动参数与内存调优一些学习笔记
  6. 批改网禁止粘贴怎么破_教育部对家长批改作业表态了,明令禁止!你怎么看?...
  7. 【转】中国式管理最有趣的地方,可能就在这些关键词之中
  8. CCF201809-4 再卖菜
  9. 深度学习主流框架介绍(PyTorch、TensorFlow、Keras、Caffe、Theano、MXNET)
  10. 济南大学转专业计算机面试难吗,我校2016-2017学年学生转专业工作结束
  11. Hexo+GitHub搭建个人博客
  12. phpcms v9如何实现下载列表页直接调用下载地址
  13. (转)罗振宇跨年演讲:哪来直接登顶的人生,只有不断迭代的历程
  14. 智力题、推理判断题、数量关系题(三)
  15. 免费好用的数据可视化软件工具
  16. 深入Windows开发--WM_USER、WM_APP、RegisterWindowMessage
  17. Java软件开发实习生面试题总结
  18. 5年了,高通还是那个高通,华为却早已不是当年的华为!
  19. 如何快速将会议录音转文字
  20. React-Native关于色盘的探索

热门文章

  1. 06 基于v5-resources在macos系统搭建a8服务
  2. 电化学线性极化曲线的Tafel外推法(Tafel拟合)得到年腐蚀速率和极化电阻的原理(科研投稿)
  3. 常见的几种html转pdf方法
  4. 初识Django 笔记
  5. Tensorboard远程连接
  6. 微信小程序开发—— tabbar 配置
  7. 机房里的未卜先知!PAKDD2021 第二届阿里云智能运维算法大赛启动
  8. 记Vivado使用,报错记录本
  9. python实现翻转金字塔图案
  10. Web前端开发主要做什么