这是用于读取文件并将差异写入excel的每个单元格的程序
我遇到一个问题,控制台中的输出显示i的递增值,但它没有写入所有索引的值,而只写入最后一个索引。
我是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中的所有四个索引(单元格)上打印 我错过了什么,请帮助我:
答案 0 :(得分: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个索引处的单元格,因此在这些工作簿中的每个工作簿中,单元格都是在以前的工作簿中创建的 - &gt;工作表 - &gt;单元格(i)加1.这没有任何意义。 典型的Excel工作表应该看起来像1个工作簿,1个或更多工作表,然后每个工作表包含1行或更多行,每行包含一个或多个单元格。
在for-loop
之前移动它 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循环后。