2012-02-11 100 views
1

我是Apache-poi的新手,並且使用3.8版的poi。在Excel中寫入值時,如果它們匹配,我檢查列名,我會在該列中寫入一些值並完成它,然後我將開始在頭上寫入。問題是,如果只寫入三個列值,最後一列值是添加或保存的,並且前兩個值不保存在列中。任何人都可以告訴哪裏出了錯。 (不好意思,萬一有什麼錯誤)Excel在編寫時缺少一些值

Code: 
      int i = 0; 
     Row row = sheet.createRow(i); 
     CellStyle cellStyle = workbook.createCellStyle(); 
     cellStyle.setFillBackgroundColor(HSSFColor.LIGHT_ORANGE.index); 
     String validate_header = null; 
     while (iterator.hasNext()) { 
      if (eiterator.hasNext()) { 
       validate_header = eiterator.next(); 
      } 
      Cell cell = row.createCell(i); 
      String col_heading = iterator.next(); 
      cell.setCellValue(col_heading); 
      if(col_heading.equalsIgnoreCase("Assigned Date")) 
      { 
       Add_value(i, col_heading, row, sheet); 
       row=sheet.getRow(0); 
       cell=row.getCell(i); 
      } 
      else if(col_heading.startsWith("Review")) 
      { 
       int count=-1; 
       int n=Col_values.get("Defect Summary").size(); 
       for (int j = 0; j < n; j++) { 
          row = sheet.createRow(count); 
        cell = row.createCell(i); 
        String s="External QC Defect ";  
        cell.setCellValue(s); 
        count++; 

       } 
       row=sheet.getRow(0); 
       cell=row.getCell(i); 

      } 

      sheet.autoSizeColumn(i); 
      i++; 

     } 

private static Sheet Add_value(int k,String name,Row row, Sheet sheet) { 

     System.out.println("Inside the add method"); 


     if(name.equalsIgnoreCase("Assigned Date")||name.equalsIgnoreCase("Reported Date")) 
     { 
     vector = Col_values.get("TargetDate"); 
     int count = 1; 

     System.out.println("IF Size of the vector " + vector.size()); 
     for (int j = 0; j < vector.size(); j++) { 

      row = sheet.createRow(count); 
      cell = row.createCell(k); 
      String s = (String) vector.get(j); 
      System.out.println(s); 
      cell.setCellValue(s); 
      count++; 

     } 
     } 
     else 
     { 
     vector = Col_values.get("Defect Summary"); 
      int count = 1; 
    System.out.println("ELSE Size of the vector " + vector.size()); 
     for (int j = 0; j < vector.size(); j++) { 

      row = sheet.createRow(count); 
      cell = row.createCell(k); 
      String s = (String) vector.get(j); 
      System.out.println(s); 
      cell.setCellValue(s); 
      count++; 
     } 
     } 
     return sheet; 

    } 

'

u能告訴了什麼問題?

+0

哪裏是代碼?我們如何知道你錯在哪裏? – 2012-02-11 04:18:41

+0

對不起。我現在添加了。 – Loga 2012-02-11 05:06:52

+0

從頭開始重寫代碼:它有太多的開放端(迭代器)和太多的本地目的(使用createRow)。遵循Java約定(即在這裏接受)也會更好:更多地使用空格,'camelCaseNames' i.o. 'Capitalized_underscored_ones'。您可以將小功能的自頂向下設計用於特定目的。 _我瞭解原因:首先用醜陋的POI進行實驗._ – 2012-02-11 09:48:47

回答

1

看起來Add_value開始從頂部創建行。因此,在第二次調用時,舊行將被刪除。

更換

  row = sheet.createRow(count); 

  row = k == 0 ? sheet.createRow(count) : sheet.getRow(count); 
+0

不,看起來在創建特定行的單元格後,計數已經增加。我需要垂直插入值不水平。 – Loga 2012-02-11 10:41:57

+0

它不起作用。從概念上說,工作表是行[],行是單元[]。您可以嘗試對其他createRows進行註釋並嘗試我的代碼。 – 2012-02-11 12:08:50

+0

我已經理解了..我一次又一次地創建一行,而不是爲了得到創建的行,這是我的問題..謝謝,很多..先生喬普 – Loga 2012-02-12 03:30:52