java读取excel文件中的数据
来源:互联网 发布:c语言编译器安卓 编辑:程序博客网 时间:2024/05/21 11:09
private static List<List<List<String>>> readExcelXlsx(InputStream is) throws IOException{
String result_s = "";
//result用来存放从Excel中读取到的数据
List<List<List<String>>> result = new ArrayList<List<List<String>>>();
//获得excel文件的IO流
//InputStream is = new FileInputStream(file);
//得到后缀为.xlsx的excel对象
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(xssfWorkbook);
//读取每一页
for(XSSFSheet xssfSheet : xssfWorkbook){
//resultSheet存放每一页的数据
List<List<String>> resultSheet = new ArrayList<List<String>>();
if(xssfSheet == null){
continue;
}
//读取每一行
for(int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++){
//得到行对象
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
//rowList存放每一行的数据
List<String> resultRow = new ArrayList<String>();
if(xssfRow == null){
continue;
}
//最小列索引
int minColIx = xssfRow.getFirstCellNum();
//最大列索引
int maxColIx = xssfRow.getLastCellNum();
//读取每一格
for(int colIx = minColIx; colIx <= maxColIx; colIx++){
//得到格对象
XSSFCell cell = xssfRow.getCell(colIx);
if(cell == null){
//保证结构固定
resultRow.add("");
continue;
}
//设置每个表格的类型为String
//cell.setCellType(Cell.CELL_TYPE_STRING);
//将得到的每一格的值插入到resultRow
//resultRow.add(cell.toString());
//LIUHONGYU 获取表格里的值
result_s = getValue(cell,evaluator);
resultRow.add(result_s);
}
//将读取到的每一行插入到resultSheet
resultSheet.add(resultRow);
}
//将读取到的每一页插入到result
result.add(resultSheet);
}
return result;
}
/**
* LIUHONGYU 获取表格中的值
* @param cell
* @param evaluator
* @return
*/
private static String getValue(XSSFCell cell,
XSSFFormulaEvaluator evaluator) {
String result = new String();
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:// 数字类型
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
SimpleDateFormat sdf = null;
if (cell.getCellStyle().getDataFormat() == HSSFDataFormat
.getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
}
Date date = cell.getDateCellValue();
result = sdf.format(date);
} else if (cell.getCellStyle().getDataFormat() == 58) {
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil
.getJavaDate(value);
result = sdf.format(date);
} else {
HSSFDataFormatter dataFormatter = new HSSFDataFormatter();
result = dataFormatter.formatCellValue(cell);
}
break;
case HSSFCell.CELL_TYPE_FORMULA://LIUHONGYU 2017/6/19
CellValue cellValue = evaluator.evaluate(cell);
double cv = cellValue.getNumberValue();
DecimalFormat df = new DecimalFormat("#.###");
result=df.format(cv);
break;
case HSSFCell.CELL_TYPE_STRING:// String类型
result = cell.getRichStringCellValue().toString();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = "";
default:
result = "";
break;
}
return result;
}
String result_s = "";
//result用来存放从Excel中读取到的数据
List<List<List<String>>> result = new ArrayList<List<List<String>>>();
//获得excel文件的IO流
//InputStream is = new FileInputStream(file);
//得到后缀为.xlsx的excel对象
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(xssfWorkbook);
//读取每一页
for(XSSFSheet xssfSheet : xssfWorkbook){
//resultSheet存放每一页的数据
List<List<String>> resultSheet = new ArrayList<List<String>>();
if(xssfSheet == null){
continue;
}
//读取每一行
for(int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++){
//得到行对象
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
//rowList存放每一行的数据
List<String> resultRow = new ArrayList<String>();
if(xssfRow == null){
continue;
}
//最小列索引
int minColIx = xssfRow.getFirstCellNum();
//最大列索引
int maxColIx = xssfRow.getLastCellNum();
//读取每一格
for(int colIx = minColIx; colIx <= maxColIx; colIx++){
//得到格对象
XSSFCell cell = xssfRow.getCell(colIx);
if(cell == null){
//保证结构固定
resultRow.add("");
continue;
}
//设置每个表格的类型为String
//cell.setCellType(Cell.CELL_TYPE_STRING);
//将得到的每一格的值插入到resultRow
//resultRow.add(cell.toString());
//LIUHONGYU 获取表格里的值
result_s = getValue(cell,evaluator);
resultRow.add(result_s);
}
//将读取到的每一行插入到resultSheet
resultSheet.add(resultRow);
}
//将读取到的每一页插入到result
result.add(resultSheet);
}
return result;
}
/**
* LIUHONGYU 获取表格中的值
* @param cell
* @param evaluator
* @return
*/
private static String getValue(XSSFCell cell,
XSSFFormulaEvaluator evaluator) {
String result = new String();
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:// 数字类型
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
SimpleDateFormat sdf = null;
if (cell.getCellStyle().getDataFormat() == HSSFDataFormat
.getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
}
Date date = cell.getDateCellValue();
result = sdf.format(date);
} else if (cell.getCellStyle().getDataFormat() == 58) {
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil
.getJavaDate(value);
result = sdf.format(date);
} else {
HSSFDataFormatter dataFormatter = new HSSFDataFormatter();
result = dataFormatter.formatCellValue(cell);
}
break;
case HSSFCell.CELL_TYPE_FORMULA://LIUHONGYU 2017/6/19
CellValue cellValue = evaluator.evaluate(cell);
double cv = cellValue.getNumberValue();
DecimalFormat df = new DecimalFormat("#.###");
result=df.format(cv);
break;
case HSSFCell.CELL_TYPE_STRING:// String类型
result = cell.getRichStringCellValue().toString();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = "";
default:
result = "";
break;
}
return result;
}
阅读全文
0 0
- Java读取Excel文件中的数据
- java读取excel文件中的数据
- 读取Excel文件中的数据
- Java读取Excel中的数据
- Java读取Excel中的数据
- JAVA读取Excel中的数据
- Java读取Excel中的数据
- JAVA读取Excel中的数据
- Java读取Excel中的数据
- java读取Excel中的数据
- Java读取excel中的数据
- java读取Excel中的数据
- Java读取、写入、处理Excel文件中的数据
- Java读取、写入、处理Excel文件中的数据
- Java读取、写入、处理Excel文件中的数据
- vb.net 读取EXCEL文件中的数据
- MATLAB如何读取excel文件中的数据?
- MATLAB读取Excel,Txt文件中的数据
- Unity_状态模式_028
- 2017 Multi-University Training Contest
- IDEA activiti插件actiBPM乱码问题
- 2017 Multi-University Training Contest
- springboot顶层工程,打包配置
- java读取excel文件中的数据
- 三论SOC的起源
- 面向对象的设计原则:开放-封闭原则
- 多校6055 Regular polygon
- 栈帧
- ubuntu,docker旧版本卸载,脚本安装新版本及boot2docker如何修改docker daemon的启动参数
- POJ
- 用python对两个文本进行相同项筛选
- 分享几条比较好的关于聊天机器人的链接