简体   繁体   中英

How to make workbook(outputstream) write all rows to an xlsx file instead of only one row?

I am still working on my file reading program but I have some problem with my code. A class 'file rating' will read all files in a directory and give them a rating. All the values are given to my 'sheetWriter class', which u can see down here. The class gets the correct values if I print the 'obj' (objects) but writing them to an excel is not working properly --> it will only write two rows: row 1 (the "Reachable for user", "Rating", "File path", and etc.) and row 2: ( reachable, 45, C://blabla, etc...). So it basically writes only one file to the xlsx. How Can I make it work so it writes all files to the xlsx?

Thanks! (I am a Java rookie)

public class SheetWriter {

private XSSFWorkbook workbook;
private XSSFSheet sheet;

//!! Maybe important to know: the values come from a for-loop
//from another class 'file rating': For (x : sourcefiles){ points=5  setPoints(points)  }    

public void SheetWriter(String file,String reachable, int points,String filePath,String fileName,String keywordMatch,String grootte, 
        String resolutie, String crea_date,String crea_mod,String last_acc, String authorString,String datetakenString,
        String manufactString,String modelString,String gps ) {

workbook = new XSSFWorkbook(); 
sheet = workbook.createSheet("Rating Files");

Map<String, Object[]> data = new TreeMap<String, Object[]>(); 
    data.put("1", new Object[]{"Reachable for user", "Rating", "File path","File name","Keyword","Size","Dimensions","Date_crea","Date_mod","Date_last_access",
    "Author","Date taken","Camera maker","Camera model","GPS-data"});

data.put(file, new Object[]{reachable, points,filePath,fileName,keywordMatch,grootte, resolutie, crea_date,crea_mod,last_acc,
             authorString,datetakenString,manufactString,modelString,gps});

Set<String> keyset = data.keySet(); 
    int rownum = 0; 
    for (String key : keyset) { 
        // this creates a new row in the sheet 
        Row row = sheet.createRow(rownum++);
        Object[] objArr = data.get(key); 
        int cellnum = 0;
        for (Object obj : objArr) { 
            // this line creates a cell in the next column of that row
            Cell cell = row.createCell(cellnum++);
            //System.out.println(obj);
            if (obj instanceof String) 
                cell.setCellValue((String)obj);
            else if (obj instanceof Integer) 
                cell.setCellValue((Integer)obj); 
        }

    }  //!! so here it's still OK. I can print all the obj (objects)

try { 
        sheet.autoSizeColumn(0);sheet.autoSizeColumn(1);sheet.autoSizeColumn(2);sheet.autoSizeColumn(3);
        sheet.autoSizeColumn(4);sheet.autoSizeColumn(5);sheet.autoSizeColumn(6);sheet.autoSizeColumn(7);
        sheet.autoSizeColumn(8);sheet.autoSizeColumn(9);sheet.autoSizeColumn(10);sheet.autoSizeColumn(11);
        sheet.autoSizeColumn(12);sheet.autoSizeColumn(13);sheet.autoSizeColumn(14);
         FileOutputStream out = new FileOutputStream(new File("C:/Users/user/Pictures/test.xlsx")); 
         workbook.write(out);  //!! Only writes one file to xlsx 
         out.close(); 
         workbook.close();
         System.out.println("test.xlsx is finished."); 

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

If you want to write several rows into a sheet and the result is only one row, then you likely have a problem with a counter variable not getting properly incremented or every iteration doing exactly the same.

This time, it's slightly different because you are writing (creating and setting a value) rows in an enhanced for loop relying on the keySet of a Map with two entries only.

That means you always write those two entries only .

The problem that not all the files are written may be caused by an issue in an outer loop. I suggest to pass an argument List<String> fileNames instead of String fileName and do the writing for all the files in this method. Otherwise check the outer loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM