I have a Map
with single key and multiple values.I'm trying to write those data in an excel sheet.
Map<String, Object[]> data=new TreeMap<String, Object[]>();
In this map I'm putting some values.
ContentRoleType role=ContentRoleType.toContentRoleType("PRIMARY");
QueryResult contentQuery=ContentHelper.service.getContentsByRole(cadDoc, role);
System.out.println("Content length of "+cadDoc.getName()+" is "+contentQuery.size());
ContentLength=contentQuery.size();
data.put(CadName, new Object[]{Iteration,ContentLength});
Then I'm iterating over this map and writing those elements into the cells.
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
{
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("D:\\testExcel.xlsx"));
workbook.write(out);
out.close();
System.out.println("testExcel.xlsx written successfully on disk.");
}
catch (Exception e)
{
e.printStackTrace();
}
The Problem here is in my excel sheet I can able to see only two columns that are values.I can't print the key inside excel sheet.I want print my key as my first column then two and three are values.How to add the key inside this loop?
Thanks for all
You wouldn't add the key inside the inner loop - it's not part of the data you're looping over. You need to add it before the loop:
row.createCell(0).setCellValue(key); // Key is in column 0
int cellNum = 1; // Values now start at column 1
// Loop as before
(This is still within the outer loop, of course - you want to display one key per row.)
See more on this question at Stackoverflow