POI-Excel读
POI-Excel读
03 | 07
03版本
public void testRead03() throws IOException {
//获取文件流
FileInputStream fileInputStream = new FileInputStream(PATH + "testWrite03.xls");
//1,读取流文件
Workbook workbook = new HSSFWorkbook(fileInputStream);
//2,得到表
Sheet sheet = workbook.getSheetAt(0);
//3,得到行
Row row = sheet.getRow(0);
//4,得到具体的单元格(列)
Cell cell1 = row.getCell(0);
Cell cell2 = row.getCell(1);
// 读取值的时候一定要注意值的类型
// getStringCellValue 目前获取的是字符串
System.out.println(cell1.getStringCellValue());
// getNumericCellValue 目前获取的是数字
System.out.println(cell2.getNumericCellValue());
fileInputStream.close();
}
07版本
public void testRead07() throws IOException {
//获取文件流
FileInputStream fileInputStream = new FileInputStream(PATH + "testWrite07.xlsx");
//1,读取流文件
Workbook workbook = new XSSFWorkbook(fileInputStream);
//2,得到表
Sheet sheet = workbook.getSheetAt(0);
//3,得到行
Row row = sheet.getRow(0);
//4,得到具体的单元格(列)
Cell cell1 = row.getCell(0);
Cell cell2 = row.getCell(1);
// 读取值的时候一定要注意值的类型
// getStringCellValue 目前获取的是字符串
System.out.println(cell1.getStringCellValue());
// getNumericCellValue 目前获取的是数字
System.out.println(cell2.getNumericCellValue());
fileInputStream.close();
}
注意获取的值的类型即可
读取不同的数据类型
public void testCellType() throws IOException {
//获取文件流
FileInputStream fileInputStream = new FileInputStream(PATH + "明细表.xls");
//1,读取流文件
Workbook workbook = new HSSFWorkbook(fileInputStream);
//2,得到表
Sheet sheet = workbook.getSheetAt(0);
//3,得到行
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.print(cellType);
}
}
System.out.println();
}
//获取表中的内容
//获取Excel数据当前有多少行
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 (cellType){
case STRING://字符串
System.out.print("【String类型】");
cellValue = cell.getStringCellValue();
break;
case _NONE:// 未知类型,仅限内部使用
System.out.print("【_NONE类型】");
break;
case BOOLEAN://布尔类型
System.out.print("【BLANK类型】");
break;
case NUMERIC://数字(日期、普通数字)
System.out.print("【NUMERIC类型】");
if(DateUtil.isCellDateFormatted(cell)){
System.out.print("【日期】");
cellValue = new DateTime(cell.getDateCellValue()).toString("yyyy-MM-dd");
}else {
//不是日期格式,防止数字过长
System.out.print("【转换为字符串输出】");
cell.setCellType(CellType.STRING);
cellValue = cell.toString();
}
break;
case FORMULA:// 公式
System.out.print("【FORMULA类型(公式)】");
break;
case BLANK:// 空单元格-没有值,但有单元格样式
System.out.print("【BLANK类型】");
break;
case ERROR:// 错误
System.out.print("【数据类型错误】");
break;
}
System.out.println(cellValue);
}
}
}
}
fileInputStream.close();
}
可以将FileInputStream fileInputStream = new FileInputStream(PATH + "明细表.xls");提取成入参,做成工具类;
计算公式(没啥意思)
public void testFormula() throws IOException {
//获取文件流
FileInputStream fileInputStream = new FileInputStream(PATH + "公式.xls");
//1,读取流文件
Workbook workbook = new HSSFWorkbook(fileInputStream);
//2,得到表
Sheet sheet = workbook.getSheetAt(0);
//3,得到行
Row row = sheet.getRow(4);
Cell cell = row.getCell(0);
//拿到计算公式
FormulaEvaluator fFormulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook)workbook);
//输出单元格内容
CellType cellType = cell.getCellType();
switch (cellType){
case FORMULA:// 公式
System.out.print("【FORMULA类型(公式)】");
//获取公式
String cellFormula = cell.getCellFormula();
System.out.println(cellFormula);
double cellValue = cell.getNumericCellValue();
System.out.println(cellValue);
break;
}
fileInputStream.close();
}

浙公网安备 33010602011771号