Data Export Text/SYLK/CSV/DIF in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

This sample demonstrates how to export data table to text/sylk/csv/dif file.

The picture above represents using Excel application to open a SYLK type output file.

private void btnText_Click(object sender, EventArgs e)
{
    System.Data.OleDb.OleDbConnection oleDbConnection1
         = new System.Data.OleDb.OleDbConnection();
    oleDbConnection1.ConnectionString
        = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb";

    System.Data.OleDb.OleDbCommand oleDbCommand1
        = new System.Data.OleDb.OleDbCommand();
    oleDbCommand1.CommandText = "select * from parts";
    oleDbCommand1.Connection = oleDbConnection1;

    Spire.DataExport.TXT.TXTExport txtExport1
        = new Spire.DataExport.TXT.TXTExport();

    txtExport1.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
    txtExport1.DataFormats.CultureName = "zh-CN";
    txtExport1.DataFormats.Currency = "c";
    txtExport1.DataFormats.DateTime = "yyyy-M-d H:mm";
    txtExport1.DataFormats.Float = "g";
    txtExport1.DataFormats.Integer = "g";
    txtExport1.DataFormats.Time = "H:mm";
    txtExport1.DataEncoding = Spire.DataExport.Common.EncodingType.ASCII;
    txtExport1.SQLCommand = oleDbCommand1;

    oleDbConnection1.Open();

    txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.SYLK;
    txtExport1.FileName = "sample.slk";
    txtExport1.SaveToFile();

    txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.CSV;
    txtExport1.FileName = "sample.csv";
    txtExport1.SaveToFile();

    txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.DIF;
    txtExport1.FileName = "sample.dif";
    txtExport1.SaveToFile();

    txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.TXT;
	txtExport1.FileName = "sample.txt";
	txtExport1.SaveToFile();
}

Private Sub btnText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnText.Click
    Dim oleDbConnection1 As New System.Data.OleDb.OleDbConnection()
    oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb"

    Dim oleDbCommand1 As New System.Data.OleDb.OleDbCommand()
    oleDbCommand1.CommandText = "select * from parts"
    oleDbCommand1.Connection = oleDbConnection1

    Dim txtExport1 As New Spire.DataExport.TXT.TXTExport()

    txtExport1.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView
    txtExport1.DataFormats.CultureName = "zh-CN"
    txtExport1.DataFormats.Currency = "c"
    txtExport1.DataFormats.DateTime = "yyyy-M-d H:mm"
    txtExport1.DataFormats.Float = "g"
    txtExport1.DataFormats.[Integer] = "g"
    txtExport1.DataFormats.Time = "H:mm"
    txtExport1.DataEncoding = Spire.DataExport.Common.EncodingType.ASCII
    txtExport1.SQLCommand = oleDbCommand1

    oleDbConnection1.Open()

    txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.SYLK
    txtExport1.FileName = "sample.slk"
    txtExport1.SaveToFile()

    txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.CSV
    txtExport1.FileName = "sample.csv"
    txtExport1.SaveToFile()

    txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.DIF
    txtExport1.FileName = "sample.dif"
    txtExport1.SaveToFile()

    txtExport1.ExportType = Spire.DataExport.TXT.TextExportType.TXT
    txtExport1.FileName = "sample.txt"
    txtExport1.SaveToFile()
End Sub