poi解析读取Excel文档

Apache POI常用的类:
HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC97格式档案的功能。
XWPF - 提供读写Microsoft Word DOC2003格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。

1.创建Excel表格

//创建文件
    public static void createExcel() throws IOException {
        //从桌面获取真实路径
        FileSystemView fileSystemView = FileSystemView.getFileSystemView();
        String path = fileSystemView.getHomeDirectory().getPath();
        //设置文件路径
        String filePath = path + "/测试.xls";//这里后缀是.xls 类一定用HSSF(参考常用类对应)

        //根据路径创建一个新文件
        File file = new File(filePath);
        FileOutputStream outputStream = new FileOutputStream(file);
        HSSFWorkbook sheets = new HSSFWorkbook();
        //创建一个新表
        HSSFSheet sheet1 = sheets.createSheet("客户管理");
        //创建第一行
        HSSFRow row1 = sheet1.createRow(0);
        //填字段
        row1.createCell(0).setCellValue("id");
        row1.createCell(1).setCellValue("name");
        row1.createCell(2).setCellValue("sex");
        row1.createCell(3).setCellValue("phone");
        row1.createCell(4).setCellValue("QQ");
        row1.createCell(5).setCellValue("Email");
        row1.createCell(6).setCellValue("parentId");
        row1.createCell(7).setCellValue("count");

        //创建第二行
        HSSFRow row2 = sheet1.createRow(1);
        //填写数据
        row2.createCell(0).setCellValue("1");
        row2.createCell(1).setCellValue("尉某人");
        row2.createCell(2).setCellValue("男");
        row2.createCell(3).setCellValue("189****7950");
        row2.createCell(4).setCellValue("11468***98");
        row2.createCell(5).setCellValue("YLL130****8233@163.com");
        row2.createCell(6).setCellValue(0);
        row2.createCell(7).setCellValue(135);


        sheets.setActiveSheet(0);
        sheets.write(outputStream);
        outputStream.close();
        System.out.println("=================");
        System.out.println("创建成功");
        System.out.println("=================");
    }

2.解析Excel内容

//解析文件
    public static void lookExcel() throws IOException {
        //获取文件路径
        FileSystemView fileSystemView = FileSystemView.getFileSystemView();
        String path = fileSystemView.getHomeDirectory().getPath();
        String filePath = path + "/测试.xls";//注意后缀

        //将文件转化成流
        FileInputStream fileInputStream = new FileInputStream(filePath);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        POIFSFileSystem poifsFileSystem = new POIFSFileSystem(bufferedInputStream);
        HSSFWorkbook sheets = new HSSFWorkbook(poifsFileSystem);

		//获取哪个sheet内容
        HSSFSheet sheel1 = sheets.getSheet("客户管理");

		//获取行数
        int rowNum = sheel1.getLastRowNum();
        System.out.println("====================");
        System.out.println(rowNum);


        for (int i = 0; i <= rowNum; i++) {
            HSSFRow row = sheel1.getRow(i);
            if (row == null){
                System.out.println("没有了");
                break;
            }
			
			//获取列数
            short cellNum = row.getLastCellNum();
            System.out.println(cellNum);
            System.out.println("====================");
            for (int j = 0; j < cellNum ; j++) {
            	//这里根据需要修改解析结果类型(我全部解析成string类型了)
				row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
                String cellValue = row.getCell(j).getStringCellValue();
                System.out.println("***********************");
                System.out.println(cellValue);
                System.out.println("***********************");
            }
        }
        bufferedInputStream.close();
    }

