将 Excel 数据设置为 Java Bean
Set Excel Data to Java Bean
I want to set the excel data to a Java POJO
My Excel will be
firstName lastName amount
A B E
C D F
我创建了与 excel Headers
完全匹配的 Java Bean
public class ExcelBean {
private String firstName;
private String lastName;
private String amount;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
}
下面的代码执行 excel 行。它遍历每一列并获取单元格数据
static File file;
static FileInputStream fileInputStream;
static XSSFWorkbook workBook;
static XSSFSheet sheet;
static XSSFRow row;
static XSSFCell cell;
static int rowNum;
static int j;
private static final String filePath = "Excel.xlsx";
public static XSSFSheet getSheet() throws IOException {
try {
file = new File(filePath);
fileInputStream = new FileInputStream(file);
workBook = new XSSFWorkbook(fileInputStream);
sheet = workBook.getSheetAt(0);
} catch (Exception e) {
System.out.println("Error while trying to get the sheet " + e.getMessage());
}
return sheet;
}
public static void getExcelData(XSSFSheet sheet) {
int totalRows = sheet.getLastRowNum()+1;
int totalColums = sheet.getRow(0).getLastCellNum();
ExcelBean excelBean=new ExcelBean();
for(int i=0;i<totalRows;i++){
row=sheet.getRow(i);
for(int j=0;j<totalColums;j++){
String fieldValue = row.getCell(j).getStringCellValue();
System.out.println(fieldValue);
}
}
}
public static void main(String[] args) throws IOException{
sheet=ExcelUtil.getSheet();
ExcelUtil.getExcelData(sheet);
}
我的意图是从 excel 获取值并将其设置为我不知道的 java bean。谁能帮我实现这个功能。
像这样更新 getExcelData() 函数
public static void getExcelData(XSSFSheet sheet) {
int totalRows = sheet.getLastRowNum()+1;
int totalColums = sheet.getRow(0).getLastCellNum();
ExcelBean excelBean=new ExcelBean();
for(int i=0;i<totalRows;i++){
row=sheet.getRow(i);
for(int j=0;j<totalColums;j++){
String fieldValue = row.getCell(j).getStringCellValue();
System.out.println(fieldValue);
switch(j)
{
case 0:excelBean.setFirstName(fieldValue);break;
case 1:excelBean.setLastName(fieldValue);break;
case 2:excelBean.setAmount(fieldValue);break;
default:break;
}
}
}
}
I want to set the excel data to a Java POJO
My Excel will be
firstName lastName amount
A B E
C D F
我创建了与 excel Headers
完全匹配的 Java Beanpublic class ExcelBean {
private String firstName;
private String lastName;
private String amount;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
}
下面的代码执行 excel 行。它遍历每一列并获取单元格数据
static File file;
static FileInputStream fileInputStream;
static XSSFWorkbook workBook;
static XSSFSheet sheet;
static XSSFRow row;
static XSSFCell cell;
static int rowNum;
static int j;
private static final String filePath = "Excel.xlsx";
public static XSSFSheet getSheet() throws IOException {
try {
file = new File(filePath);
fileInputStream = new FileInputStream(file);
workBook = new XSSFWorkbook(fileInputStream);
sheet = workBook.getSheetAt(0);
} catch (Exception e) {
System.out.println("Error while trying to get the sheet " + e.getMessage());
}
return sheet;
}
public static void getExcelData(XSSFSheet sheet) {
int totalRows = sheet.getLastRowNum()+1;
int totalColums = sheet.getRow(0).getLastCellNum();
ExcelBean excelBean=new ExcelBean();
for(int i=0;i<totalRows;i++){
row=sheet.getRow(i);
for(int j=0;j<totalColums;j++){
String fieldValue = row.getCell(j).getStringCellValue();
System.out.println(fieldValue);
}
}
}
public static void main(String[] args) throws IOException{
sheet=ExcelUtil.getSheet();
ExcelUtil.getExcelData(sheet);
}
我的意图是从 excel 获取值并将其设置为我不知道的 java bean。谁能帮我实现这个功能。
像这样更新 getExcelData() 函数
public static void getExcelData(XSSFSheet sheet) {
int totalRows = sheet.getLastRowNum()+1;
int totalColums = sheet.getRow(0).getLastCellNum();
ExcelBean excelBean=new ExcelBean();
for(int i=0;i<totalRows;i++){
row=sheet.getRow(i);
for(int j=0;j<totalColums;j++){
String fieldValue = row.getCell(j).getStringCellValue();
System.out.println(fieldValue);
switch(j)
{
case 0:excelBean.setFirstName(fieldValue);break;
case 1:excelBean.setLastName(fieldValue);break;
case 2:excelBean.setAmount(fieldValue);break;
default:break;
}
}
}
}