Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Export from a list containing objects to excel file in java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 26.9k
    Comment on it

    Suppose you have a list containing objects. You can export it to the excel file. 

    You will require the following jars for this:

    • xmlbeans-2.3.0.jar
    • poi-3.9-20121203.jar
    • poi-ooxml-3.9-20121203.jar
    • poi-ooxml-schemas-3.9-20121203.jar
    • dom4j-1.6.1.jar

    If using maven add the following dependencies:

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.9</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.poi</groupId>
    	<artifactId>poi-ooxml</artifactId>
    	<version>3.9</version>
    </dependency>

    Example:

    Suppose we have class User.java

    package com.demo.rough;
    
    public class User {	
    	private String id;
    	private String name;
    	public User(){
    		this.id="";
    		this.name="";
    	}
    	public User(String id, String name) {
    		this.id = id;
    		this.name = name;
    	}
    	public String getId() {
    		return id;
    	}
    	public void setId(String id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    }
    

    Then, our main class ExportList.java

    import java.io.File;
    import java.io.FileOutputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    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 ExportList {
    	public static void main(String args[]) 
        {
            	List<User> list=new ArrayList<User>();
            	User user1=new User("1","John");
            	User user2=new User("2","Chris");
            	User user3=new User("3","Roth");
            	list.add(user1);
            	list.add(user2);
            	list.add(user3);    	
           
            	 try
                 {	
            XSSFWorkbook workbook = new XSSFWorkbook(); 
                  
            XSSFSheet sheet = workbook.createSheet("sheet1");// creating a blank sheet
             int rownum = 0;
             for (User user : list)
                {
                Row row = sheet.createRow(rownum++);
                createList(user, row);
                    
            }                   
           
                FileOutputStream out = new FileOutputStream(new File("NewFile.xlsx")); // file name with path
                workbook.write(out);
                out.close();
               
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
        
    }
      private static void createList(User user, Row row) // creating cells for each row
    {
    	    Cell cell = row.createCell(0);
    	    cell.setCellValue(user.getId());
    	 
    	    cell = row.createCell(1);
    	    cell.setCellValue(user.getName());
    	 
    	   
    	}
    }
    

    So your List will be exported to excel format with the file name NewFile.xlsx and will contain the records which we entered in the list. 

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: