java poi读取excel文件
来源:互联网 发布:手机淘宝小二怎么联系 编辑:程序博客网 时间:2024/04/30 08:29
/**
* 读取excel文件
* @param filePath 文件相对路径
* @return
*/
public static Map<String, String> readExcelToMap(String filePath){
Map<String, String> keyMap = new HashMap<String, String>();
File file = null;
FileInputStream is = null;
filePath = CommonUtil.class.getResource("/").getPath() + filePath;
try {
file = new File(filePath);
is = new FileInputStream(file);
Workbook workbook = WorkbookFactory.create(is);
if(workbook.getNumberOfSheets() < 1){
return null;
}
Sheet sheet = workbook.getSheetAt(0);
int rowCount = sheet.getPhysicalNumberOfRows();
for(int i = 0; i < rowCount; i++){
Row row = sheet.getRow(i);
Cell keyCell = row.getCell(0);
String value = null;
if(keyCell != null && keyCell.getCellType() == Cell.CELL_TYPE_STRING){
value = keyCell.getStringCellValue();
Cell valueCell = row.getCell(1);
if(valueCell.getCellType() == Cell.CELL_TYPE_STRING){
String key = valueCell.getStringCellValue();
key = key.trim();
value = value.trim();
keyMap.put(key, value);
}
}
}
} catch (Exception e) {
throw new RuntimeException("打开 " + filePath + " 时出错!", e);
} finally {
if(is != null){
try {
is.close();
} catch (IOException e) {
//
}
}
}
return keyMap;
* 读取excel文件
* @param filePath 文件相对路径
* @return
*/
public static Map<String, String> readExcelToMap(String filePath){
Map<String, String> keyMap = new HashMap<String, String>();
File file = null;
FileInputStream is = null;
filePath = CommonUtil.class.getResource("/").getPath() + filePath;
try {
file = new File(filePath);
is = new FileInputStream(file);
Workbook workbook = WorkbookFactory.create(is);
if(workbook.getNumberOfSheets() < 1){
return null;
}
Sheet sheet = workbook.getSheetAt(0);
int rowCount = sheet.getPhysicalNumberOfRows();
for(int i = 0; i < rowCount; i++){
Row row = sheet.getRow(i);
Cell keyCell = row.getCell(0);
String value = null;
if(keyCell != null && keyCell.getCellType() == Cell.CELL_TYPE_STRING){
value = keyCell.getStringCellValue();
Cell valueCell = row.getCell(1);
if(valueCell.getCellType() == Cell.CELL_TYPE_STRING){
String key = valueCell.getStringCellValue();
key = key.trim();
value = value.trim();
keyMap.put(key, value);
}
}
}
} catch (Exception e) {
throw new RuntimeException("打开 " + filePath + " 时出错!", e);
} finally {
if(is != null){
try {
is.close();
} catch (IOException e) {
//
}
}
}
return keyMap;
}
主要代码就是这样
0 0
- java poi读取excel文件
- Java--POI读取excel文件
- java poi读取excel文件
- java 读取Excel文件(POI)
- java POI 读取EXCEL文件
- [JAVA] java读取Excel文件,Apache POI
- java中用poi读取Excel文件
- Java用poi读取excel文件
- Java 读取Excel文件(POI)
- java 操作 poi 解析、读取 Excel 文件
- 使用java poi 读取excel文件
- 【java】使用POI读取excel文件内容
- JAVA利用Apache Poi读取Excel文件
- java poi读取excel文件内容
- Java使用poi读取Excel文件例子
- 【Apache POI】Java 读取Excel文件
- Java使用POI读取Excel文件
- Java用poi读取excel文件
- 设置UIButton的文字显示位置、字体的大小、字体的颜色
- ping排错三步走
- spark createDirectStream保存kafka offset(JAVA实现)
- scala-45:Scala中Context Bounds代码实战及其在Spark中的应用源码解析
- ubuntu下的jdk,eclipse安装
- java poi读取excel文件
- Oracle RAC 11g上创建服务
- 如何查看Python是32位还是64位
- php 日期函数
- Java拾遗之Spring Web MVc
- ffmpeg在windows上编译
- leetcode 140:Word Break II 采用很巧妙的动态规划和DFS联合使用 JAVA源代码实现
- Treap 树
- 「译」Android最佳实践指南——GitHub Star 7000+