import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFComment;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
/**
* @author FUCCKL
* @version 2019.1.7 14:30~
* 读取EXCEL数据存放到列表里,可以判断数据属于什么类型
*/
public class readdata {
static int row=0;
static int column=0;
public static ArrayList readdata(String path)throws Exception//读取字符串数据
{
XSSFWorkbook workbook;
XSSFSheet sheet;
XSSFCell cell;
ArrayList xlist=new ArrayList();
InputStream iStream=new FileInputStream(path);
workbook=new XSSFWorkbook(iStream);
sheet=workbook.getSheetAt(0);
row=sheet.getLastRowNum()+1;//获取行数
column=sheet.getRow(0).getPhysicalNumberOfCells();//获取列数
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
XSSFRow row1=sheet.getRow(i);
XSSFCell cell1 = row1.getCell(j); // 获取对应元素
CellType str0=cell1.getCellType();
switch (str0) {
case STRING: //字符串类型
String str=cell1.getStringCellValue();
xlist.add(str);
break;
case BOOLEAN: //布尔类型
boolean str1=cell1.getBooleanCellValue();
xlist.add(str1);
break;
case NUMERIC: //数值类型
double str2=cell1.getNumericCellValue();
xlist.add(str2);
break;
}
}
}
return xlist;
}
}
读取的数据被存放到自己定义的列表里,调用方式如下:
ArrayList movielist=new ArrayList();
String path0="D:\\eclipse-workspace\\recommend system\\movielist.xlsx";
movielist=readdata.readdata(path0);