Java 读取excel 文件流

public static void initType(String path) {

try {

//1 获取excel文件流 excel xls 文件 暂不支持xlsx
if (path.contains("xlsx") || path.contains("XLSX")) {
System.err.println("请使用xls格式文件");
return;
}
InputStream inputStream = new FileInputStream(path);
POIFSFileSystem fileSystem = new POIFSFileSystem(inputStream);
HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
//2 获取sheet 列数
int sheets = workbook.getNumberOfSheets();
//3 遍历所有sheet列
for (int i = 0; i < sheets; i++) {
//获取sheet
HSSFSheet sheet = workbook.getSheetAt(i);
//读取第一行
HSSFRow headerRow = sheet.getRow(0);
//获取sheet 所有行数
int rows = sheet.getPhysicalNumberOfRows();
List<String> headerColumns = new ArrayList<>();
for (int j = 0; j < headerRow.getPhysicalNumberOfCells(); j++) {
HSSFCell cell = headerRow.getCell(j);
cell.getStringCellValue();
headerColumns.add(cell.getStringCellValue());
}
//获取类型
HSSFRow secondRow = sheet.getRow(1);

int cells = secondRow.getPhysicalNumberOfCells();

//对第二行的数据进行操作
for (int j = 1; j < rows; j++) {
HSSFRow row = sheet.getRow(j);
List<String> cellsValue = new ArrayList<>();
//获取字段属性
for (int k = 2; k < cells; k++) {
cellsValue.add(row.getCell(k).getStringCellValue());
}


}


}

}
posted @ 2019-03-20 14:54  夏风中的Young_Uncle  阅读(5764)  评论(0编辑  收藏  举报