DataGridViewExporter

Add the ability to export a DataGridView control's contents to a CSV, HTML or XML file.
Email Image
kick it on DotNetKicks.com
// This example uses the following references.
using System.IO;
using System.Windows.Forms;
using System.Xml;
public static void ExportToCSV(DataGridView dataGrid, string exportPath)
{
    // Create the CSV file to which the grid data will be exported.
    using (StreamWriter streamWriter = new StreamWriter(exportPath, false))
    {
        // Write the column headers.
        foreach (DataGridViewColumn column in dataGrid.Columns)
        {
            streamWriter.Write(column.HeaderText);

            // Add a comma after the column header unless it is the last column.
            if (column.Index < (dataGrid.Columns.Count - 1))
                streamWriter.Write(",");
        }

        // Add a new line below the column headers.
        streamWriter.Write(streamWriter.NewLine);

        // Write the rows.
        foreach (DataGridViewRow row in dataGrid.Rows)
        {
            // Loop through the cells in this row.
            for (int i = 0; i < row.Cells.Count; i++)
            {
                // Write the column value.
                streamWriter.Write(row.Cells[i].Value);

                // Add a comma after the data in this cell unless it is the last cell in the row.
                if (i < (row.Cells.Count - 1))
                    streamWriter.Write(",");
            }

            // Write a new line.
            streamWriter.Write(streamWriter.NewLine);
        }

        streamWriter.Close();
    }
}
public static void ExportToHtml(DataGridView dataGrid, string exportPath)
{
    // Create the HTML file to which the grid data will be exported.
    using (StreamWriter streamWriter = new StreamWriter(exportPath, false))
    {
        // Write the initial HTML file.
        streamWriter.WriteLine("<html>");
        streamWriter.WriteLine("<head>");
        streamWriter.WriteLine("<title></title>");
        streamWriter.WriteLine("</head>");
        streamWriter.WriteLine("<body>");
        streamWriter.WriteLine("<table border=\"1\">");

        // Write the first Table Row.
        streamWriter.Write("<tr>");

        // Write the column headers.
        foreach (DataGridViewColumn column in dataGrid.Columns)
        {
            streamWriter.Write(String.Format("<th>{0}</th>", column.HeaderText));
        }

        // Close the first Table Row and add a new line to the file.
        streamWriter.Write("</tr>");
        streamWriter.Write(Environment.NewLine);

        // Write the rows.
        foreach (DataGridViewRow row in dataGrid.Rows)
        {
            // Write the Row.
            streamWriter.Write("<tr>");

            // Loop through the Cells and create a table cell for each.
            for (int i = 0; i < row.Cells.Count; i++)
            {
                // Write the column value.
                streamWriter.Write(String.Format("<td>{0}</td>", row.Cells[i].Value));
            }

            // Close the first Table Row and add a new line to the file.
            streamWriter.Write("</tr>");
            streamWriter.Write(Environment.NewLine);
        }

        // Close the HTML Table and file.
        streamWriter.WriteLine("</table>");
        streamWriter.WriteLine("</body>");
        streamWriter.WriteLine("</html>");

        streamWriter.Close();
    }
}
public static void ExportToXml(DataGridView dataGrid, string exportPath)
{
    XmlDocument xmlDocument = new XmlDocument();
    XmlElement xmlElement;
    XmlNode xmlNode;
    XmlText xmlText;

    // Add the XML Declaration.
    xmlNode = xmlDocument.CreateNode(XmlNodeType.XmlDeclaration, String.Empty, String.Empty);
    xmlDocument.AppendChild(xmlNode);

    // Add a Root Element called Table.
    xmlElement = xmlDocument.CreateElement(String.Empty, "Table", String.Empty);
    xmlDocument.AppendChild(xmlElement);

    // Reference the Table element we just added.
    XmlNode table = xmlDocument.ChildNodes[1];

    // Write the data in each of the rows.
    foreach (DataGridViewRow row in dataGrid.Rows)
    {
        // Create a new element to contain the data for each row.
        XmlElement tableRow = xmlDocument.CreateElement("Row");

        // Create an XmlNode for each cell in the row.
        for (int i = 0; i < row.Cells.Count; i++)
        {
            // Create an XmlElement with the name of the column heading for this column.
            xmlElement = xmlDocument.CreateElement(dataGrid.Columns[i].HeaderText.Replace(" ", ""));

            // Create an XmlText with the text value of this cell.
            xmlText = xmlDocument.CreateTextNode(row.Cells[i].Value.ToString());

            // Add the XmlText object to the XmlElement object.
            xmlElement.AppendChild(xmlText);

            // Add the XmlElement to the tableRow.
            tableRow.AppendChild(xmlElement);
        }

        // Add the tableRow to the table.
        table.AppendChild(tableRow);
    }

    // Save the XmlDocument.
    xmlDocument.Save(exportPath);
}

This article was published on the 26th November 2007
Last Edited 22nd July 2008