1. PersonDO
import java.util.ArrayList;
/**
*
*/
/**
* @author abhinav
*/
public class PersonDO
{
private String name;
private String city;
private String phone;
public static ArrayList<PersonDO> getPersons( Number n )
{
ArrayList<PersonDO> list = new ArrayList<PersonDO>();
for ( int i = 0; i <= n.intValue(); i++ )
{
PersonDO p = new PersonDO();
p.setName( "abhinav " + i );
p.setCity( "pune " + i );
p.setPhone( "phone " + i );
list.add( p );
}
return list;
}
/**
* @return the name
*/
public String getName()
{
return name;
}
/**
* @param name the name to set
*/
public void setName( String name )
{
this.name = name;
}
/**
* @return the city
*/
public String getCity()
{
return city;
}
/**
* @param city the city to set
*/
public void setCity( String city )
{
this.city = city;
}
/**
* @return the phone
*/
public String getPhone()
{
return phone;
}
/**
* @param phone the phone to set
*/
public void setPhone( String phone )
{
this.phone = phone;
}
}
==============================================
2. Write to Excel
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//import statements
public class ReadExcelDemo
{
public static void main( String[] args )
{
System.out.println( java.lang.Runtime.getRuntime().maxMemory() );
try
{
FileInputStream file = new FileInputStream( new File( "path2template.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<PersonDO> persons = PersonDO.getPersons( 1000000 );
int startIndex = 1;
System.out.println( "Populating data..." );
for ( PersonDO p : persons )
{
// Row row = sheet.getRow( startIndex );
Row row = sheet.createRow( startIndex ); // getRow( startIndex );
row.createCell( 0 ).setCellValue( p.getName() );
row.createCell( 1 ).setCellValue( p.getPhone() );
row.createCell( 2 ).setCellValue( p.getCity() );
if ( startIndex % 500 == 0 )
{
System.out.println( "At " + startIndex );
System.out.println( java.lang.Runtime.getRuntime().freeMemory() );
}
startIndex++;
}
System.out.println( "Writing to excel..." );
FileOutputStream fileOut = new FileOutputStream( "output_file.xlsx" );
workbook.write( fileOut );
System.out.println( "Completed..." );
fileOut.close();
file.close();
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
Used JARS
G:\WORKSPACE\ALLPROJECTS\EXCELPOI\LIBS
- dom4j-1.6.1.jar
- poi-3.10-FINAL-20140208.jar
- poi-examples-3.10-FINAL-20140208.jar
- poi-ooxml-3.10-FINAL-20140208.jar
- poi-ooxml-schemas-3.10-FINAL-20140208.jar
- xmlbeans-2.3.0.jar
ExcelTemplate
https://drive.google.com/file/d/0B3prgPRdV5ZbOEhVSEpqZEV6SjQ/edit?usp=sharing
No comments:
Post a Comment