gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:30:24 25 4
gpt4 key购买 nike

我需要将数据导出到Excel。

我编写了一个传递查询结果的方法,即集合对象和字符串数组 - attrs,其类型为 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();
}

最佳答案

您将对同一数组()的引用放在一个 over 上。因此,所有 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);
}

附注请阅读How to create a Minimal, Reproducible ExampleHow to debug small programs

关于java - 即使使用 java 结果集具有不同的数据,相同的数据也会插入到 Excel 的每一行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59907293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com