Apache POI报表统计

devtools/2024/9/24 3:34:22/

Apache POl是一个处理Miscrosoft Office各种文件格式的开源项目。简单来说就是,我们可以使用 POlJava 程N序中对Miscrosoft Office各种文件进行读写操作。一般情况下,POI都是用于操作 Excel 文件。

导入Maven坐标:

	<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.1.0</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.1.0</version></dependency>

入门案例之写操作

package PoiExcel;import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;public class POITest {public static void write() throws IOException {//在内存中创建一个Excel文件XSSFWorkbook excel = new XSSFWorkbook();//创建sheetXSSFSheet sheet = excel.createSheet("info");//是从0开始计数的XSSFRow row = sheet.createRow(1);row.createCell(1).setCellValue("姓名");row.createCell(2).setCellValue("年龄");row = sheet.createRow(2);row.createCell(1).setCellValue("鹏翔");row.createCell(2).setCellValue("18");//利用文件输出流将内存中的Excel写入磁盘FileOutputStream fileOutputStream = new FileOutputStream(new File("D:/results.xlsx"));excel.write(fileOutputStream);//关闭资源fileOutputStream.close();excel.close();}public static void main(String[] args) throws IOException {write();}
}

在这里插入图片描述

入门案例之Excel读取:

public static void read() throws IOException {FileInputStream fileInputStream = new FileInputStream(new File("D:/results.xlsx"));//通过输入流传入读取的excel文件XSSFWorkbook excel= new XSSFWorkbook(fileInputStream);XSSFSheet sheet = excel.getSheet("info");int lastRowNum = sheet.getLastRowNum();for (int  i= 1;  i<= lastRowNum; i++) {XSSFRow row = sheet.getRow(i);String cellValue1 = row.getCell(2).getStringCellValue();String cellValue2 = row.getCell(1).getStringCellValue();System.out.println(cellValue2 + cellValue1);}fileInputStream.close();excel.close();}

在这里插入图片描述

了解了入门案例后,我们看看如何实现导出报表,首先我们的报表的格式要固定且统一

在这里插入图片描述

我们将这个设计好的模板文件读取出来,然后将数据库中查到的信息查到固定的位置,最后下载下来即可

public void exportBusinessData(HttpServletResponse response) {LocalDate begin = LocalDate.now().minusDays(30);LocalDate end = LocalDate.now().minusDays(1);//查询概览运营数据,提供给Excel模板文件BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(begin,LocalTime.MIN), LocalDateTime.of(end, LocalTime.MAX));InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");try {//基于提供好的模板文件创建一个新的Excel表格对象XSSFWorkbook excel = new XSSFWorkbook(inputStream);//获得Excel文件中的一个Sheet页XSSFSheet sheet = excel.getSheet("Sheet1");sheet.getRow(1).getCell(1).setCellValue(begin + "至" + end);//获得第4行XSSFRow row = sheet.getRow(3);//获取单元格row.getCell(2).setCellValue(businessData.getTurnover());row.getCell(4).setCellValue(businessData.getOrderCompletionRate());row.getCell(6).setCellValue(businessData.getNewUsers());row = sheet.getRow(4);row.getCell(2).setCellValue(businessData.getValidOrderCount());row.getCell(4).setCellValue(businessData.getUnitPrice());for (int i = 0; i < 30; i++) {LocalDate date = begin.plusDays(i);//准备明细数据businessData = workspaceService.getBusinessData(LocalDateTime.of(date,LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));row = sheet.getRow(7 + i);row.getCell(1).setCellValue(date.toString());row.getCell(2).setCellValue(businessData.getTurnover());row.getCell(3).setCellValue(businessData.getValidOrderCount());row.getCell(4).setCellValue(businessData.getOrderCompletionRate());row.getCell(5).setCellValue(businessData.getUnitPrice());row.getCell(6).setCellValue(businessData.getNewUsers());}//通过输出流将文件下载到客户端浏览器中ServletOutputStream out = response.getOutputStream();excel.write(out);//关闭资源out.flush();out.close();excel.close();}catch (IOException e){e.printStackTrace();}}

在这里插入图片描述

在这里插入图片描述


http://www.ppmy.cn/devtools/8748.html

相关文章

租用马来西亚服务器:稳定高效的网络选择

马来西亚首都是吉隆坡。作为一个新兴的多元化经济国家&#xff0c;也属于亚洲四小龙之一。地理位置优越&#xff0c;中间隔着南中国海。一部分是北接泰国的位于马来半岛的西马来西亚&#xff0c;另一部分则是东马来西亚&#xff0c;在婆罗洲岛的北部。这种地理位置有利于促进该…

SQL注入学习

Mysql&#xff1a; 联合查询注入&#xff1a; 判断列数&#xff1a;order by x&#xff08;变量&#xff0c;输入数字判断具提列数&#xff09; 判断回显注入点&#xff1a; union select 1,2,3,…,x(测试哪一行会显示在页面上&#xff0c;查看数据变化) 爆库&#xff1a;uni…

CDN的原理

CDN的原理 CDN的基本原理是依靠部署在各个区域大量缓存服务器的响应。当用户访问网站时&#xff0c;不需要访问站点的DNS服务器&#xff0c;而是利用全局负载技术将用户的请求直接指向最近的缓存服务器上&#xff0c;且保证服务器是正常工作的&#xff0c;访问的路径和内容是传…

【解决去除springboot-内嵌tomcat的异常信息显示】去掉版本号和异常信息

调用这个&#xff0c;能复现tomcat的报错 http://localhost:8182/defaultroot/DownloadServlet?modeType2&pathhtml&FileName…\login.jsp&name123&fiewviewdownload2&cdinline&downloadAll2 springboot项目如何隐藏&#xff1f; springboot内嵌了to…

虚良SEO怎么有效的对百度蜘蛛权重优化?

人们交换链接通常首先要问的是你BR值是多少&#xff1f;国内搜索引擎来说以百度马首是瞻&#xff0c;无论seo还是竞价都看重的是百度&#xff0c;那么针对百度权重的优化就特别重要了。其实&#xff0c;百度权重是民间的一种说法&#xff0c;百度官方并没有认同这个数值&#x…

console.log进阶

参考链接&#xff1a;【伸手党福利】console.log()修改字体大小、颜色、背景【基础进阶总结】_console.log 颜色-CSDN博客 文字大小颜色设置 console.log(%c电话&#xff1a;\r\n邮箱&#xff1a;qq.com\r\n, "color: red; font-size: 16px;");

安卓平台下OkHttp3网络库的全面探讨与实践

摘要 本文深入剖析了安卓应用开发中广泛采用的网络通信库——OkHttp3。作为一款高性能、易于使用的HTTP客户端工具包&#xff0c;OkHttp3凭借其卓越的设计理念与强大的功能特性&#xff0c;在移动应用开发领域内占据了主导地位。本文旨在系统性地介绍OkHttp3的核心概念&#xf…

Asp.Net Core中的进程内托管与进程外托管模型

在ASP.NET Core中&#xff0c;托管模型决定了应用程序如何运行及其与Web服务器交互的方式。主要有两种托管模式&#xff1a;进程内托管&#xff08;In-Process Hosting&#xff09;和进程外托管&#xff08;Out-of-Process Hosting&#xff09;。每种模式都有自己的优势和使用场…