package com. myFirstSpring. test;
import org. apache. poi. ss. usermodel. *;
import com. alibaba. fastjson. JSON;
import com. alibaba. fastjson. JSONArray;
import com. sun. org. apache. bcel. internal. generic. RET;
import java. io. File;
import java. io. FileInputStream;
import java. text. SimpleDateFormat;
import java. util. ArrayList;
import java. util. Date;
import java. util. List;
public class ReadExcel {
public static void main ( String[ ] args) {
readExcel ( "D:\\测试生成Excel文件\\withoutHead2.xls" ) ;
}
public static void readExcel ( String path) {
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
File file = new File ( path) ;
FileInputStream fis = null;
Workbook workBook = null;
if ( file. exists ( ) ) {
try {
fis = new FileInputStream ( file) ;
workBook = WorkbookFactory. create ( fis) ;
int numberOfSheets = workBook. getNumberOfSheets ( ) ;
List< read_excel> list = new ArrayList < > ( ) ;
for ( int s = 0 ; s < numberOfSheets ; s++ ) {
Sheet sheetAt = workBook. getSheetAt ( s) ;
String sheetName = sheetAt. getSheetName ( ) ;
System. out. println ( "工作表名称:" + sheetName) ;
int rowsOfSheet = sheetAt. getPhysicalNumberOfRows ( ) ;
System. out. println ( "当前表格的总行数:" + rowsOfSheet) ;
Row row0 = sheetAt. getRow ( 0 ) ;
int physicalNumberOfCells = sheetAt. getRow ( 0 ) . getPhysicalNumberOfCells ( ) ;
String[ ] title = new String [ physicalNumberOfCells] ;
for ( int i = 0 ; i < physicalNumberOfCells; i++ ) {
title[ i] = row0. getCell ( i) . getStringCellValue ( ) ;
System. out. print ( title[ i] + " " ) ;
}
System. out. println ( ) ;
for ( int r = 1 ; r < rowsOfSheet; r++ ) {
Row row = sheetAt. getRow ( r) ;
int cellCount = row. getPhysicalNumberOfCells ( ) ;
read_excel excel = new read_excel ( ) ;
for ( int c = 0 ; c < cellCount; c++ ) {
Cell cell = row. getCell ( c) ;
int cellType = cell. getCellType ( ) ;
String cellValue = null;
switch ( cellType) {
case Cell. CELL_TYPE_STRING:
cellValue = cell. getStringCellValue ( ) ;
break ;
case Cell. CELL_TYPE_NUMERIC:
if ( DateUtil. isCellDateFormatted ( cell) ) {
cellValue = sdf. format ( cell. getDateCellValue ( ) ) ;
}
else {
cellValue = String. valueOf ( cell. getNumericCellValue ( ) ) ;
}
break ;
case Cell. CELL_TYPE_BOOLEAN:
cellValue = String. valueOf ( cell. getBooleanCellValue ( ) ) ;
break ;
case Cell. CELL_TYPE_BLANK:
cellValue = cell. getStringCellValue ( ) ;
break ;
case Cell. CELL_TYPE_ERROR:
cellValue = "错误" ;
break ;
case Cell. CELL_TYPE_FORMULA:
cellValue = "错误" ;
break ;
default :
cellValue = "错误" ;
}
if ( cellValue == "" || "错误" . equals ( cellValue) ) {
System. out. println ( "第" + r + "行,第" + c+ "列[" + title[ c] + "]数据错误!" ) ;
return ;
}
switch ( c) {
case 0 :
excel. setId ( cellValue) ;
break ;
case 1 :
excel. setName ( cellValue) ;
break ;
case 2 :
excel. setAge ( Integer. parseInt ( cellValue) ) ;
break ;
case 3 :
excel. setTime ( cellValue) ;
break ;
default :
break ;
}
System. out. print ( cellValue + " " ) ;
}
list. add ( excel) ;
System. out. println ( ) ;
}
System. out. println ( ) ;
}
if ( fis != null) {
fis. close ( ) ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
} else {
System. out. println ( "文件不存在!" ) ;
}
}
public static void readExcel2 ( )
{
SimpleDateFormat fmt = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
try {
File excelFile = new File ( "D:\\测试生成Excel文件\\withoutHead2.xls" ) ;
FileInputStream is = new FileInputStream ( excelFile) ;
Workbook workbook = WorkbookFactory. create ( is) ;
int sheetCount = workbook. getNumberOfSheets ( ) ;
for ( int s = 0 ; s < sheetCount; s++ ) {
Sheet sheet = workbook. getSheetAt ( s) ;
int rowCount = sheet. getPhysicalNumberOfRows ( ) ;
for ( int r = 0 ; r < rowCount; r++ ) {
Row row = sheet. getRow ( r) ;
int cellCount = row. getPhysicalNumberOfCells ( ) ;
for ( int c = 0 ; c < cellCount; c++ ) {
Cell cell = row. getCell ( c) ;
int cellType = cell. getCellType ( ) ;
String cellValue = null;
cell. setCellType ( Cell. CELL_TYPE_STRING) ;
cellValue = cell. getStringCellValue ( ) ;
System. out. print ( cellValue + " " ) ;
}
System. out. println ( ) ;
}
}
}
catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
public void stop ( ) {
for ( int i = 0 ; i< 10 ; i++ ) {
for ( int j = 0 ; j< 10 ; j++ )
{
System. out. println ( j) ;
if ( i< 10 ) {
System. out. println ( "retuen 跳出循环" ) ;
return ;
}
}
}
}
}
1 、创建Workbook对象
File file = new File ( path) ;
FileInputStream fis = null;
Workbook workBook = null;
fis = new FileInputStream ( file) ;
workBook = WorkbookFactory. create ( fis) ;
2 、获取第一张表
int numberOfSheets = workBook. getNumberOfSheets ( ) ;
for ( int s = 0 ; s < numberOfSheets ; s++ ) {
Sheet sheetAt = workBook. getSheetAt ( s) ;
}
3 、获取sheet表中的总行数
String sheetName = sheetAt. getSheetName ( ) ;
ystem. out. println ( "工作表名称:" + sheetName) ;
int rowsOfSheet = sheetAt. getPhysicalNumberOfRows ( ) ;
System. out. println ( "当前表格的总行数:" + rowsOfSheet) ;
4 、获取sheet表中的总列数
for ( int r = 1 ; r < rowsOfSheet; r++ ) {
Row row = sheetAt. getRow ( r) ;
int cellCount = row. getPhysicalNumberOfCells ( ) ;
}