当前位置: 代码网 > 服务器>服务器>Linux > Apache POI案例代码详解

Apache POI案例代码详解

2024年05月26日 Linux 我要评论
简介:apache poi 是一个处理miscrosoft office各种文件格式的开源项目。简单来说就是,我们可以使用poi在java程序中对miscrosoft office各种文件进行读写操作

简介:apache poi 是一个处理miscrosoft office各种文件格式的开源项目。简单来说就是,我们可以使用poi在java程序中对miscrosoft office各种文件进行读写操作。

1、应用场景

图 1-1 apache poi应用场景

apache poi应用场景:

  • 银行网银系统导出交易明细
  • 各种业务系统导出excel报表
  • 批量导入业务数据

2、案例代码

2.1 创建 excel 文件

package com.sky.test;
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 main(string[] args) throws ioexception {
        write();
    }
    /**
     * 通过poi创建excel文件并且写入文件内容
     */
    public static void write() throws ioexception {
        //创建一个excel文件
        xssfworkbook excel = new xssfworkbook();
        //创建excel中的单元测
        xssfsheet sheet = excel.createsheet("info");
        //在sheet中创建行对象,rownum编号从零开始
        xssfrow row = sheet.createrow(1);
        //创建单元格并且写入文件内容
        row.createcell(1).setcellvalue("姓名");
        row.createcell(2).setcellvalue("城市");
        //创建一个新行
        xssfrow row1 = sheet.createrow(2);
        row1.createcell(1).setcellvalue("王宁");
        row1.createcell(2).setcellvalue("亳州");
        //创建一个新行
        xssfrow row2 = sheet.createrow(2);
        row2.createcell(1).setcellvalue("王宁");
        row2.createcell(2).setcellvalue("亳州");
        //将内存中的excel表格存储到指定的盘符下面
        fileoutputstream fileoutputstream = new fileoutputstream(new file("d:\\info.xlsx"));
        excel.write(fileoutputstream);
        //关闭资源
        excel.close();
        fileoutputstream.close();
    }
}

2.2 读取 excel 文件

package com.sky.test;
import org.apache.poi.xssf.usermodel.xssfrow;
import org.apache.poi.xssf.usermodel.xssfsheet;
import org.apache.poi.xssf.usermodel.xssfworkbook;
import java.io.*;
public class poitest {
    public static void main(string[] args) throws ioexception {
        read();
    }
    /**
     * 通过poi读取excel文件
     */
    public static void read() throws ioexception{
        fileinputstream fileinputstream = new fileinputstream(new file("d:\\info.xlsx"));
        //创建一个excel对象,用于读取excle数据
        xssfworkbook excel = new xssfworkbook(fileinputstream);
        //读取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);
        }
        //关闭资源
        excel.close();
        fileinputstream.close();
    }
}

到此这篇关于apache poi案例详解的文章就介绍到这了,更多相关apache poi内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com