首先maven先引入
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ReadExcel_1 {
private Workbook wb; //工作谱
private Sheet sheet; //工作表
private Row row; //工作行
//读取的Excel文件路径
private final static String path = "D:\\test\\自己的文件名.xlsx";
//读取Excels表格
public ReadExcel_1(String filepath) {
if(filepath==null){
return;
}
try {
/** 根据文件的路径得到文件流*/
InputStream is = new FileInputStream(filepath);
/** 得到工作簿,可以近似看成是excel表格对象吧*/
wb = new XSSFWorkbook(is);
} catch (IOException e) {
System.out.println("IOException" + e);
}
}
@SuppressWarnings("deprecation")
public void readExcelContent() throws Exception{
if(wb==null){
throw new Exception("Workbook对象为空!");
}
/**获得excel表格中有多少个表,例如sheet1,sheet2,sheet3 ,那么就是3个*/
int a = wb.getNumberOfSheets();
System.out.println("======"+wb.getNumberOfSheets()+"=========");
for(int k=0;k<wb.getNumberOfSheets();k++){
/**一个一个的处理表,也就是依次处理sheet1,sheet2,sheet3*/
sheet = wb.getSheetAt(k);
/**获得该表中的行数*/
int rowNum = sheet.getPhysicalNumberOfRows();
System.out.println("=======总行数"+rowNum);
int colNum = 0;
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 1; i < rowNum; i++) {
/**获得当前行*/
row = sheet.getRow(i);
/**获得当前行对应的列数*/
colNum = row.getPhysicalNumberOfCells();
for(int j =0; j < colNum; j++){
/**获得单元格中的元素*/
XSSFCell cell = (XSSFCell)sheet.getRow(i).getCell(j);
String res = "";
res = getStringCellValue(cell);
System.out.print(res+ j + " ");
}
System.out.println();
}
System.out.println("新的sheet");
}
}
@SuppressWarnings("deprecation")
private static String getStringCellValue(XSSFCell cell) {
String strCell = "";
SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd");
if (cell == null) {
return "";
}
/**一般excel中单元格的类型有数字类型,字符类型,布尔类型等等,其中数字类型又分为日期和数
字*/
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_NUMERIC:
if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
Date d = cell.getDateCellValue();
String tmpDate = fmt.format(d);
strCell = String.valueOf(tmpDate);
}else{
strCell = String.valueOf(cell.getNumericCellValue());
}
break;
case XSSFCell.CELL_TYPE_STRING:
strCell = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
strCell = String.valueOf(cell.getBooleanCellValue());
break;
case XSSFCell.CELL_TYPE_BLANK:
strCell = "";
break;
default:
strCell = "";
break;
}
if (strCell.equals("")) {
return "";
}
return strCell;
}
public static void main(String args[]){
ReadExcel_1 re = new ReadExcel_1(path);
try{
re.readExcelContent();
} catch (Exception e) {
e.printStackTrace();
}
}
}
参考资料:POI处理Excel中各种日期格式问题
java读取Excel —— XSSFWorkbook 找不到该类
感谢以上博主。