How to Export DataTable to RTF through DataGridView

Why Export Datatable to RTF?

RTF is a Microsoft specification and certified file format used with DOC and DOCX. It is a core part of the Microsoft Office system. RTF usually used for cut and paste, including paste special and used when opening documents into Word. RTF does not cause document corruption.

RTF allows Workshare tremendous flexibility in successfully translating between any number of document types because of its ubiquity. Workshare products can not only maintain document fidelity, but provide portability advantages when moving around different Microsoft Word systems, or translating to other document types.

How to Export DataTable to RTF through DataGridView?

Download Spire.DataExport (or Spire.Office) with .NET Framework together. Only 2 Simple steps you can finish the whole datatable to RTF exporting process.

Step 1: Load Data Information

Before exporting data from DataTable, we should load data information from data source. And select which information we need export. Through DataGridVew, we even can preview and modify data information. So, in this step, our job is to prepare data which is about to be exported out.

[C#]
        private void btnLoad_Click(object sender, EventArgs e)
        {
            oleDbConnection.ConnectionString = this.textBox1.Text;
            OleDbCommand oleDbCommand = new OleDbCommand();
            oleDbCommand.CommandText = this.textBox2.Text;
            oleDbCommand.Connection = oleDbConnection;
            using (OleDbDataAdapter da = new OleDbDataAdapter(oleDbCommand))
            {
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }

Effect Screenshot

Datatable to RTF

Step 2: Set Export into RTF

Spire.DataExport allows user to export data into most popular file formats including MS Excel, RTF, HTML, PDF, XML, CSV, DBF, DIF, etc. Here we need set it as RTF format. Spire.DataExport will create a new RTF file and through DataGridView export data into RTF file. You also can rename the file as you like.

[C#]
        private void btnRun_Click(object sender, EventArgs e)
        {
            Spire.DataExport.RTF.RTFExport RTFExport = new Spire.DataExport.RTF.RTFExport();
            RTFExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
            RTFExport.DataTable = this.dataGridView1.DataSource as DataTable;
            RTFExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
            RTFExport.FileName = "RTF0722.rtf";
            RTFExport.SaveToFile();
        }

Effect Screenshot

Datatable to RTF