[英]Using Apache POI to write in xlsx file, Getting expected answer in the last column of the file
I am learning how to write in the xlsx file using Apache poi,Now in the below code i am using 2 array. 我正在学习如何使用Apache poi在xlsx文件中编写代码,现在在下面的代码中我正在使用2数组。 1.Month 2.Logging_Hours. 1.Month.Logging_Hours。
I am using Month array to assign the column name in the first row and it work fine for me.Now what i want the another Array in my code Logging_Hours print in each column but i am not getting the expected answer by using the below code. 我正在使用Month数组在第一行中分配列名称,它对我来说很好。现在我想要在我的代码Logging_Hours中的每个列中打印另一个数组,但是我无法通过使用以下代码得到预期的答案。
For_Expected refer to the screen :"Expected_Xlsx" For_Actual refer to the screen :"Actual_Xlsx" For_Expected参见屏幕:“ Expected_Xlsx” For_Actual参见屏幕:“ Actual_Xlsx”
public class Writing_xlsx {
public static void main(String[] args) throws IOException {
Workbook wb = new XSSFWorkbook();
Sheet sheet1=wb.createSheet("SheetOne");
String [] Month={"January" , "Feb", "March","Apr"};
int [] Logging_Hours={ 7 ,5, 9,10};
int f=0;
System.out.println(Month[0]);
Row r=sheet1.createRow(f);
for(int i=0;i<4;i++){
r.createCell(i).setCellValue(Month[i]);
}
for(int c=0;c<4;c++){}
int d=0;
while(d<4){
for(int rn=1;rn<=4;rn++) {
r=sheet1.createRow(rn);
r.createCell(d).setCellValue(Logging_Hours[rn-1]);
System.out.println(Logging_Hours[rn-1]);
}
d++;
}
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
}
For_Expected refer to the screen :"Expected_Xlsx" For_Expected请参见屏幕:“ Expected_Xlsx”
For_Actual refer to the screen :"Actual_Xlsx" For_Actual参见屏幕:“ Actual_Xlsx”
Thank You in advance ,Sorry for the bad code i am just in a learning phase. 预先谢谢您,很抱歉我的代码不好,我正处于学习阶段。
You will need two for
loops (nested) to write the data, eg: 您将需要两个for
循环(嵌套)来写入数据,例如:
for (int rn = 1; rn <= 4; rn++) {
Row row = sheet1.createRow(rn);
for (int i = 0; i < 4; i++) {
row.createCell(i).setCellValue(Month[rn-1]);
}
}
Current for
loop creates only one cell
per row and writes the value into it whereas we need to write the values in all 4 cells per row. Current for
循环每行仅创建一个cell
并将其写入其中,而我们需要在每行所有4个单元格中写入值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.