如何在 Java 日志记录中将数据记录到 Excel 中


public static void main(String[] args) {  
Logger logger = Logger.getLogger("MyLog");  
FileHandler fh;  
try {  
// This block configure the logger with handler and formatter  
fh = new FileHandler("C:/temp/test/MyLogFile.log");  
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();  
fh.setFormatter(formatter);  
// the following statement is used to log any messages  
logger.info("My first log");  
} catch (SecurityException e) {  
e.printStackTrace();  
} catch (IOException e) {  
e.printStackTrace();  
}  
logger.info("Test1 : Hi How r u?");  
}

MyLogFile.log时产生输出

Apr 2, 2013 9:57:08 AM testing.MyLogger main  
INFO: My first log  
Apr 2, 2013 9:57:08 AM testing.MyLogger main  
INFO: Test : Hi How r u?

这里MyLogFile.log是文本文件,我想创建excel文件用于输出而不是文本,请对此提出一些建议? 我想按列/按行输出 excel,如下所示 -

column1   column2
Test      Hi How r u ? ......

我正在使用Apache POI从Excel输入中读取,使用以下方法创建日志 - 导入java.util.logging.FileHandler; import java.util.logging.Logger; import java.util.logging.SimpleFormatter; 并希望在Excel而不是文本文件中打印日志输出。 CSV 输出文件作为除日志文件之外的测试用例执行报告 除日志文件外的 Excel 报告文件。

实际上,您需要特定的Handler写入Excel文件,而不是简单的FileHandler

好吧,我还没有找到任何现有的,但是您可以非常轻松地创建自己的POI,因为您已经在项目中使用了POI。

最新更新