XSSFWorkbook 读取xlsx

package cn.action.serviceImpl;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class InputExcelService {

public static void inputExcel() throws IOException{
String path = "F:/Student.xlsx";

try {
InputStream is = new FileInputStream(path); //读取指定目录下的文件
XSSFWorkbook hssfWorkbook = new XSSFWorkbook(is); //创建一个工作薄
for(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){ //循环取Sheet
XSSFSheet shset = hssfWorkbook.getSheetAt(numSheet);
if(shset == null){
continue;
}
for(int rowNum = 0; rowNum <= shset.getLastRowNum(); rowNum++){ //循环一个工作薄里面的所有行
XSSFRow hssfRow = shset.getRow(rowNum);
if(hssfRow == null){
continue;
}
int hssFCell = hssfRow.getPhysicalNumberOfCells(); //取一个行里面的所有列
for(int cellNum = 0; cellNum < hssFCell; cellNum++){ //循环取列
XSSFCell hsc = hssfRow.getCell(cellNum);
if(hsc == null){
continue;
}
System.out.print(getValue(hsc)+" "); //取值
}
System.out.println("\n");
}

hssfWorkbook.close();
is.close();
}

} catch (Exception e) {
e.printStackTrace();
}
}

@SuppressWarnings("static-access")
private static String getValue(XSSFCell hssfCell){
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {

hssfCell.setCellType(hssfCell.CELL_TYPE_STRING);
// 返回数值类型的值
return hssfCell.getStringCellValue();
}
else {
// 返回字符串类型的值
return String.valueOf(hssfCell.getStringCellValue()); }
}

public static void outputExcel(){

}

}

你可能感兴趣的:(xss)