1

単一のキーと複数の値がMapあります。それらのデータを Excel シートに書き込もうとしています。

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

このマップでは、いくつかの値を入れています。

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

次に、このマップを繰り返し処理し、それらの要素をセルに書き込みます。

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

ここでの問題は、Excel シートにあり、値である 2 つの列しか表示できません。Excel シート内にキーを印刷できません。キーを最初の列として印刷し、2 つと 3 つが値です。追加方法このループ内のキー?

全てに感謝

4

1 に答える 1

1

内側のループ内にキーを追加しないでください。これは、ループしているデータの一部ではありません。ループのに追加する必要があります。

row.createCell(0).setCellValue(key); // Key is in column 0
int cellNum = 1; // Values now start at column 1
// Loop as before

(もちろん、これはまだ外側のループ内にあります。行ごとに 1 つのキーを表示する必要があります。)

于 2013-10-16T06:02:17.323 に答える