当前位置: 移动技术网 > IT编程>开发语言>Java > 【学习笔记】使用Java读取、写入Excel全版本(包含xls、xslx格式)通用方法及代码展示(POI)

【学习笔记】使用Java读取、写入Excel全版本(包含xls、xslx格式)通用方法及代码展示(POI)

2020年07月03日  | 移动技术网IT编程  | 我要评论

        POI是Java编写的开源跨平台Excel处理工具,不仅提供了对Excel的操作,也提供了对Word、PowerPoint和Visio等格式的文档的操作。

jar包下载

        基于Maven工程的pom.xml文件配置POI如下所示:

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.17</version>
</dependency>

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>3.17</version>
</dependency>

相关类的介绍

Workbook:Workbook接口,用于创建工作簿。
Sheet:Sheet接口,用于创建工作表。
Row:Row接口,用于操作行。
Cell:Cell接口,用于操作列。
        针对xls格式的Excel数据,需要使用HSSF开头的类进行操作;针对xlsx格式(Excel 2007以上版本)的Excel数据,需要使用XSSF开头的类进行操作。

相关方法的介绍

createRow(int column)方法:创建某行。
createCell(int column)方法:创建某列。
setCellValue(String value)方法:为该单元格赋值。
getSheet(String name)方法:读取工作表。
getSheetAt(int index)方法:读取工作表。
getLastRowNum()方法:获取表格的所有行数。
getLastCellNum()方法:获取某行的所有列数。

Java写入Excel全版本通用代码

/**
 * 使用POI写入Excel(包括xls和xlsx)
 *
 * @author hakutaku
 * @create 2020-07-01-21:07
 **/
public class PoiExcelWritelProcess {
    public static void main(String[] args) throws IOException {
        // 文件名称
        File file = new File("test/helloworld.xlsx");
//         File file = new File("test/read.xlsx");
        OutputStream outputStream = new FileOutputStream(file);
        Workbook workbook = getWorkBook(file);
        Sheet sheet = workbook.createSheet("Sheet1");
        // 添加表头
        Row row = sheet.createRow(0);   // 创建某行
        row.createCell(0).setCellValue("post_id");
        row.createCell(1).setCellValue("post_title");
        // 添加内容
        for (int i = 0; i < 2; i++) {
            Row everyRow = sheet.createRow(i + 1);
            everyRow.createCell(0).setCellValue("帖子id为:0" +  i);
            everyRow.createCell(1).setCellValue("帖子内容为:" + i);
        }
        workbook.write(outputStream);
        // 释放资源
        workbook.close();
        outputStream.close();
    }
    /*
     * 判断Excel的版本,初始化不同的Workbook
     * */
    public static Workbook getWorkBook(File file) {
        Workbook workbook = null;
        // Excel 2003版本
        if(file.getName().endsWith("xls")){
            workbook = new HSSFWorkbook();
        } // Excel 2007以上版本
        else if (file.getName().endsWith("xlsx")){
            workbook = new XSSFWorkbook();
        }
        return workbook;
    }
}

Java读取Excel全版本通用代码

/**
 * 使用POI读取Excel文件(包括xls和xlsx)
 *
 * @author hakutaku
 * @create 2020-07-01-19:39
 **/
public class PoiExcelRead {
    public static void main(String[] args) throws IOException {
        // 文件名称
        File file = new File("test/helloworld.xlsx");
//         File file = new File("test/read.xlsx");
        // 根据文件名称获取操作工作簿
        Workbook workbook = getWorkBook(file);
        // 获取读取的工作表,这里有两种方式
        Sheet sheet = workbook.getSheet("Sheet1");
//        Sheet sheet = workbook.getSheetAt(0);
        int allRow = sheet.getLastRowNum();   // 获取行数
        // 按行读取数据
        for (int i = 0; i <= allRow; i++) {
            Row row = sheet.getRow(i);
            // 获取列数
            short lastCellNum = row.getLastCellNum();
            for (int j = 0; j < lastCellNum; j++) {
                String cellValue = row.getCell(j).getStringCellValue();
                System.out.print(cellValue + "\t");
            }
            System.out.println();
        }
        workbook.close();
    }
    /*
    * 判断Excel的版本,初始化不同的Workbook
    * */
    public static Workbook getWorkBook(File file) throws IOException {
        // 输入流
        InputStream in = new FileInputStream(file);
        Workbook  workbook = null;
        // Excel 2003版本
        if(file.getName().endsWith("xls")){
            workbook = new HSSFWorkbook(in);
        } // Excel 2007以上版本
        else if (file.getName().endsWith("xlsx")){
            workbook = new XSSFWorkbook(in);
        }
        in.close();
        return workbook;
    }
}

运行结果

在这里插入图片描述

本文地址:https://blog.csdn.net/qq_42506411/article/details/107072618

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网