如何使在Excel中的Excel表

我想使Java应用程序

  1. 从保存在文件夹中的文件列表中读取。
  2. 将每个文件的名称发送到命令行。
  3. 从cmd输出并保存在数组列表中
  4. 使哈希映射保存在其中的所有数组列表,例如哈希映射(键,值)
  5. 写excel表单来为文件夹中的每个文件写入所有权限

问题是只有一个logging出现在Excel表和哈希映射只保存最后一个文件为什么?任何一个帮助我继续这个代码? 写在excel表中的所有logging我有12个文件

我的代码:

public class CreateExcellSheet { public String line; public Map < String, Object[] > appinfo = new TreeMap < String, Object[] >(); public int counter=0; public ArrayList<String> permissions=new ArrayList<String>(); public Set<String> permissionsSet=new HashSet<String>(); public ArrayList<String> AppPermissions=new ArrayList<String>(); public String AppName; public HashMap<Integer,ArrayList<String>> singleAppPermissions=new HashMap<Integer,ArrayList<String>>(); public int key=0; public CreateExcellSheet() { } public static void main(String[] args) throws Exception { CreateExcellSheet createExcelSheet=new CreateExcellSheet(); final File folder = new File("GooglePlaySamples"); for ( File fileEntry : folder.listFiles()) { createExcelSheet.AppPermissions.clear(); createExcelSheet.AppName = fileEntry.getName(); ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "aapt d permissions GooglePlaySamples/"+createExcelSheet.AppName); builder.redirectErrorStream(true); Process p = builder.start(); BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); createExcelSheet.key++; while (true) { createExcelSheet.line = r.readLine(); if (createExcelSheet.line == null) { break; } else if (createExcelSheet.line.startsWith("package:")){continue;} else if(createExcelSheet.line.startsWith("W/asset")){continue;} else if(createExcelSheet.line.startsWith("ERROR")){continue;} // System.out.println(createExcelSheet.line); createExcelSheet.permissions.add(createExcelSheet.line); createExcelSheet.AppPermissions.add(createExcelSheet.line); createExcelSheet.counter++; } createExcelSheet.singleAppPermissions.put(createExcelSheet.key, createExcelSheet.AppPermissions); } System.out.println(createExcelSheet.key+""+createExcelSheet.singleAppPermissions.get(9)); createExcelSheet.permissionsSet.addAll(createExcelSheet.permissions); createExcelSheet.permissions.clear(); createExcelSheet.permissions.addAll(createExcelSheet.permissionsSet); //Create blank workbook XSSFWorkbook workbook = new XSSFWorkbook(); //Create a blank sheet XSSFSheet spreadsheet = workbook.createSheet(" Employee Info "); //Create row object for (int i=0;i<createExcelSheet.permissions.size();i++){ createExcelSheet.appinfo.put( "1",new Object[] {createExcelSheet.permissions.get(i)}); } XSSFRow row,newrow; //Iterate over data and write to sheet Set < String > keyid = createExcelSheet.appinfo.keySet(); int rowid = 0; newrow = spreadsheet.createRow(rowid++); for (String key : keyid) { row = spreadsheet.createRow(0); Object [] objectArr = createExcelSheet.appinfo.get(key); for (Object obj : objectArr) { // Cell cell = row.createCell(cellid++); for (int i=0;i<createExcelSheet.permissions.size();i++){ row.createCell(i).setCellValue(createExcelSheet.permissions.get(i)); } } for (Object obj : objectArr) { newrow=spreadsheet.createRow(rowid++); for(int i=0;i<createExcelSheet.permissions.size();i++){ for(int j=0;j<createExcelSheet.AppPermissions.size();j++){ if(createExcelSheet.permissions.get(i).equals(createExcelSheet.AppPermissions.get(j))){ //System.out.println(createExcelSheet.permissions.get(i)); newrow.createCell(i).setCellValue(1); } } } //Write the workbook in file system FileOutputStream out = new FileOutputStream( new File("Writesheet.xlsx")); workbook.write(out); out.close(); System.out.println( "Writesheet.xlsx written successfully" ); } }}} 

问题是你使用相同的键(即“1”)所有你的HashMap条目。

你可能想要:

 for (int i=0;i<createExcelSheet.permissions.size();i++){ createExcelSheet.appinfo.put("" + (i+1),new Object[] {createExcelSheet.permissions.get(i)}); } 

因为当你这样做时:

 map.put("1", someObject); map.put("1", anotherObject); 

键“1”的input被覆盖