日期:2014-05-17  浏览次数:20976 次

C# excel导入datagridview显示问题
EXCEL有10行10列,导入后, DataGridView显示10行20列.会多出10列..意思就是EXCEL有M行N列.DataGridView就显示M行2N列.

C# code

 private void button1_Click(object sender, EventArgs e)//导入Button
        {
            //dataGridView1.Columns.Clear();
            //for (int i = 0; i < 27; i++)
            //    dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Excel文件";
            ofd.FileName = "";
            ofd.InitialDirectory = Environment.GetFolderPath(Environment .SpecialFolder .MyDocuments );
            ofd.Filter = "Excel文件(*.xls)|*.xls";
            ofd.ValidateNames = true;
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;

            string strName = string.Empty;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                strName = ofd.FileName;
            }

            if (strName == "")
            {
                MessageBox.Show("没有选择Excel文件!无法进行数据导入");
                return;
            }
            ExcelToDataGridView(strName ,this.dataGridView1);
        }

        public void ExcelToDataGridView(string filePath,DataGridView dgv)
        {
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=YES;IMEX=1'";
            OleDbConnection conn = new OleDbConnection(strConn );
            conn.Open();
            string strExcel = "";
            OleDbDataAdapter myCommand = null;
            DataSet ds = null;
            strExcel = "select * from [sheet4$]";
            myCommand = new OleDbDataAdapter(strExcel ,strConn );
            ds = new DataSet();
            //myCommand.Fill(ds,"[sheet4$]");
            myCommand.Fill(ds, "table1");



            DataTable tb = new DataTable();
            foreach (DataGridViewColumn dgvc in dgv.Columns)
            {
                if (dgvc.Visible && dgvc.CellType != typeof(DataGridViewCheckBoxCell))
                {
                    DataColumn dc = new DataColumn();
                    dc.ColumnName = dgvc.DataPropertyName;
                    tb.Columns.Add(dc);
                }
            }

            foreach (DataRow excelRow in ds.Tables[0].Rows)
            {
                int i = 0;
                DataRow dr = tb.NewRow();
                foreach (DataColumn dc in tb.Columns)
                {
                    dr[dc ]=excelRow [i];
                    i++;
                }
                tb.Rows.Add(dr );
            }
            dgv.DataSource = tb;
            
        }



到底是什么原因造成上述问题..请教高人讲解一下...

------解决方案--------------------
跟进 ExcelToDataGridView中调试,就两个for循环吗,看看dgv.Columns和后面table的columns到底是多少
------解决方案--------------------
探讨
跟进 ExcelToDataGridView中调试,就两个for循环吗,看看dgv.Columns和后面table的columns到底是多少

------解决方案--------------------
一般都断点查看数据对比就知道条件设定对否.
------解决方案--------------------
你的列循环有问题,打断点调试看下
------解决方案--------------------
public void ExcelToDataGridView(string filePath,DataGridView dgv)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=YES;IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn );
conn.Open();
string strExcel = "";