Converting a datatable to csv, xml and html by using Extensions
Introduction
Very often I use extensions to simplify casting of different datatypes, something like string to boolean or string to datetime. So why not create an extension to cast a datatable to csv...
Background
Extensions are very powerfull and make coding much more easy. In mostly every project I implement my own extension library. So spent some time to create that library carefully and try to handle all conversion possibilities.
Using the code
The Extension Class is quite simple to realize... just refer to the thousands of samples you can find in the web:
public static class Extensions
{
//ToString() Extensions:
public static string ToCsvString(this DataTable dt)
{
return DataTableFunctions.ToCsv(dt);
}
public static string ToHtmlString(this DataTable dt)
{
return DataTableFunctions.ToHtml(dt);
}
public static string ToXmlString(this DataTable dt)
{
return DataTableFunctions.ToXml(dt);
}
//ToFile() Extensions
public static void ToXmlFile(this DataTable dt)
{
CommonTools.WriteFile(CommonTools.SaveFileName("XML File", "xml (*.xml)|*.xml|All Files (*.*)|*.*"), DataTableFunctions.ToXml(dt));
}
public static void ToCsvFile(this DataTable dt)
{
CommonTools.WriteFile(CommonTools.SaveFileName("Csv File", "csv (*.csv)|*.csv|All Files (*.*)|*.*"), DataTableFunctions.ToCsv(dt));
}
public static void ToHtmlFile(this DataTable dt)
{
CommonTools.WriteFile(CommonTools.SaveFileName("Html File", "html (*.html)|*.html|All Files (*.*)|*.*"), DataTableFunctions.ToHtml(dt));
}
//ToClipBoard() Extensions
public static void ToXmlClipboard(this DataTable dt)
{
Clipboard.SetText(DataTableFunctions.ToXml(dt));
}
public static void ToCsvClipboard(this DataTable dt)
{
Clipboard.SetText(DataTableFunctions.ToCsv(dt));
}
public static void ToHtmlClipboard(this DataTable dt)
{
Clipboard.SetText(DataTableFunctions.ToHtml(dt));
}
}
To use the convertion u will need the conversion classes itself. This should be only a sample... no error handling at all:
private static class DataTableFunctions
{
public static String ToCsv(DataTable dt)
{
var sb = new StringBuilder();
//Add Header Header
for (var x = 0; x < dt.Columns.Count; x++)
{
if (x != 0) sb.Append(";");
sb.Append(dt.Columns[x].ColumnName);
}
sb.AppendLine();
//Add Rows
foreach (DataRow row in dt.Rows)
{
for (var x = 0; x < dt.Columns.Count; x++)
{
if (x != 0) sb.Append(";");
sb.Append(row[dt.Columns[x]]);
}
sb.AppendLine();
}
return sb.ToString();
}
public static string ToXml(DataTable dt)
{
var writer = new StringWriter();
var name = dt.TableName;
if (name == string.Empty)
{
dt.TableName = "XMLTABLE";
}
dt.WriteXml(writer, true);
dt.TableName = name;
return writer.ToString();
}
public static string ToHtml(DataTable dt)
{
if (dt == null)
{
throw new ArgumentNullException("dt");
}
var builder = new StringBuilder();
builder.Append("");
builder.Append("");
builder.Append("");
builder.Append("Page-");
builder.Append(Guid.NewGuid().ToString());
builder.Append(" ");
builder.Append("");
builder.Append("");
builder.Append(" builder.Append("style='border: solid 1px Silver; font-size: x-small;'>");
");
builder.Append("");
builder.Append("");
return builder.ToString();
}
So the whole class could look like that:
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace Extensions
{
public static class Extensions
{
//ToString() Extensions:
public static string ToCsvString(this DataTable dt)
{
return DataTableFunctions.ToCsv(dt);
}
public static string ToHtmlString(this DataTable dt)
{
return DataTableFunctions.ToHtml(dt);
}
public static string ToXmlString(this DataTable dt)
{
return DataTableFunctions.ToXml(dt);
}
//ToFile() Extensions
public static void ToXmlFile(this DataTable dt)
{
CommonTools.WriteFile(CommonTools.SaveFileName("XML File", "xml (*.xml)|*.xml|All Files (*.*)|*.*"), DataTableFunctions.ToXml(dt));
}
public static void ToCsvFile(this DataTable dt)
{
CommonTools.WriteFile(CommonTools.SaveFileName("Csv File", "csv (*.csv)|*.csv|All Files (*.*)|*.*"), DataTableFunctions.ToCsv(dt));
}
public static void ToHtmlFile(this DataTable dt)
{
CommonTools.WriteFile(CommonTools.SaveFileName("Html File", "html (*.html)|*.html|All Files (*.*)|*.*"), DataTableFunctions.ToHtml(dt));
}
//ToClipBoard() Extensions
public static void ToXmlClipboard(this DataTable dt)
{
Clipboard.SetText(DataTableFunctions.ToXml(dt));
}
public static void ToCsvClipboard(this DataTable dt)
{
Clipboard.SetText(DataTableFunctions.ToCsv(dt));
}
public static void ToHtmlClipboard(this DataTable dt)
{
Clipboard.SetText(DataTableFunctions.ToHtml(dt));
}
///
/// Common Tool Class
///
private static class CommonTools
{
public static string SaveFileName(string title, string filter)
{
var fd = new SaveFileDialog();
fd.Title = title;
fd.Filter = filter;
fd.ShowDialog();
return fd.FileName;
}
public static void WriteFile(string filename, string content)
{
var sr = new StreamWriter(filename);
sr.Write(content);
sr.Flush();
sr.Close();
}
}
///
/// DataTable Converter Class
///
private static class DataTableFunctions
{
public static String ToCsv(DataTable dt)
{
var sb = new StringBuilder();
//Add Header Header
for (var x = 0; x < dt.Columns.Count; x++)
{
if (x != 0) sb.Append(";");
sb.Append(dt.Columns[x].ColumnName);
}
sb.AppendLine();
//Add Rows
foreach (DataRow row in dt.Rows)
{
for (var x = 0; x < dt.Columns.Count; x++)
{
if (x != 0) sb.Append(";");
sb.Append(row[dt.Columns[x]]);
}
sb.AppendLine();
}
return sb.ToString();
}
public static string ToXml(DataTable dt)
{
var writer = new StringWriter();
var name = dt.TableName;
if (name == string.Empty)
{
dt.TableName = "XMLTABLE";
}
dt.WriteXml(writer, true);
dt.TableName = name;
return writer.ToString();
}
public static string ToHtml(DataTable dt)
{
if (dt == null)
{
throw new ArgumentNullException("dt");
}
var builder = new StringBuilder();
builder.Append("");
builder.Append("");
builder.Append("");
builder.Append("Page-");
builder.Append(Guid.NewGuid().ToString());
builder.Append(" ");
builder.Append("");
builder.Append("");
builder.Append(" builder.Append("style='border: solid 1px Silver; font-size: x-small;'>");
");
builder.Append("");
builder.Append("");
return builder.ToString();
}
}
}
}
To use the Extensions simpley try this in ur app:
var x = myDataTable.ToCsvString();
or to save the datatable to a xml file:
myDataTable.ToXmlFile()
Very often I use extensions to simplify casting of different datatypes, something like string to boolean or string to datetime. So why not create an extension to cast a datatable to csv...
Background
Extensions are very powerfull and make coding much more easy. In mostly every project I implement my own extension library. So spent some time to create that library carefully and try to handle all conversion possibilities.
Using the code
The Extension Class is quite simple to realize... just refer to the thousands of samples you can find in the web:
public static class Extensions
{
//ToString() Extensions:
public static string ToCsvString(this DataTable dt)
{
return DataTableFunctions.ToCsv(dt);
}
public static string ToHtmlString(this DataTable dt)
{
return DataTableFunctions.ToHtml(dt);
}
public static string ToXmlString(this DataTable dt)
{
return DataTableFunctions.ToXml(dt);
}
//ToFile() Extensions
public static void ToXmlFile(this DataTable dt)
{
CommonTools.WriteFile(CommonTools.SaveFileName("XML File", "xml (*.xml)|*.xml|All Files (*.*)|*.*"), DataTableFunctions.ToXml(dt));
}
public static void ToCsvFile(this DataTable dt)
{
CommonTools.WriteFile(CommonTools.SaveFileName("Csv File", "csv (*.csv)|*.csv|All Files (*.*)|*.*"), DataTableFunctions.ToCsv(dt));
}
public static void ToHtmlFile(this DataTable dt)
{
CommonTools.WriteFile(CommonTools.SaveFileName("Html File", "html (*.html)|*.html|All Files (*.*)|*.*"), DataTableFunctions.ToHtml(dt));
}
//ToClipBoard() Extensions
public static void ToXmlClipboard(this DataTable dt)
{
Clipboard.SetText(DataTableFunctions.ToXml(dt));
}
public static void ToCsvClipboard(this DataTable dt)
{
Clipboard.SetText(DataTableFunctions.ToCsv(dt));
}
public static void ToHtmlClipboard(this DataTable dt)
{
Clipboard.SetText(DataTableFunctions.ToHtml(dt));
}
}
To use the convertion u will need the conversion classes itself. This should be only a sample... no error handling at all:
private static class DataTableFunctions
{
public static String ToCsv(DataTable dt)
{
var sb = new StringBuilder();
//Add Header Header
for (var x = 0; x < dt.Columns.Count; x++)
{
if (x != 0) sb.Append(";");
sb.Append(dt.Columns[x].ColumnName);
}
sb.AppendLine();
//Add Rows
foreach (DataRow row in dt.Rows)
{
for (var x = 0; x < dt.Columns.Count; x++)
{
if (x != 0) sb.Append(";");
sb.Append(row[dt.Columns[x]]);
}
sb.AppendLine();
}
return sb.ToString();
}
public static string ToXml(DataTable dt)
{
var writer = new StringWriter();
var name = dt.TableName;
if (name == string.Empty)
{
dt.TableName = "XMLTABLE";
}
dt.WriteXml(writer, true);
dt.TableName = name;
return writer.ToString();
}
public static string ToHtml(DataTable dt)
{
if (dt == null)
{
throw new ArgumentNullException("dt");
}
var builder = new StringBuilder();
builder.Append("");
builder.Append("");
builder.Append("
builder.Append("Page-");
builder.Append(Guid.NewGuid().ToString());
builder.Append("
builder.Append("");
builder.Append("");
builder.Append("
"); builder.Append(c.ColumnName); builder.Append(" | ");
"); builder.Append(r[c.ColumnName]); builder.Append(" | ");
builder.Append("");
builder.Append("");
return builder.ToString();
}
So the whole class could look like that:
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace Extensions
{
public static class Extensions
{
//ToString() Extensions:
public static string ToCsvString(this DataTable dt)
{
return DataTableFunctions.ToCsv(dt);
}
public static string ToHtmlString(this DataTable dt)
{
return DataTableFunctions.ToHtml(dt);
}
public static string ToXmlString(this DataTable dt)
{
return DataTableFunctions.ToXml(dt);
}
//ToFile() Extensions
public static void ToXmlFile(this DataTable dt)
{
CommonTools.WriteFile(CommonTools.SaveFileName("XML File", "xml (*.xml)|*.xml|All Files (*.*)|*.*"), DataTableFunctions.ToXml(dt));
}
public static void ToCsvFile(this DataTable dt)
{
CommonTools.WriteFile(CommonTools.SaveFileName("Csv File", "csv (*.csv)|*.csv|All Files (*.*)|*.*"), DataTableFunctions.ToCsv(dt));
}
public static void ToHtmlFile(this DataTable dt)
{
CommonTools.WriteFile(CommonTools.SaveFileName("Html File", "html (*.html)|*.html|All Files (*.*)|*.*"), DataTableFunctions.ToHtml(dt));
}
//ToClipBoard() Extensions
public static void ToXmlClipboard(this DataTable dt)
{
Clipboard.SetText(DataTableFunctions.ToXml(dt));
}
public static void ToCsvClipboard(this DataTable dt)
{
Clipboard.SetText(DataTableFunctions.ToCsv(dt));
}
public static void ToHtmlClipboard(this DataTable dt)
{
Clipboard.SetText(DataTableFunctions.ToHtml(dt));
}
///
/// Common Tool Class
///
private static class CommonTools
{
public static string SaveFileName(string title, string filter)
{
var fd = new SaveFileDialog();
fd.Title = title;
fd.Filter = filter;
fd.ShowDialog();
return fd.FileName;
}
public static void WriteFile(string filename, string content)
{
var sr = new StreamWriter(filename);
sr.Write(content);
sr.Flush();
sr.Close();
}
}
///
/// DataTable Converter Class
///
private static class DataTableFunctions
{
public static String ToCsv(DataTable dt)
{
var sb = new StringBuilder();
//Add Header Header
for (var x = 0; x < dt.Columns.Count; x++)
{
if (x != 0) sb.Append(";");
sb.Append(dt.Columns[x].ColumnName);
}
sb.AppendLine();
//Add Rows
foreach (DataRow row in dt.Rows)
{
for (var x = 0; x < dt.Columns.Count; x++)
{
if (x != 0) sb.Append(";");
sb.Append(row[dt.Columns[x]]);
}
sb.AppendLine();
}
return sb.ToString();
}
public static string ToXml(DataTable dt)
{
var writer = new StringWriter();
var name = dt.TableName;
if (name == string.Empty)
{
dt.TableName = "XMLTABLE";
}
dt.WriteXml(writer, true);
dt.TableName = name;
return writer.ToString();
}
public static string ToHtml(DataTable dt)
{
if (dt == null)
{
throw new ArgumentNullException("dt");
}
var builder = new StringBuilder();
builder.Append("");
builder.Append("");
builder.Append("
builder.Append("Page-");
builder.Append(Guid.NewGuid().ToString());
builder.Append("
builder.Append("");
builder.Append("");
builder.Append("
"); builder.Append(c.ColumnName); builder.Append(" | ");
"); builder.Append(r[c.ColumnName]); builder.Append(" | ");
builder.Append("");
builder.Append("");
return builder.ToString();
}
}
}
}
To use the Extensions simpley try this in ur app:
var x = myDataTable.ToCsvString();
or to save the datatable to a xml file:
myDataTable.ToXmlFile()
ConversionConversion EmoticonEmoticon