gpt4 book ai didi

java - 每次在JAVA中调用函数时在Excel中添加行

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:12 25 4
gpt4 key购买 nike

我正在使用此代码在 Excel 工作表末尾添加行。每次调用函数时。初始化时的 last=0

connectionTime, mobility, angleofArrival, sender and peer

是一些变量,其值随着函数调用而改变。当调用函数时,我想在每一行中保存值。

public void excelWriter(DTNHost sender, DTNHost peer){
String fileName = "Results.xls";

try {

File file = new File(fileName);
FileInputStream fin = null;
HSSFWorkbook workbook = null;
HSSFSheet worksheet = null;
FileOutputStream fout = null;
POIFSFileSystem lPOIfs = null;
if (file.exists()) {
try{
fout = new FileOutputStream(fileName, true);
fin = new FileInputStream(fileName);
lPOIfs = new POIFSFileSystem(fin);
workbook = new HSSFWorkbook(lPOIfs);
worksheet = workbook.getSheet("POI Worksheet");

for (int i=0; i<workbook.getNumberOfSheets(); i++) {
System.out.println( workbook.getSheetName(i) );
}
HSSFSheet sheet = workbook.getSheetAt(0);
last = sheet.getLastRowNum();
}catch (IOException e) {
e.printStackTrace();
}catch (NullPointerException e){
e.printStackTrace();
}
} else {
//create new file
try{
fout = new FileOutputStream(file);
}catch (IOException e) {
e.printStackTrace();
}
workbook = new HSSFWorkbook();
worksheet = workbook.createSheet("POI Worksheet");
}

if(last != 0){
last = worksheet.getLastRowNum();
}else{

last = worksheet.getLastRowNum()+1;
System.out.println(last);

}

HSSFRow row = worksheet.createRow(last++);
HSSFCell cellA1 = row.createCell((int)0);
cellA1.setCellValue(sender+" and "+peer);
HSSFCell cellB1 = row.createCell((int) 1);
cellB1.setCellValue(mobility);
HSSFCell cellC1 = row.createCell((int) 2);
cellC1.setCellValue(angleOfArival);
HSSFCell cellD1 = row.createCell((int) 3);
cellD1.setCellValue(connectionTime);
System.out.println("Data written");
workbook.write(fout);
fout.flush();

fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}




////End
}

但它只添加一行(仅第一次)。我已经对其进行了调试。每次调用函数时 last 值都会增加,但不会每次都添加行。谁能告诉我我在这里做错了什么。

最佳答案

public void excelWriter(DTNHost sender, DTNHost peer) {
String fileName = "Results.xls";
int last = 0;
try {

File file = new File(fileName);
FileInputStream fin = null;
HSSFWorkbook workbook = null;
HSSFSheet worksheet = null;
FileOutputStream fout = null;
POIFSFileSystem lPOIfs = null;
if (file.exists()) {
try {

fin = new FileInputStream(fileName);
lPOIfs = new POIFSFileSystem(fin);
workbook = new HSSFWorkbook(lPOIfs);
worksheet = workbook.getSheet("POI Worksheet");
last = worksheet.getLastRowNum() + 1;

fout = new FileOutputStream(fileName);

} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
} else {
// create new file
try {

fout = new FileOutputStream(file);
} catch (IOException e) {
e.printStackTrace();
}
workbook = new HSSFWorkbook();
worksheet = workbook.createSheet("POI Worksheet");

}
HSSFRow row = worksheet.createRow(last);
HSSFCell cellA1 = row.createCell((int) 0);
cellA1.setCellValue(sender + " and " + peer);
HSSFCell cellB1 = row.createCell((int) 1);
cellB1.setCellValue("mobility");
HSSFCell cellC1 = row.createCell((int) 2);
cellC1.setCellValue("angleOfArival");
HSSFCell cellD1 = row.createCell((int) 3);
cellD1.setCellValue("connectionTime");
System.out.println("Data written");
workbook.write(fout);
fout.flush();
fout.close();

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

//// End
}

上述方法有效,一旦加载工作表,OutputStream就会打开,已删除行HSSFSheet Sheet = workbook.getSheetAt(0);,因为它是多余的,您已经拥有可以从中获取lastRowNum的工作表实例。语句HSSFRow row = worksheet.createRow(last++);使用后增量运算符,因此它将继续写入同一行,“last”变量的值将递增仅在该语句执行后。

关于java - 每次在JAVA中调用函数时在Excel中添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36007625/

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