Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,


I want to import data from particular columns in an excel file to TextBoxes in C#.

I want it to be manually done. For example, I want:

A2 = TextBox1
B3 = TextBox2 and so on....

Here, I want to manually give column names to be copied.

Please help.

Regards
Aman

What I have tried:

string filepath = @"C:/Program Files/Test.xlsx";

            var excelApp = new Excel.Application();

            excelApp.Workbooks.Open(filepath);
            Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
            workSheet.Cells[2, "A"] = TitleTextBox.Text;
            workSheet.Cells[2, "B"] = PublisherTextBox.Text;
            workSheet.Cells[2, "C"] = FileNameTextBox.Text;
            workSheet.Cells[2, "D"] = IssueTextBox.Text;
            workSheet.Cells[2, "E"] = ReleaseDateTextBox.Text;
            workSheet.Cells[2, "F"] = PageCountTextBox.Text;
Posted
Updated 24-Apr-18 7:45am
Comments
Richard MacCutchan 21-Apr-18 3:10am    
Your code is copying textbox data into the worksheet cells, not the other way round. But either way, using textboxes for such a problem is not the best option. One of the data grid types would be better.
Primo Chalice 21-Apr-18 3:18am    
Oh, I see. Sorry, my bad. So is it possible to copy the data from Excel to DataGrid and then from DataGrid to TextBox?
Richard MacCutchan 21-Apr-18 7:37am    
Why do you want to use a Textbox? A datagrid will provide everything you need to manage Excel (or any other database type) data.

1 solution

The simplest way is to use ADO.NET[^] (OleDb[^]).
Steps to do:
1. Create OleDbConnection[^]
2. Create OleDbCommand[^]
3. Execute command to OleDbReader[^]
4. Load data to DataTable[^]
5. Bind data to DataGridView via DataSource[^]

C#
string sFileName = @"D:\MyUsers.xlsx"; //change the path to Excel file
string sConStr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES';", sFileName);


//1.
using (OleDbConnection connection = new OleDbConnection(sConStr))
{
    string sql = "SELECT * FROM [Sheet1$]";
    connection.Open();
    //2.
    OleDbCommand command = new OleDbCommand(sql, connection);

    //3.
    OleDbDataReader reader = command.ExecuteReader();

    //4.
    DataTable dt = new DataTable();
    dt.Load(reader);

    //5.
    this.dataGridView1.DataSource = dt;

    reader.Close();
    reader.Dispose();
    command.Dispose();
}


For further details, please see:
How to: Bind Data to the Windows Forms DataGridView Control | Microsoft Docs[^]
 
Share this answer
 
Comments
Primo Chalice 28-Apr-18 1:00am    
I will surely try this out let you know :)
Maciej Los 8-May-18 4:41am    
Have you tried, yet?
Primo Chalice 9-May-18 6:26am    
Yes, but I finally wrote a code for TextBoxes

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;

int rCnt;
int cCnt;
int rw = 0;
int cl = 0;

xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(@"C:\Test\Text.xlsx");
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

range = xlWorkSheet.UsedRange;
rw = range.Rows.Count;
cl = range.Columns.Count;

for (rCnt = 2; rCnt <= rw; rCnt++)
{
for (cCnt = 1; cCnt <= cl; cCnt++)
{
TitleTextBox.Text = (string)(range.Cells[2, 1] as Excel.Range).Value2;
PublisherTextBox.Text = (string)(range.Cells[2, 2] as Excel.Range).Value2;
FileNameTextBox.Text = (string)(range.Cells[2, 3] as Excel.Range).Value2;
IssueTextBox.Text = (string)(range.Cells[2, 4] as Excel.Range).Value2;
ReleaseDateTextBox.Text = (string)(range.Cells[2, 5] as Excel.Range).Value2;
PageCountTextBox.Text = Convert.ToString((range.Cells[2, 6] as Excel.Range).Value2);
}
}

xlWorkBook.Close(true, null, null);
xlApp.Quit();

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