| |

VerySource

 Forgot password?
 Register
Search
View: 869|Reply: 4

Ask c# to import data from excel and text documents into DataGridView

[Copy link]

2

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-10 14:00:02
| Show all posts |Read mode
Instead of using excel or text documents as the data source, import the data into the datagridview.

For example, my datagridview originally had 4 rows of records. Now I want to import the data in a text document or excel behind the 4 rows of data, that is, add a few rows to the datagridview. The added row data is the data in excel or text document.
Reply

Use magic Report

0

Threads

8

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-12-10 15:30:01
| Show all posts
The datagridview should correspond to a DATATABLE, first use another DATATABLE (the structure is the same as the previous one) to get the data in EXCEL or TXT, use the MERGE of the DATATABLE, and then re-bind, the idea is generally like this.
Reply

Use magic Report

1

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-10 18:15:01
| Show all posts
XmlDocument doc =new Xmldocument();
doc.Load("Your Path");
XmlNode s=doc.CreateElement();

..........
There is information online, search more
Reply

Use magic Report

0

Threads

13

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-12-10 18:30:01
| Show all posts
System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection();
cn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Book1.xls;Extended Properties=Excel 8.0;";
cn.Open();
string sql = "select * from [Sheet1$A2:F10]";
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(sql, cn);
System.Data.OleDb.OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
  MessageBox.Show(dr[0].ToString());
}

cn.Close();

Use the above statement to connect first and then add
Reply

Use magic Report

2

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-12-10 19:00:01
| Show all posts
Thanks everyone for helping
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list