Apache POl

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

Apache POl 的应用场景:

  1. 银行网银系统导出交易明细
  2. 各种业务系统导出Excel报表
  3. 批量导入业务数据

使用POI需要导入下面2个坐标:

复制代码
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
</dependency>

往表格存数据:

复制代码
public class POITest {
    /*
    * 通过POI创建Excel文件并且写入文件内容
    * */
    @Test
    public void writeTest() throws IOException {
        //在内存中创建一个Excel文件
        XSSFWorkbook excel = new XSSFWorkbook();
        //在Excel文件中创建一个Sheet页
        XSSFSheet sheet = excel.createSheet("info");
        //在Sheet中创建行对象,rownum编号从0开始
        XSSFRow row = sheet.createRow(1); //1代表第2行
        row.createCell(1).setCellValue("姓名");//创建单元格写入内容
        row.createCell(2).setCellValue("城市");
        //创建一个新行
        row = sheet.createRow(2);//第3行
        row.createCell(1).setCellValue("张三");//创建单元格写入内容
        row.createCell(2).setCellValue("厦门");
 
        row = sheet.createRow(3);//第4行
        row.createCell(1).setCellValue("李四");//创建单元格写入内容
        row.createCell(2).setCellValue("南京");
        //上面写的都是在内存,现在想在磁盘看到
        FileOutputStream out = new FileOutputStream(new File("C://software/info.xlsx"));//设置文件
        excel.write(out);//写入到文件
        //关闭资源
        out.close();
        excel.close();
 
    }
}

读取excel中的数据:

复制代码
@Test
public void readTest() throws IOException{
    FileInputStream in = new FileInputStream(new File("C://software/info.xlsx"));
    //读取磁盘上已经存在的Excel文件
    XSSFWorkbook excel = new XSSFWorkbook(in);
    //读取Excel文件中的第一个Sheet页
    XSSFSheet sheet = excel.getSheetAt(0);
    //获取Sheet中最后一行行号
    int lastRowNum = sheet.getLastRowNum();
    for(int i=1;i<=lastRowNum;i++){
        //获得某一行
        XSSFRow row = sheet.getRow(i);
        //获得单元格对象
        String cellValue1 = row.getCell(1).getStringCellValue();
        String cellValue2 = row.getCell(2).getStringCellValue();
        System.out.println(cellValue1+" "+cellValue2);
    }
    //关闭资源
    in.close();
 
}

代码解析:

复制代码
public void export(HttpServletResponse response){
        reportService.exportBusinessData(response);
    }

其中**HttpServletResponse** 是 Java Servlet API 中的一个接口,用于在服务器端向客户端发送 HTTP 响应。它提供了一系列方法,用于设置和控制响应的内容、状态码、头部信息等。

具体来说,HttpServletResponse 接口允许开发者在 Servlet 中生成 HTTP 响应,并将其发送到客户端。通过该接口,开发者可以:

  1. 设置响应的状态码和状态消息(例如:200 OK、404 Not Found)。
  2. 添加响应头部信息(例如:Content-Type、Cache-Control)。
  3. 获取输出流,以便向客户端发送数据。
  4. 设置响应的字符编码和内容类型。
  5. 重定向请求到其他 URL。
  6. 向响应中添加 Cookie。在上面的代码中起到的是第三个功能。

使用 HttpServletResponse 接口,开发者可以灵活地控制和定制服务器端向客户端发送的 HTTP 响应,以实现各种需求,如返回网页、文件下载、JSON 数据等。

相关推荐
SelectDB技术团队25 分钟前
永久免费!专为 Apache Doris 打造的可视化数据管理工具 SelectDB Studio V1.1.0 重磅发布!
数据仓库·apache·doris·数据可视化·日志数据
拾贰_C4 小时前
【SpringBoot】MyBatisPlus(MP | 分页查询操作
java·spring boot·后端·spring·maven·apache·intellij-idea
hnlucky15 小时前
Windows 上安装下载并配置 Apache Maven
java·hadoop·windows·学习·maven·apache
sadoshi3 天前
phpstudy的Apache添加AddType application/x-httpd-php .php .php5配置无效的处理方式
开发语言·php·apache
众乐乐_20083 天前
Uniapp中小程序调用腾讯地图(获取定位地址)
linux·服务器·apache
Johny_Zhao3 天前
CentOS Stream 10安装部署Zabbix 7.0网络监控平台和设备配置添加
linux·网络·网络安全·docker·信息安全·云计算·apache·zabbix·devops·yum源·huawei·系统运维·itsm·华三
小沈同学呀3 天前
Apache JMeter API 接口压测技术指南
jmeter·apache
何xiao树4 天前
Apache Ranger 2.2.0 编译
apache·rancher
wusam4 天前
Linux系统管理与编程20:Apache
linux·运维·服务器·apache·shell编程
Johny_Zhao5 天前
Ubuntu安装部署Zabbix网络监控平台和设备配置添加
linux·网络·mysql·网络安全·信息安全·云计算·apache·zabbix·shell·yum源·系统运维·itsm