Unable to write into Excel using Apache POI

Multi tool use
Multi tool use


Unable to write into Excel using Apache POI



My goal is to write data to excel depends on the length of a 2D array. Based on String types, the length is 2. Thus, I would like my excel's output to be


String types



However, this was what I get



Below are my codes.


String types = { {"type1", "value1"}, {"type2", "value2"}, };

try {
FileInputStream fis= new FileInputStream(new File(excelFile));
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet worksheet = wb.getSheetAt(0);

for (int i = 0; i <4 ; i++) { // this will loop 4 times

for (int a = 0; a < types.length ; a++) {
inputs[a] = "word";
XSSFRow row = worksheet.createRow(i);
row.createCell(a).setCellValue(inputs[a]);
}
}
..



Any help with be appreciated :)





Your program logic is wrong. You should only create a new row for each i and not for each a too. So do moving the XSSFRow row = worksheet.createRow(i); out of the for a loop. for (int i = 0; i <4 ; i++) { XSSFRow row = worksheet.createRow(i); for (int a = 0; a < types.length ; a++) { ... .
– Axel Richter
Jul 3 at 8:48



XSSFRow row = worksheet.createRow(i);


for (int i = 0; i <4 ; i++) { XSSFRow row = worksheet.createRow(i); for (int a = 0; a < types.length ; a++) { ...





@AxelRichter it works! Do u want to put your comment as an answer? So I can tick it?
– Emma E
Jul 3 at 8:54





No, it is so obvious that it is not worth an answer in my opinion. But you can answering your own question now. In the answer you should describing your new gained knowledge about why it had failed in your old code and why it works now in your new code.
– Axel Richter
Jul 3 at 9:00









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

49zb,e hn
dW8NcPwCxAXpNKv,w ExdRnrgys,zVw9

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications