简体   繁体   English

如何使工作簿(输出流)将所有行写入 xlsx 文件而不是仅一行?

[英]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. class 'file rating' 将读取目录中的所有文件并对其进行评级。 All the values are given to my 'sheetWriter class', which u can see down here.所有的值都给了我的“sheetWriter 类”,你可以在这里看到。 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...).如果我打印“obj”(对象)但将它们写入 excel 无法正常工作,则 class 会获得正确的值 -> 它只会写入两行:第 1 行(“用户可访问”、“评级”、 “文件路径”等)和第 2 行:(可达,45,C://blabla 等)。 So it basically writes only one file to the xlsx.所以它基本上只将一个文件写入 xlsx。 How Can I make it work so it writes all files to the xlsx?我怎样才能使它工作,以便将所有文件写入 xlsx?

Thanks!谢谢! (I am a Java rookie) (我是Java菜鸟)

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.这一次,它略有不同,因为您在增强for循环中编写(创建和设置值)行,该循环依赖于仅具有两个条目的MapkeySet

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.我建议传递一个参数List<String> fileNames而不是String fileName并为此方法中的所有文件进行写入。 Otherwise check the outer loop.否则检查外循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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