简体   繁体   中英

Log file to excel sheet in C#

I've a log file similar to below and I want to convert this log file into excel sheeet.

sample log file

DateTime: 2012-12-10 20:27:10
Command: ALTER INDEX [Mypageviews] ON [WhosOff].[dbo].[PageViews] REORGANIZE WITH (LOB_COMPACTION = ON)
Comment: ObjectType: Table, IndexType: NonClustered, ImageText: N/A, NewLOB: No, FileStream: N/A, AllowPageLocks: Yes, PageCount: 7639, Fragmentation: 8.1817
Outcome: Succeeded
Duration: 00:00:41
DateTime: 2012-12-10 20:27:51

DateTime: 2012-12-10 20:27:52
Command: ALTER INDEX [MyVisitKeys] ON [WhosOff].[dbo].[Visit] REBUILD WITH (SORT_IN_TEMPDB = OFF, ONLINE = ON)
Comment: ObjectType: Table, IndexType: NonClustered, ImageText: N/A, NewLOB: No, FileStream: N/A, AllowPageLocks: Yes, PageCount: 7229, Fragmentation: 94.4667
Outcome: Succeeded
Duration: 00:00:06
DateTime: 2012-12-10 20:27:58

I want the above log text file convert into following format in excel sheet using C#. Output

StartTime             IndexName     Dbnanme Tbname
2012-12-10 20:27:10  MyPageviews    WhosOff PageViews
2012-12-10 20:27:52  MyVisitKeys    WhoOff  Visit

Can anyone tell me how i can write program to achieve above output?

You could try to implement SmartXls . There you can easily build Excel files from, for example, a DataTable.

Otherwise, try to create a file with a html-table, save it as .xls and open it in Excel. Or why not just .csv format?

I do not have a specific code example of the parsing and conversion, but it seems straightforward enough for most to grasp? Here are some references that might help:

The most simple way, in my opinion, would be convert it to CSV format . All versions of excel can recognize it well. But be carefull about date, time, decimals and all other locale specific formated values. Excel will attempt convert all that values to its internal representation, this will be successfully only if values represented in computer's locale

I reckon you'd need to parse your input into something meaningful, which would be the most complex part of your problem.

But once you have the data in some kind of structure the most basic way to do this will be to create a delimited text file (eg csv) which could then be imported into Excel.

To do this just put a comma (or whatever delimiter you choose) between each column value, and each row on a new line.

There are more elegant and complicated solutions, for example outputting an xml document in Open Office format, or accessing the Excel object model. As I say each of these is more elegant but will come with a steeper learning curve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM