无法在Excel的单元格中打印。 JAVA
问题描述:
这是读取文件并将差异写入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中。
我失去了一些东西,请大家帮我:
答
有一个在你的逻辑
//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循环被关闭。
哦,这是该死的愚蠢的错误的逻辑,谢谢你@蒂莫西指出的错误。 – Neha