POI:对Excel的基本读操作 整理2

1 简单读取操作

java 复制代码
public class ExcelRead {
    String PATH = "D:\\Idea-projects\\POI\\POI_projects";

    // 读取的一系列方法
    // ......
}

因为07版本和03版本操作流程大差不差,所以这边就以03版本为例

java 复制代码
    @Test
    public void testRead03() throws IOException {

        //获取文件流
        FileInputStream fileInputStream = new FileInputStream(PATH + "xiexu03.xls");

        //1.创建一个工作簿,获取到文件中的内容
        Workbook workbook = new HSSFWorkbook(fileInputStream);

        // 2.得到表,获取具体位置的内容
        Sheet sheet = workbook.getSheetAt(0);

        // 3.得到行
        Row row = sheet.getRow(0);

        // 4.得到列
        Cell cell = row.getCell(1);

        // System.out.println(cell.getStringCellValue());  // 得到单元格中的字符串内容

        System.out.println(cell.getNumericCellValue());   // 得到单元格中的数字内容

        fileInputStream.close();
    }



2 分类型读取数据

读取下列含有不同数据类型的Excel表格,并输出读取的数据


java 复制代码
    @Test
    public void testCellType() throws IOException {
        //获取文件流
        FileInputStream fileInputStream = new FileInputStream("D:\\Idea-projects\\POI\\明细表.xls");

        //1.创建一个工作簿
        Workbook workbook = new HSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheetAt(0);

        // 2. 获取标题内容
        Row rowTitle = sheet.getRow(0);
        if (rowTitle != null) {
            // 这种写法一定要掌握
            int cellCount = rowTitle.getPhysicalNumberOfCells();   // 读取所有有数据的单元格数量

            for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                Cell cell = rowTitle.getCell(cellNum);
                if (cell != null) {
                    CellType cellType = cell.getCellType();
                    String cellValue = cell.getStringCellValue();
                    System.out.print(cellValue + " | ");
                }
            }
            System.out.println();
        }

        // 获取表中的内容
        int rowCount = sheet.getPhysicalNumberOfRows();   // 获取所有含有数据的行的数量
        for (int rowNum = 1; rowNum < rowCount; rowNum++) {
            Row rowData = sheet.getRow(rowNum);
            if (rowData != null) {
                // 读取行中的列
                int cellCount = rowTitle.getPhysicalNumberOfCells();
                for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                    System.out.print("[" + (rowNum + 1) + "-" + (cellNum + 1) + "]");
                    Cell cell = rowData.getCell(cellNum);

                    // 匹配列的数据类型

                    if ( cell != null) {
                        CellType cellType = cell.getCellType();
                        String cellValue = "";

                        switch (cell.getCellType()) {
                            case STRING:
                                System.out.println("字符串");
                                cellValue = cell.getStringCellValue();
                                break;
                            case BLANK:
                                System.out.println("空");
                                break;
                            case BOOLEAN:
                                System.out.println("布尔");
                                cellValue = String.valueOf(cell.getBooleanCellValue());
                                break;
                            case NUMERIC:
                                System.out.println("数字");   // 分为日期和普通数字
                                if (DateUtil.isCellDateFormatted(cell)) {
                                    System.out.println("日期");
                                    Date date = cell.getDateCellValue();
                                    DateTime time = new DateTime(date);
                                    cellValue = time.toString("yyyy-MM-dd");
                                } else{
                                    //   普通数字  -> 转换为字符串输出
                                    System.out.println("转换为字符串输出");
                                    cell.setCellType(CellType.STRING);  // 需要设置类型为字符串
                                    cellValue = cell.toString();
                                }
                                break;
                            case ERROR:
                                System.out.println("数据类型错误");
                                break;
                        }
                        System.out.println(cellValue);
                    }
                }
            }
        }

        fileInputStream.close();
    }




3 读取公式


java 复制代码
 @Test
    public void  testReadCalculate() throws IOException {   // 公式

        //获取文件流
        FileInputStream fileInputStream = new FileInputStream("D:\\Idea-projects\\POI\\公式.xls");

        //1.创建一个工作簿
        Workbook workbook = new HSSFWorkbook(fileInputStream);

        // 2.得到表
        Sheet sheet = workbook.getSheetAt(0);
    
        // 3.得到行
        Row row = sheet.getRow(4);

        // 4.得到列
        Cell cell = row.getCell(0);

        // 拿到计算公式
        FormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);

        // 输出单元格的内容
        CellType cellType = cell.getCellType();

        switch (cell.getCellType()) {
            case FORMULA:
                System.out.println("公式");
                String formula = cell.getCellFormula();   // 获取计算公式
                System.out.println(formula);

                // 计算
                CellValue evaluate = formulaEvaluator.evaluate(cell);
                String cellValue = evaluate.formatAsString();   // 将计算结果格式化为字符串

                // 输出
                System.out.println(cellValue);
                break;
        }

        fileInputStream.close();
    }


相关推荐
华科易迅2 小时前
Spring 事务(注解)
java·数据库·spring
写代码的小阿帆2 小时前
Web工程结构解析:从MVC分层到DDD领域驱动
java·架构·mvc
东离与糖宝2 小时前
Java 26+Spring Boot 3.5,微服务启动从3秒压到0.8秒
java·人工智能
禹中一只鱼3 小时前
【力扣热题100学习笔记】 - 哈希
java·学习·leetcode·哈希算法
凌波粒3 小时前
LeetCode--349.两个数组的交集(哈希表)
java·算法·leetcode·散列表
于先生吖3 小时前
Java+SpringBoot 无人健身房物联网系统完整源码实现
java·spring boot·物联网
johnrui3 小时前
SpringBoot-JdbcTemplate
java·spring boot·后端
码云社区3 小时前
JAVA二手车交易二手车市场系统源码支持微信小程序+微信公众号+H5+APP
java·开发语言·微信小程序·二手交易·闲置回收
crescent_悦3 小时前
C++:The Largest Generation
java·开发语言·c++
indexsunny4 小时前
互联网大厂Java面试实战:从Spring Boot到微服务的技术问答解析
java·spring boot·redis·微服务·消息队列·电商