gpt4 book ai didi

java - Excel 数据仅写入第一行 - java

转载 作者:行者123 更新时间:2023-12-02 12:12:36 25 4
gpt4 key购买 nike

我已经编写了使用 java 将数据写入 Excel 的代码,但是,它不会增加 roe 值,导致第 1 行本身的数据被覆盖。我知道问题出在 rownum 计数增量。

import java.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import java.io.*;

public class ExcelWriter {
static int rownum;
@Test
public void ExcelWriters(Integer i, String CableName, String Count,
String EndCableId, String FirstCableId) {
rownum = i;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("WhileTrue");
Map < Integer, Object[] > data = new HashMap < Integer, Object[] > (); {
data.put(i, new Object[] {
"Sr.no",
"Cable",
"Total Count",
"EndCableId",
"FirstCableId"
});
data.put(i, new Object[] {
i,
CableName,
Count,
EndCableId,
FirstCableId
});

Set < Integer > keyset = data.keySet();

for (Integer key: keyset) {
HSSFRow row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellnum = 0;
for (Object obj: objArr) {
HSSFCell cell = row.createCell(cellnum++);

if (obj instanceof Date)
cell.setCellValue((Date) obj);
else if (obj instanceof Boolean)
cell.setCellValue((Boolean) obj);
else if (obj instanceof String)
cell.setCellValue((String) obj);
else if (obj instanceof Double)
cell.setCellValue((Double) obj);
}

}

try {
FileOutputStream outputStream =

new FileOutputStream((new File("FilePath")));
workbook.write(outputStream);
outputStream.close();
System.out.println("Excel written Successfully");

} catch (IOException e) {
e.printStackTrace();
}
}
}

}

最佳答案

通过使用 data.put(i,new Object[]{...} 两次,映射仅保存给定键 i 的最后一个值,(即)您覆盖列的名称。

将第一个 put(i,YOUR_COL_NAMES); 更改为 put(0,YOUR_COL_NAMES);

关于java - Excel 数据仅写入第一行 - java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46424344/

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