Javaを使用して結果セットに異なるデータがあるにもかかわらず、Excelのすべての行に同じデータが挿入される

Vivek:

Excelにデータをエクスポートする必要があります。

クエリの結果、つまりコレクションオブジェクトと文字列配列-タイプの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();
    }
デフォルトのロケール:

同じ配列への参照(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);
}

PSは、お読みくださいミニマル、再現性の例を作成する方法どのように小さなプログラムをデバッグします

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