duhuizhe
2024-04-17 c389984b5d3e5523e1b22de1fc045dc415261330
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.yqzx.common.util;
 
import java.io.*;
 
import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
 
 
/**
 
 * 读取Excel
 
 *
 
 * @author zengwendong
 
 */
@Slf4j
public class ReadExcelUtils {
 
    private Logger logger = LoggerFactory.getLogger(ReadExcelUtils.class);
 
    private Workbook wb;
 
    private Sheet sheet;
 
    private Row row;
 
 
    // int firstRowIndex = sheet.getFirstRowNum()+1;   //第一行是列名,所以不读
    // int lastRowIndex = sheet.getLastRowNum();
 
    /**
     * @Author wh
     * @Date 2022/6/7 - 9:46
     * @Description //
     * filepath:路径
     * firstRowIndex:开始行数从
     **/
    public static void ReadExcelUtils(String filepath, int firstRowIndex) {
 
        try {
            //String encoding = "GBK";
//            File excel = new File(filepath);
            File excel = FileUtil.touch(filepath);
            if (excel.isFile() && excel.exists()) {   //判断文件是否存在
 
                String[] split = excel.getName().split("\\.");  //.是特殊字符,需要转义!!!!!
                Workbook wb;
                //根据文件后缀(xls/xlsx)进行判断
                if ( "xls".equals(split[1])){
                    FileInputStream fis = new FileInputStream(excel);   //文件流对象
                    wb = new HSSFWorkbook(fis);
                }else if ("xlsx".equals(split[1])){
                    wb = new XSSFWorkbook(excel);
                }else {
                    System.out.println("文件类型错误!");
                    return;
                }
 
                //开始解析
                Sheet sheet = wb.getSheetAt(0);     //读取sheet 0
 
                // int firstRowIndex = sheet.getFirstRowNum()+1;   //第一行是列名,所以不读
                int lastRowIndex = sheet.getLastRowNum();
 
                for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {   //遍历行
                    System.out.println("rIndex: " + rIndex);
                    Row row = sheet.getRow(rIndex);
                    if (row != null) {
                        int firstCellIndex = row.getFirstCellNum();
                        int lastCellIndex = row.getLastCellNum();
                        for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {   //遍历列
                            Cell cell = row.getCell(cIndex);
                            if (cell != null) {
                                System.out.println(cell.toString());
                            }
                        }
                    }
                }
            } else {
                log.info("找不到指定的文件");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
 
    }
}