excel 中的每一行中都插入了相同的数据,即使结果集使用 java 具有不同的数据

Same data is getting inserted in every row in excel even though the result set has different data using java

我需要将数据导出到 excel。

我写了一个传递查询结果的方法,即集合对象和字符串数组 - 属性 LinkedHashMap<Integer, 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();
    }

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

您需要为每一行创建不同的数组对象:

//remove this line
//Object[] values = new Object[properties.length];

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

P.S。请阅读 How to create a Minimal, Reproducible Example and How to debug small programs