Novell Home

CSV Export

From Developer Community

Tool for exporting datas to predefined format (here Comma-Separeted Values)

CsvExport.cs

using System;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;

public class CsvExport
{
	public static void Main (string[] args)
	{
		Template tpl = new Template(
			"\"%FieldA%\", \"%FieldB.Name%\", %Nono% %loop:A%, \"%Meuh%\"%endloop:A%"
		);
		
		Hashtable[] aloop = new Hashtable[]
		{
			buildHashtable("M0"),
			buildHashtable("M1"),
			buildHashtable("M2"),
			buildHashtable("M3")
		};
		
		Hashtable[] datas = new Hashtable[]
		{
			buildHashtable("Say My Name","Bibi \"one\" again", "97", aloop),
			buildHashtable("It's Eminem","Woot", "97", aloop)
		};
		
		string[] cvs = tpl.Apply(datas);
		
		foreach(string line in cvs)
		{
			Console.WriteLine(line);
		}
	}
	
	public static Hashtable buildHashtable(string a, string b, string nono, Hashtable[] atable)
	{
		Hashtable table = new Hashtable();
		table["FieldA"] = a;
		table["FieldB.Name"] = b;
		table["Nono"] = nono;
		table["A"] = atable;
		
		return table;
	}
	
	public static Hashtable buildHashtable(string meuh)
	{
		Hashtable table = new Hashtable();
		table["Meuh"] = meuh;
		
		return table;
	}
}

// ----------------------------------------------------------------------------

// Template class (very simple)
public class Template
{
	public string Text
	{
		get
		{
			return _text;
		}
		set
		{
			_text = value;
		}
	}
	
	// ------------------------------------------------------------------------
	
	public Template(string text)
	{
		Text = text;
	}
	
	// ------------------------------------------------------------------------
	
	public static string Clean(string text)
	{
		Regex re = new Regex(_sep + ".*?" + _sep);
		
		return re.Replace(text, "");
	}
	
	// Apply the values to the template
	public string[] Apply(Hashtable[] data)
	{
		string[] applied = new string[data.Length];
		
		for(int i = 0; i<applied.Length; i++)
		{
			applied[i] = Apply(_text, data[i]);
		}
		
		return applied;
	}
	
	public static string Apply(string text, Hashtable data)
	{
		foreach(DictionaryEntry entry in data)
		{
			// literal
			if(entry.Value is string)
			{
				text = text.Replace(_sep+entry.Key+_sep, ((string)entry.Value).Replace("\"","\"\"\""));
			}
			// loop
			else if(entry.Value is Hashtable[])
			{
				text = Loop(text, (string)entry.Key, (Hashtable[])entry.Value);
			}
		}
		return text;
	}
	
	public static string Loop(string text, string loopname, Hashtable[] data)
	{
		// matchs the loop (the first one)
		Regex re = new Regex(_sep + _loop + loopname + _sep + "(?<body>(.*?))"+ _sep + _endloop + loopname + _sep);
		Match m = re.Match(text);
		
		string loop = "";
		
		for(int i=0; i<data.Length; i++)
		{
			string t = m.Groups["body"].Value;
			t = Apply(t, data[i]);
			loop += t;
		}
		
		// replaces the loops (all of them with the given name)
		text = re.Replace(text, loop);
		
		return text;
	}
	
	// ------------------------------------------------------------------------
	
	private string _text;
	private static string _sep = "%";
	private static string _loop = "loop:";
	private static string _endloop = "endloop:";
}

--Yoan Blanc

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.