2017-10-20 137 views
0

這是讀取文件並將差異寫入excel的每個單元格的程序。
我面臨的一個問題是,控制檯中的輸出顯示i的遞增值,但它不會將值寫入所有索引,而只寫入最後一個索引。
我是新來的java,並嘗試對代碼進行更改,但沒有任何工作。無法在Excel的單元格中打印。 JAVA

下面是我的代碼:在控制檯上

FileInputStream fstream = new FileInputStream("C:\\Users\\Vishal\\workspace\\timestampAutomation\\bin\\com\\time\\output\\myoutput1.txt"); 
    DataInputStream in = new DataInputStream(fstream); 
    BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
    String strLine; 
    String timestamp=""; 
    String value=""; 
    int count = 0; 
    int i = 0; 
    ArrayList words=new ArrayList<String>(); 
    Pattern p = Pattern.compile("\\bSYSTEM:TIMESTAMP\\b", Pattern.CASE_INSENSITIVE); 
    while ((strLine = br.readLine()) != null) 
    { 
     String[] words1=strLine.split(","); 
     words.addAll(Arrays.asList(words1)); 
    } 

    System.out.println("WORDS LENGTH:"+words.size()); 
    for (String word : (ArrayList<String>)words) 
    { 

     Matcher m=p.matcher(word); 
     count++; 

     if (m.find()) 
     { 

      if(count<words.size()-1) 
      { 
       String tmp=(String)words.get(count); 
       String[] tmpArr=tmp.split("="); 
       timestamp=tmpArr[1]; 
       String val=(String)words.get(count+1); 
       String[] valArr=val.split("="); 
       value=valArr[1]; 
      } 

       System.out.println("Timestamp:"+timestamp+"\tValue:"+value); 
       //Splitting output into data format given 
       //Splitting output into data format given 
       String year=value.substring(0, 4); 

       String mnt=value.substring(4, 6); 

       String day=value.substring(6, 8); 

       String hr=value.substring(8, 10); 

       int hours=Integer.parseInt(hr)-2; 

       String min=value.substring(10, 12); 

       String sec=value.substring(12, 14); 

       String valueCon=year+"/"+mnt+"/"+day+" "+String.valueOf(hours)+":"+min+":"+sec; 

       long newtime= Long.parseLong(timestamp); 
       Date currentDate = new Date(newtime - TimeUnit.MINUTES.toMillis(330)); 

       String timeStamp = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(currentDate); 

       String dateStart = timeStamp; 
       String dateStop = valueCon; 

       SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss"); 

       Date d1 = null; 
       Date d2 = null; 

       d1 = format.parse(dateStart); 
       d2 = format.parse(dateStop); 

       long duration = d1.getTime() - d2.getTime(); 

       long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration); 
       System.out.println("hbase Timestamp "+timeStamp); 
       System.out.println("Value: "+valueCon); 
       System.out.println("Difference of Timestamp in Seconds:"+diffInSeconds); 

       //printing values in excel 
       Workbook wb = new HSSFWorkbook(); 
       Sheet sheet = wb.createSheet("sheet"); 
       Row row = sheet.createRow((short) 0); 



       row.createCell(i).setCellValue(diffInSeconds); 

       FileOutputStream fileOut = new FileOutputStream("C:\\Users\\Vishal\\workspace\\timestampAutomation\\bin\\com\\time\\output\\helloworl.xls"); 
       wb.write(fileOut); 
       fileOut.close(); 
       i++; 
     } 


    } 
    } 

輸出:

WORDS LENGTH:123 
Timestamp:1504767614024 Value:20170907090000 
hbase Timestamp 2017/09/07 07:00:14 
Value: 2017/09/07 7:00:00 
Difference of Timestamp in Seconds:14 
current value of i 0 Timestamp:1504767614025 Value:20170907090000 
hbase Timestamp 2017/09/07 07:00:14 
Value: 2017/09/07 7:00:00 
Difference of Timestamp in Seconds:14 
current value of i 1 Timestamp:1504767614029 Value:20170907090000 
hbase Timestamp 2017/09/07 07:00:14 
Value: 2017/09/07 7:00:00 
Difference of Timestamp in Seconds:14 
current value of i 2 Timestamp:1504767614030 Value:20170907090000 
hbase Timestamp 2017/09/07 07:00:14 
Value: 2017/09/07 7:00:00 
Difference of Timestamp in Seconds:14 
current value of i 3 

不過,這並不在所有四個指標進行打印(細胞)在Excel中。
我失去了一些東西,請大家幫我:

output in excel

回答

1

有一個在你的邏輯

//printing values in excel 
    Workbook wb = new HSSFWorkbook(); 
    Sheet sheet = wb.createSheet("sheet"); 
    Row row = sheet.createRow((short) 0); 
    row.createCell(i).setCellValue(diffInSeconds); 

所以,你正在創建循環工作簿,這意味着你正在創建N個問題然後在這些工作簿的每個工作簿中創建一個工作表,然後在每個工作表中在第0個索引處分配一行,然後在此行中在第i個索引處創建一個單元,因此在這些工作簿中的每個工作簿中,工作簿的 - >工作表 - >單元格(i)加1.這是沒有意義的。 典型的Excel工作表應該看起來像1個工作簿,1個或多個工作表,然後每個工作表包含一個或多個行,每一行包含一個或多個單元。

移動這是你的for循環

Workbook wb = new HSSFWorkbook(); 
    Sheet sheet = wb.createSheet("sheet"); 

在for循環中根據自己的需要創建行之前,從給定的代碼,它看起來像你只需要1行。然後對於該行,從給定的代碼4單元中創建您希望的單元格。 在for循環中創建這些單元格,然後在完成時將輸出寫入文件。

移動這個

FileOutputStream fileOut = new FileOutputStream("C:\\Users\\Vishal\\workspace\\timestampAutomation\\bin\\com\\time\\output\\helloworl.xls"); 
       wb.write(fileOut); 
       fileOut.close(); 

後,所有的for循環被關閉。

+0

哦,這是該死的愚蠢的錯誤的邏輯,謝謝你@蒂莫西指出的錯誤。 – Neha