private static final String EXCEL_XLS = \"xls\";
private static final String EXCEL_XLSX = \"xlsx\";
/**
* 判断文件是否是excel
*
* @throws Exception
*/
public static void checkExcelVaild(File file) throws Exception {
if (!file.exists()) {
throw new Exception(\"文件不存在\");
}
if (!(file.isFile() && (file.getName().endsWith(EXCEL_XLS) || file.getName().endsWith(EXCEL_XLSX)))) {
throw new Exception(\"文件不是Excel\");
}
}
public static void main(String[] args) throws Exception {
// C:\\Users\\lqf\\Desktop\\import.xlsx
String path = \"C://Users//lqf//Desktop//import.xlsx\";
List< > list = new ArrayList<>();
try {
// 同时支持Excel 2003、2007
File excelFile = new File(path); // 创建文件对象
FileInputStream in = new FileInputStream(excelFile); // 文件流
checkExcelVaild(excelFile);
Workbook workbook = WorkbookFactory.create(in); // 这种方式
Sheet sheet = workbook.getSheetAt(0); // 遍历第一个Sheet
o = new ();
for (Row row : sheet) {
for (int i = 0; i < row.getLastCellNum(); i++) {
Cell cell = row.getCell(i);
int cellType = cell.getCellType();
switch (cellType) {
case 0:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
o = cell.getDateCellValue();
} else if (cell.getCellStyle().getDataFormat() == 58) {
double value = cell.getNumericCellValue();
o = DateUtil.getJavaDate(value);
} else {
o = cell.getNumericCellValue();
}
break;
case 1:
o = cell.getStringCellValue();
break;
case 2:
o = cell.getCellFormula();
break;
case 3:
o = \"\";
break;
case 4:
o = cell.getBooleanCellValue();
break;
default:
// cell.setCellType(1);
o = cell.getStringCellValue();
break;
}
// 如果要修改笨本地的excel
/*
* cell.setCellValue(\"\"); FileOutputStream excelFileOutPutStream = new
* FileOutputStream(path);// 写数据到这个路径上 workbook.write(excelFileOutPutStream);
*/
list.add(o);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
for ( o : list) {
System.out.println(o);
}
}
}
继续阅读与本文标签相同的文章
大咖云集!“创响中国”昆明经开区站启动
-
中国智能家居的蝴蝶效应
2026-05-18栏目: 教程
-
2019年回顾 - Joomla前12名SEO扩展和插件
2026-05-18栏目: 教程
-
数据结构与算法之约瑟夫问题
2026-05-18栏目: 教程
-
Spring Batch 4.2 新特性
2026-05-18栏目: 教程
-
windows(ECS)网卡信息不一致
2026-05-18栏目: 教程
