简体   繁体   English

使用poi和hashmap编写Excel工作表

[英]Writing an excel sheet using poi and hashmaps

I am attempting to write an excel sheet using POI and hashMaps. 我正在尝试使用POI和hashMaps编写Excel工作表。 My code successfully creates and populates an excel sheet but not all of the information is written to the sheet. 我的代码成功创建并填充了一个Excel工作表,但是并非所有信息都被写入工作表。 In debug mode it seems to write 5 key, value pairs and then loops to the start at key [0]. 在调试模式下,似乎要写入5个键,值对,然后循环到键[0]处开始。 Can someone tell me where the mistake is in my logic? 有人可以告诉我我的逻辑错误在哪里吗?

HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet(make + " sheet");
        int rowNum = 0;
        ConcurrentHashMap <Integer, String[] > data = new 
                        ConcurrentHashMap<Integer, String[]>();

        data.put(rowNum++, new String[] {"VIN", "Make", "Model", "Year", 
                        "Major Group", "Caption", "Cap Filter", "Illustration",
                        "Illus Filter", "PNC", "Part Name", "Part Number", "Quantity",
                        "Part Filter"});

        for (Part p : plist){
            String PNC = p.getPNC();
            String quantity = p.getQuantity();

            if(vFilterId.contains(p.getId())) {
                data.put(rowNum++ , new String[] {vinId, make, 
                                    model, year, group, caption, capFilter, illusName,
                                    iFilter, PNC, p.getName(), p.getPartNumber(), 
                                    quantity, "NONFILTERED"});

            } else {
                data.put(rowNum++ , new String[] {vinId, make, 
                                    model, year, group, caption, capFilter, illusName,
                                    iFilter, PNC, p.getName(), p.getPartNumber(), 
                                    quantity, "NONFILTERED"});

            }

        }
        int rowIndex = 0;
        Set<Integer> keyset = data.keySet();
        for (Integer key : keyset) {
            Row row = sheet.createRow(rowIndex++);
            Object[] objArr = data.get(key);
            int cellIndex = 0;
            for (Object obj : objArr) {
                Cell cell = row.createCell(cellIndex++);
                cell.setCellValue((String) obj);
            }

        }

        try {
            FileOutputStream fos = new FileOutputStream(outputFile);
            workbook.write(fos);
            fos.close();
            System.out.println("XLS sheet written.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return ReturnConstants.SUCCESS;
}

Thanks in advance! 提前致谢!

You're using a map to store data representing rows in a table. 您正在使用地图存储表示表中行的数据。 You'd be better off using a List since maps are generally not sorted (ie the order of the entries might change over time). 您最好使用List,因为通常不会对地图进行排序(即,条目的顺序可能会随时间变化)。

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(make + " sheet");
List<String[]> data = new ArrayList<String[]>();

data.add(new String[] {"VIN", "Make", "Model", "Year", 
        "Major Group", "Caption", "Cap Filter", "Illustration",
        "Illus Filter", "PNC", "Part Name", "Part Number", "Quantity",
"Part Filter"});

for (Part p : plist){
    String PNC = p.getPNC();
    String quantity = p.getQuantity();

    if(vFilterId.contains(p.getId())) {
        data.add(new String[] {vinId, make, 
                model, year, group, caption, capFilter, illusName,
                iFilter, PNC, p.getName(), p.getPartNumber(), 
                quantity, "NONFILTERED"});

    } else {
        data.add(new String[] {vinId, make, 
                model, year, group, caption, capFilter, illusName,
                iFilter, PNC, p.getName(), p.getPartNumber(), 
                quantity, "NONFILTERED"});

    }

}
for (int rowIndex=0; rowIndex<data.size(); rowIndex++) {
    Row row = sheet.createRow(rowIndex);
    Object[] objArr = data.get(rowIndex);
    int cellIndex = 0;
    for (Object obj : objArr) {
        Cell cell = row.createCell(cellIndex++);
        cell.setCellValue((String) obj);
    }
}

It's hard to determine without debugging your code, but from what you described it seems like your keyset variable contains only 3 entries, which is why you are outputting only 3 rows. 不调试代码就很难确定,但是从您的描述看来,您的键集变量似乎只包含3个条目,这就是为什么只输出3行的原因。 So the first thing I would do would be to display (using System.out.println) data.size() and keyset.size() to see how big they are. 因此,我要做的第一件事是显示(使用System.out.println)data.size()和keyset.size()来查看它们的大小。 I am guessing one or both are only of size 3. 我猜一个或两个都只有3号。

Did you show all the relevant code? 您是否显示了所有相关代码? I think perhaps not, because, for example, your if statement seems redundant as you output the same thing regardless of whether you execute the if code or else clause. 我想可能不是,因为例如,无论执行if代码还是else子句,输出相同的内容时,if语句似乎都是多余的。

I do agree with the first answer that there is no need to use a HashMap; 我确实同意第一个答案,即不需要使用HashMap。 an ArrayList will work just as well. ArrayList也可以正常工作。 But, as you discovered, that has nothing to do with why you are only seeing 3 rows. 但是,正如您所发现的,这与为什么只看到3行无关。

Sorry I can't be more helpful, but I think some debugging on your part with System.out.println will go a long way in discovering what the problem is. 抱歉,我帮不上什么忙,但是我认为使用System.out.println进行一些调试对发现问题所在有很大帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM