如何将文本转换为DataGridView中的列?

时间:2018-01-15 10:10:01

标签: c# .net excel datagridview

我刚开始学习C#,我有这个问题...... 如何将文本从Excel源文件转换为DataGridView中的列?

来源格式:

NO  BRAND   NAME,ADRESS,PHONE

1 Brand1 Name1,adress1,phone1

2 Brand2 Name2,adress1,phone2

3 Brand1 Name3,adress3,phone3

4 Brand3 Name4,adress4,phone4

5 Brand1 Name5,adress5,phone5

6 Brand1 Name6,adress6,phone6

7 Brand4 Name7,adress7,phone7

8 Brand2 Name8,adress8,phone8

9 Brand4 Name9,adress9,phone9

10 Brand2 Name10,adress10,phone10

Excel来源: https://www.easypaste.org/file/dmqoMGDW/export-sources.xls?lang=en

我希望导入后的表格如下所示: enter image description here

谢谢所有帮助我......

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExceltoDGV
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openfile1 = new OpenFileDialog();
                if (openfile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.textBox1.Text = openfile1.FileName;

                    string pathconn = "Provider = Microsoft.jet.OLEDB.4.0; Data source=" + textBox1.Text + ";Extended Properties=\"Excel 8.0;HDR= yes;\";";
                    OleDbConnection conn = new OleDbConnection(pathconn);
                    OleDbDataAdapter MyDataAdapter = new OleDbDataAdapter("Select * from [Inwestycje$]", conn);
                    DataTable dt = new DataTable();
                    MyDataAdapter.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
            }
            catch
            {

            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我使用这种方法将我的csv转换为数据集。试试这个:

private DataTable GetDataFromSource(string connectionstring)
    {
         DataTable DtCsv = new DataTable();  
         string Fulltext;

            using (StreamReader sr = new StreamReader(connectionstring))
            {
                while (!sr.EndOfStream)
                {
                    Fulltext = sr.ReadToEnd().ToString(); //read full file text  
                    string[] rows = Fulltext.Split('\n'); //split full file text into rows  
                    for (int i = 0; i < rows.Count() - 1; i++)
                    {
                        string[] rowValues = rows[i].Split(','); //split each row with comma to get individual values  
                        {
                            if (i == 0)
                            {
                                for (int j = 0; j < rowValues.Count(); j++)
                                {
                                    DtCsv.Columns.Add(rowValues[j]); //add headers  
                                }
                            }
                            else
                            {
                                DataRow dr = DtCsv.NewRow();
                                for (int k = 0; k < rowValues.Count(); k++)
                                {
                                    dr[k] = rowValues[k].ToString();
                                }
                                DtCsv.Rows.Add(dr); //add other rows  
                            }
                        }
                    }
                }
            }

        return DtCsv;
    }
相关问题