Jump to content

  • Log In with Google      Sign In   
  • Create Account


C# Exporting text to CSV(or Excel)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 link161   Members   -  Reputation: 125

Like
0Likes
Like

Posted 05 April 2012 - 04:19 PM

Hey everybody,

I was hoping to get a little help on a data exporter here. I'm reading files from a delimited txt log and need to export it to a CSV file. The problem I'm having is how to get the data I need from the log. The txt files resemble this:

[Date] - [some system info] - [some system state]
[Date] - [Athenticate] - [some system state]
[Date] - [some system info] - [some system state]

I need to only grab the Athenticates in the file and export those to a CSV. Sadly I'm not to fluent in C#. What whould be the best way to go about this? I have no problem reading the file and I shouldnt have any problem exporting but how do I handle only collecting the ata I need from the file? A List maybe? or a 2-D array?

Sponsor:

#2 Telastyn   Crossbones+   -  Reputation: 3692

Like
0Likes
Like

Posted 05 April 2012 - 04:52 PM

How big is the file?

System.IO.File has ReadAllLines and WriteAllLines. Super easy if your file isn't huge.

#3 link161   Members   -  Reputation: 125

Like
0Likes
Like

Posted 05 April 2012 - 05:01 PM

They can vary. Some as small as a few KB, others can get kinda large. I need to find a way to check that 2nd part of the file to see if it will be added or not.

#4 ApochPiQ   Moderators   -  Reputation: 12444

Like
0Likes
Like

Posted 05 April 2012 - 05:04 PM

Screw writing your own tool. Just use sed or something similar.



I'm dead serious. It was literally designed to do this kind of thing.

#5 Adam Elston-Jones   Members   -  Reputation: 103

Like
0Likes
Like

Posted 05 April 2012 - 05:06 PM

You need to define a grammar for both your log file input and your csv output. This might be over engineering the problem though. You could also define a regular expression and use matching to pull out the desired data.

#6 link161   Members   -  Reputation: 125

Like
0Likes
Like

Posted 05 April 2012 - 05:38 PM

I think I may of found a way, I checked out those links Adam, and I needed something like Matching. Though again, not very fluent in C# still, I kinda implemented it my own way. Heres what I have to know that it works, I have 2 lists, one that delimits part by part and if the part matches with the Authenticate info the 2nd list stores the line. I applied these to listboxes to test, but now I just need to delimit the line to stick in colums and rows in the CSV and it should work Posted Image


public  void ReadTextFile(string fileName)
		{
			List<string> toAdd = new List<string>();
			List<string> sepList = new List<string>();
			string line;
		  
			// Read the file and display it line by line.
			using (StreamReader file = new StreamReader(fileName))
			{
				while ((line = file.ReadLine()) != null)
				{

					char[] delimiters = new char[] { '-',',' };
					string[] parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
				  
				  
					for (int i = 0; i < parts.Length; i++)
					{
						Console.WriteLine(parts[i]);
					  
						if (parts[i].ToString() == " [Authenticate info]")
						{
							sepList.Add(parts[i]);
							toAdd.Add(line);
						  
						}
					  
					}
				}
				listBox1.DataSource = sepList;
				listBox2.DataSource = toAdd;
				file.Close();
			}





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS