即使使用Java,结果集具有不同的数据,Excel的每一行也会插入相同的数据

问题描述 投票:0回答:1

我需要将数据导出到excel。

我已经编写了一种传递查询结果的方法,即集合对象和String数组-attrs,其类型为LinkedHashMap<Integer, Object[]>。该方法如下所示。每个条目的结果存储在Object数组中,并插入到键中。现在返回的结果在resultSet中,并通过另一个方法processReport(resultSet)

LinkedHashMap<Integer, Object[]> resultSet = dqlHelper.insertAttrValues(collection,
        attrs);

processReport(resultSet);

public LinkedHashMap<Integer, Object[]> insertAttrValues(IDfCollection collection,
                                                         String[] properties) throws DfException {

    if (collection == null || properties == null) {
        throw new MissingParameterException("collection and properties");
    }

    LinkedHashMap<Integer, Object[]> map = new LinkedHashMap<>();
    Object[] values = new Object[properties.length];

    int i = 0;
    while (collection.next()) {
        for (int x = 0; x < properties.length; x++) {
            values[x] = collection.getString(properties[x]);
        }
        map.put(i++, values);
    }
    return map;
}

方法processReport(resultSet);此方法采用resultSet并将数据写入excel。但是在每一行中,我得到的数据与方法中的注释所示的数据相同。但是resultSet的结果具有所有数据。不知道在哪里,我做错了。非常感谢您的帮助。

private void processReport(LinkedHashMap<Integer, Object[]> resultSet)
            throws IOException {
        // Create a Workbook - for xlsx
        Workbook workbook = new XSSFWorkbook();

         /* CreationHelper helps us create instances of various things like DataFormat,
           Hyperlink, RichTextString etc, in a format (HSSF, XSSF) independent way
        */
        workbook.getCreationHelper();

        // Create a Sheet
        Sheet sheet = workbook.createSheet("Report");

        // Create a Font for styling header cells
        Font headerFont = workbook.createFont();
        headerFont.setBold(false);
        headerFont.setFontHeightInPoints((short) 12);
        headerFont.setColor(IndexedColors.GREEN.getIndex());

        // Create a CellStyle with the font
        CellStyle headerCellStyle  = workbook.createCellStyle();
        headerCellStyle.setFont(headerFont);

        // Create a Row
        Row headerRow = sheet.createRow(0);
        // Create cells
        for (int i = 0; i < attrs.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(redigo_attrs[i]);
            cell.setCellStyle(headerCellStyle);
        }

        // Facing problem in the following code.....
        // same entry in every row in excel sheet
        // 0902b69881bd01a5     NLP_031124  VPS17_BR-NPI-EN-GILENYA_-06.06.19_MS_(clean)    Brazil  Gilenya
        // 0902b69881bd01a5     NLP_031124  VPS17_BR-NPI-EN-GILENYA_-06.06.19_MS_(clean)    Brazil  Gilenya

        Set<Integer> key_set = resultSet.keySet();
        sheet = workbook.getSheetAt(0);
        for (Integer key: key_set) {
            int last_row = sheet.getLastRowNum();
            row = sheet.createRow(++last_row);
            int cellNum = 0;
            Object[] valuesObject = resultSet.get(key);
            for (Object value: valuesObject) {
                row.createCell(cellNum++).setCellValue(value.toString());
            }
        }

        // Resize all columns to fit the content size
        for (int i = 0; i < attrs.length; i++) {
            sheet.autoSizeColumn(i);
        }
        // Write the output to the file
        FileOutputStream fileOutputStream = new FileOutputStream(XLSX_FILE_PATH);
        workbook.write(fileOutputStream);
        // close the file
        fileOutputStream.close();
        // close the workbook
        workbook.close();
    }
java set apache-poi linkedhashmap
1个回答
2
投票

您正在将引用放在同一数组(values)上。结果,所有LinkedHashMap值都是对包含结果集中最后一行的同一数组的引用。

© www.soinside.com 2019 - 2024. All rights reserved.