Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Guys, i am starting a new project for SMS I need your help for that I want to open excel file and import all contacts in the rich textbox from excel file.
thnx

What I have tried:

private void button2_Click(object sender, EventArgs e)
        {
           if(openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
           {
               label7.Text = openFileDialog1.FileName;
               textBox1.Text = File.ReadAllText(label7.Text);
           }
}

        private void button3_Click(object sender, EventArgs e)
        {
          if(saveFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
          {
              File.WriteAllText(saveFileDialog1.FileName, textBox1.Text);
          }
        }
Posted
Updated 7-Aug-17 0:52am

There are lots of examples of how to export a RichTextBox to an excel spreadsheet: c# excel to richtextbox - Google Search[^]

It would be a simple matter of reversing the process...
 
Share this answer
 
It is common to use a ListView instead of a RichText box to show tabular data. So most examples that can be found will be for ListViews (or even recordsets). They will show you how to get the Excel data for each cell (line by line and row by row).

If you still want to use a RichText box and want to dispaly the data as simple text, just append the content for each cell using appropriate separators. If you need a formatted table, you have to read the RTF specification regarding tables (which is rather complicated).

There are also different methods to read Excel files:
COM interop which requires an installed Excel version, and Excel database drivers (OleDb) or 3rd party DLLs which both do not require Excel to be installed.

Some links from CP about reading Excel with C#:
Working with Excel Using C#[^] (Interop)
C# How To Read .xlsx Excel File With 3 Lines of Code[^] (DLL)
Read Excel File into DataSet in ASP.NET Using C#[^] (OleDb)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900