2014-02-25 45 views
5

任何人都可以建议我使用Apache POI将我的xlsx工作表转换为java对象。如何使用Apache POI将我的xlsx工作表转换为java对象

当量,我的Excel工作表包含两列

  • EMP_NO EMP_NAME
  • 01阿南德
  • 02库马尔

和我的java对象

Employee{ 
String empNo; 
String empName; 
} 

现在我想转换我的Excel表格到java对象。 我曾在互联网上尝试过,但大多数教程都会讨论迭代每一行,并为每个成员分配对象的值。在JAXB xml解析器中是否有像Marshaller和UnMarshaller这样的直接转换功能?

在此先感谢。

+1

请仔细阅读[问]和改善你的问题。如果你还没有尝试过任何东西,这不是要求代码的地方。 –

+0

嗨Jonathan Drapeau,感谢您的重播,其实我已经尝试了很多教程,所有的教程都谈论有关将java对象转换为excel的问题,我无法看到有关java对象的excel问题,这就是我在这里提出问题的原因。 – Anand

+0

你找到答案了吗?我想知道如果您发现映射器或如何映射到来自Apache POI Row的对象? – Erlan

回答

6

对于给定的情景,我假设表格的每一行都代表一个员工,其中第一列保留员工编号,第二列保留员工姓名。所以你可以使用以下命令:

Employee{ 
    String empNo; 
    String empName; 
} 

创建分配员工信息的方法

assignEmployee(Row row){ 
    empNo = row.getCell(0).toString(); 
    empName = row.getCell(1).toString(); 
} 

,或者如果你愿意,你可以创建一个相同的构造。

现在你只需要遍历每一行来获取/使用上述方法的信息。

Employee emp = new Employee(); 
Iterator<Row> itr = sheet.iterator(); 
    while(itr.hasNext()){ 
     Row row = itr.next(); 
     emp.assignEmployee(row); 
     // enter code here for the rest operation 
} 
+2

感谢您的回复代替迭代每一行是否有任何转换的方式,例如JAXB我们有概念称为Unmarshaller,marshaller帮助将对象转换为xml和xml到object.Is有没有在Apache POI – Anand

1

刚刚发现两个库:

希望能对大家有所帮助别人。

+1

链接到https://github.com/jittagornp/excel-object-mapping任何种类的功能可用: - ( – rjdkolb

+1

是的,刚刚看到有人试图编辑/更新我的评论以删除“1.0-SNAPSHOT”。实际上似乎回购已被其创建者删除,但在此之前已被克隆:https://github.com/pramoth/excel-object-mapping我很漂亮,它是相同的到原始文章中提到的昵称“na5cent”:http://na5cent.blogspot.fr/2015/01/excel-object-mapping.html – maxxyme

3

试试这个库内部使用Apache POI从Excel中转​​换成POJO: Poji

1
I'm using POI and I'm uploading a simple program. Hope this will help you. 
Note: Remember to change filepath. 
Jars details: dom4j-1.6.1.jar, poi-3.9.jar,poi-ooxml-3.9.jar, poi-ooxml-schemas-3.11.jar, xmlbeans-2.6.0.jar 

***My Data in Excel Table:*** 
ID NAME LASTNAME 
1.0 Ena Rana 
2.0 Meena Hanly 
3.0 Tina Mounce 
4.0 Dina Cobain 

***Model or Pojo: NewEmployee.java*** 

public class NewEmployee { 
    private Double id; 
    private String firstName; 
    private String lastName; 

    public NewEmployee(){} 

    public NewEmployee(Double id, String firstName, String lastName) { 
     super(); 
     this.id = id; 
     this.firstName = firstName; 
     this.lastName = lastName; 
    } 

    public Double getId() { 
     return id; 
    } 

    public void setId(Double id) { 
     this.id = id; 
    } 

    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; 
    }  
} 

***Main Method: ExcelToObject.java*** 

import java.io.File; 
import java.io.FileInputStream; 
import java.util.ArrayList; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

public class ExcelToObject { 

    public static void main(String[] args) { 
     try 
      { 
       FileInputStream file = new FileInputStream(new File("/home/ohelig/eclipse/New Worksheet.xlsx")); 

       //Create Workbook instance holding reference to .xlsx file 
       XSSFWorkbook workbook = new XSSFWorkbook(file); 

       //Get first/desired sheet from the workbook 
       XSSFSheet sheet = workbook.getSheetAt(0); 

       ArrayList<NewEmployee> employeeList = new ArrayList<>(); 
    //I've Header and I'm ignoring header for that I've +1 in loop 
       for(int i=sheet.getFirstRowNum()+1;i<=sheet.getLastRowNum();i++){ 
        NewEmployee e= new NewEmployee(); 
        Row ro=sheet.getRow(i); 
        for(int j=ro.getFirstCellNum();j<=ro.getLastCellNum();j++){ 
         Cell ce = ro.getCell(j); 
        if(j==0){ 
         //If you have Header in text It'll throw exception because it won't get NumericValue 
         e.setId(ce.getNumericCellValue()); 
        } 
        if(j==1){ 
         e.setFirstName(ce.getStringCellValue()); 
        } 
        if(j==2){ 
         e.setLastName(ce.getStringCellValue()); 
        }  
        } 
        employeeList.add(e); 
       } 
       for(NewEmployee emp: employeeList){ 
        System.out.println("ID:"+emp.getId()+" firstName:"+emp.getFirstName()); 
       } 
       file.close(); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 
} 
相关问题