Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Generating Excel file using Java POI API

3272044Jul 7 2016 — edited Oct 14 2016

Hi All,

I was working on generation on excel file using POI API and written below code whic works fine:

XSSFWorkbook workbook = new XSSFWorkbook();

XSSFSheet sheet = workbook.createSheet("PMS Data");

Map<String, Object[]> data = new TreeMap<String, Object[]>();

data.put("1", new Object[] {"ID", "NAME", "LASTNAME","TENANT NAME", "RENT DURATION", "CONTRACT DURATION", "CITY"});

data.put("2", new Object[] {1, "Amit", "Shukla", "Ajay Dhawan", "12 months", "10 months", "Bangaluru"});

data.put("3", new Object[] {2, "Lokesh", "Gupta", "Priyanka Dutta", "12 months", "10 months", "Pune"});

data.put("4", new Object[] {3, "John", "Adwards", "Ravi Jain", "12 months", "10 months", "Noida"});

data.put("5", new Object[] {4, "Brian", "Schultz", "Piyush Misra", "12 months", "10 months", "Gurgaon"});

Set<String> keyset = data.keySet();

int rownum = 0;

for (String key : keyset)

{

Row row = sheet.createRow(rownum++);

Object [] objArr = data.get(key);

int cellnum = 0;

for (Object obj : objArr)

{

Cell cell = row.createCell(cellnum++);

if(obj instanceof String)

cell.setCellValue((String)obj);

else if(obj instanceof Integer)

cell.setCellValue((Integer)obj);

}

}

try

{

FileOutputStream out = new FileOutputStream(new File("PMS.xlsx"));

workbook.write(out);

out.close();

System.out.println("PMS.xlsx written successfully on disk.");

}

catch (Exception e)

{

e.printStackTrace();

}

Now i want to modify the above code in such a way that it should calls one method sat generateExcel(List<Objects>, List<String>)

where List of Objects contains only headers like {"ID", "NAME", "LASTNAME","TENANT NAME", "RENT DURATION", "CONTRACT DURATION", "CITY"} and List of String contains their values for which i'm facing problem in mapping values . Can anybody help me how to achieve it. Pls it is urgent if anybdy can.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 4 2016
Added on Jul 7 2016
0 comments
929 views