3.测试创建Excel表格和解析Excel数据
需要哪个方法放开就可以(“Ctrl + /”)

 @Test
    public static void main(String[] args) {
//        //创建表
//        try {
//            poiTest.createExcel();
//            System.out.println("真的创建成功♪(^∀^●)ノ");
//        } catch (IOException e) {
//            e.getMessage();
//            System.out.println("创建失败(づ╥﹏╥)づ");
//        }

        //读取表数据
        try {
            poiTest.lookExcel();
            System.out.println("解析成功了♪(^∀^●)ノ");
        } catch (IOException e) {
            e.getMessage();
            System.out.println("解析失败(づ╥﹏╥)づ");
        }



    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
注:下文中的 *** 代表文件名中的版本号。 # 【poi-***.jar中文文档.zip】 中包含: 中文文档:【poi-***-javadoc-API文档-中文(简体)版.zip】 jar包下载地址:【poi-***.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【poi-***.jar Maven依赖信息(可用于项目pom.xml).txt】 Gradle依赖:【poi-***.jar Gradle依赖信息(可用于项目build.gradle).txt】 源代码下载地址:【poi-***-sources.jar下载地址(官方地址+国内镜像地址).txt】 # 本文件关键字: poi-***.jar中文文档.zip,java,poi-***.jar,org.apache.poi,poi,***,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,apache,中文API文档,手册,开发手册,使用手册,参考手册 # 使用方法: 解压 【poi-***.jar中文文档.zip】,再解压其中的 【poi-***-javadoc-API文档-中文(简体)版.zip】,双击 【index.html】 文件,即可用浏览器打开、进行查看。 # 特殊说明: ·本文档为人性化翻译,精心制作,请放心使用。 ·只翻译了该翻译的内容,如:注释、说明、描述、用法讲解 等; ·不该翻译的内容保持原样,如:类名、方法名、包名、类型、关键字、代码 等。 # 温馨提示: (1)为了防止解压后路径太长导致浏览器无法打开,推荐在解压时选择“解压到当前文件夹”(放心,自带文件夹,文件不会散落一地); (2)有时,一套Java组件会有多个jar,所以在下载前,请仔细阅读本篇描述,以确保这就是你需要的文件; # Maven依赖: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>***</version> </dependency> ``` # Gradle依赖: ``` Gradle: implementation group: 'org.apache.poi', name: 'poi', version: '***' Gradle (Short): implementation 'org.apache.poi:poi:***' Gradle (Kotlin): implementation("org.apache.poi:poi:***") ``` # 含有的 Java package(包)(此处仅列举3个): ``` org.apache.poi org.apache.poi.common org.apache.poi.common.usermodel ...... ``` # 含有的 Java class(类)(此处仅列举3个): ``` org.apache.poi.EmptyFileException org.apache.poi.EncryptedDocumentException org.apache.poi.OldFileFormatException ...... ```
注:下文中的 *** 代表文件名中的版本号。 # 【poi-***.jar中文文档.zip】 中包含: 中文文档:【poi-***-javadoc-API文档-中文(简体)版.zip】 jar包下载地址:【poi-***.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【poi-***.jar Maven依赖信息(可用于项目pom.xml).txt】 Gradle依赖:【poi-***.jar Gradle依赖信息(可用于项目build.gradle).txt】 源代码下载地址:【poi-***-sources.jar下载地址(官方地址+国内镜像地址).txt】 # 本文件关键字: poi-***.jar中文文档.zip,java,poi-***.jar,org.apache.poi,poi,***,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,apache,中文API文档,手册,开发手册,使用手册,参考手册 # 使用方法: 解压 【poi-***.jar中文文档.zip】,再解压其中的 【poi-***-javadoc-API文档-中文(简体)版.zip】,双击 【index.html】 文件,即可用浏览器打开、进行查看。 # 特殊说明: ·本文档为人性化翻译,精心制作,请放心使用。 ·只翻译了该翻译的内容,如:注释、说明、描述、用法讲解 等; ·不该翻译的内容保持原样,如:类名、方法名、包名、类型、关键字、代码 等。 # 温馨提示: (1)为了防止解压后路径太长导致浏览器无法打开,推荐在解压时选择“解压到当前文件夹”(放心,自带文件夹,文件不会散落一地); (2)有时,一套Java组件会有多个jar,所以在下载前,请仔细阅读本篇描述,以确保这就是你需要的文件; # Maven依赖: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>***</version> </dependency> ``` # Gradle依赖: ``` Gradle: implementation group: 'org.apache.poi', name: 'poi', version: '***' Gradle (Short): implementation 'org.apache.poi:poi:***' Gradle (Kotlin): implementation("org.apache.poi:poi:***") ``` # 含有的 Java package(包)(此处仅列举3个): ``` org.apache.poi org.apache.poi.common org.apache.poi.common.usermodel ...... ``` # 含有的 Java class(类)(此处仅列举3个): ``` org.apache.poi.EmptyFileException org.apache.poi.EncryptedDocumentException org.apache.poi.OldFileFormatException ...... ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尉某人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值