本文主要是介绍基于Java的Excel文件解析,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
.xlsx文件:
代码:
package com.hsbc.dashboard.utils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class FileParseExcel {
public static final String file_path = "D:\\SpringWeb\\src\\test\\resources\\UserProfiles\\user.xlsx";
@Test
public void readUserExcel() throws IOException {
File file = new File(file_path);
FileInputStream fis = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheet("userinformation");
int lastRowNum = sheet.getLastRowNum();
Cell userCell;
for(int i = 1; i <= lastRowNum; i++){
Map<String, String> userData = new HashMap<>();
Row row = sheet.getRow(i);
userCell = row.getCell(1); //获取单元格
userCell.setCellType(CellType.STRING); //设置单元格类型
String dd = userCell.getStringCellValue(); //获取单元格数据
userData.put("age", dd);
System.out.println(userData);
}
}
}
运行结果:
{age=20}
{age=21}
{age=25}
{age=22}
这篇关于基于Java的Excel文件解析的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!