使用java技术进行excel表的读取
第一步: 导入jar包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
第二步: 创建类 按表格的行列读取
try {
File excel = new File("H:\\新建文件夹\\DCT 窗口时间表格12.12.xlsx");
if (excel.isFile() && excel.exists()) { //判断文件是否存在
String[] split = excel.getName().split("\\."); //.是特殊字符,需要转义!!!!!
Workbook wb;
//根据文件后缀(xls/xlsx)进行判断
if ( "xls".equals(split[2])){
FileInputStream fis = new FileInputStream(excel); //文件流对象
wb = new HSSFWorkbook(fis);
}else if ("xlsx".equals(split[2])){
wb = new XSSFWorkbook(new FileInputStream(excel));
}else {
throw new ReadExcelException("文件类型错误");
}
//开始解析
Sheet sheet = wb.getSheetAt(0);
int firstRowIndex = sheet.getFirstRowNum(); //第一行
int lastRowIndex = sheet.getLastRowNum();//最后一行
for(int rIndex = firstRowIndex; rIndex < lastRowIndex; 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) {
String text=cell.toString().split("\\.")[0];
}
}
}
}
} else {
throw new ReadExcelException("找不到文件");
}
} catch (Exception e) {
e.printStackTrace();
}