2016-09-20 169 views
0

我想在完整的data.put上創建循環。我怎樣才能做到這一點?for循環到excel

如何創建循環?

data.put("01",new Object[] { a[0],a[1],a[2],a[3],a[4], ... a[240],a[241],a[242],a[243] }); 
    data.put("02",new Object[] { a[244],a[245],a[246],a[247],a[248], ... a[484],a[485],a[486],a[487] }); 
    data.put("03",new Object[] { a[488],a[489],a[490],a[491],a[492], ... a[728],a[729],a[730],a[731] }); 

行= 244 柱= 3

public void ExcelSave() throws IOException { 

    workbook = new XSSFWorkbook(); 

    XSSFSheet sheet = workbook.createSheet("Employee Data"); 
    Map<String, Object[]> data = new TreeMap<String, Object[]>(); 

    String[] a = DataFrom.split("-"); 

     data.put("01",new Object[] { a[0],a[1],a[2],a[3],a[4], ... a[240],a[241],a[242],a[243] }); 
     data.put("02",new Object[] { a[244],a[245],a[246],a[247],a[248], ... a[484],a[485],a[486],a[487] }); 
     data.put("03",new Object[] { a[488],a[489],a[490],a[491],a[492], ... a[728],a[729],a[730],a[731] }); 

/////////////////////////// ////////////////////////////////////////////////// //////////////////////////////

Set<String> keyset = data.keySet(); 

int rownum = 0; 
    for (String key : keyset) { 

     Row row = sheet.createRow(rownum++); 

     Object[] objArr = data.get(key); 

     int cellnum = 0; 

     for (Object obj : objArr) { 
      Cell cell = row.createCell(cellnum++); 
      if (obj instanceof String) { 
       cell.setCellValue((String) obj); 
      } else if (obj instanceof Integer) { 
       cell.setCellValue((Integer) obj); 
      } 
     } 
    } 

///////////// ////////////////////////////////////////////////// ////////////////////////////////////////////

try { 

     FileOutputStream out = new FileOutputStream(new File("ExcelSaves.xlsx")); 
     workbook.write(out); 
     out.close(); 
     System.out.println("--> Excel Save..."); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+0

你的意思_如何創建loops_?你可以參加[遊覽]並首先查看[問] – Prisoner

回答

0

你可以嘗試嵌套循環,它是這樣的:

final int SIZE = 244; 
for (int column = 0; column < 3; column++) { 
    Object[] arr = new Object[SIZE]; 
    int currentColIndex = column * SIZE; 
    for (int row = 0; row < arr.length; i++) 
     arr[row] = a[currentColIndex + row]; 
    data.put("0" + (column+1), arr); 
} 
0

相信Arrays.copyOfRange將是幫助在這裏。

int column = 244; 
int row = 3; 
Map<String, Object[]> data = new TreeMap<String, Object[]>(); 
for(int i = 0; i++; i < column) { 
    String key = String.format("%02d",i); 
    int from = i * column; 
    int to = from + column; 
    data.put(key , Arrays.copyOfRange(arr, from, to)); 
}