gpt4 book ai didi

java - 无法在 Excel 单元格中打印。 java

转载 作者:行者123 更新时间:2023-12-02 12:06:44 25 4
gpt4 key购买 nike

这是读取文件并将差异写入 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 中的所有四个索引(单元格)上。
我错过了什么吗,请帮助我:

output in 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 个或多个工作表,然后每个工作表包含 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 循环都关闭后。

关于java - 无法在 Excel 单元格中打印。 java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46857332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com