1
 {    
            DialogResult dialogResult = MessageBox.Show("Sure", "Some Title", MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {
            dt = dsSource.Tables[Index];
            dt.Reset();
            Excel.Workbook workbook;
            Excel.Worksheet NwSheet;
            Excel.Range ShtRange;
            Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();
            OpenFileDialog filedlgExcel = new OpenFileDialog();
            filedlgExcel.Title = "Select file";
            filedlgExcel.InitialDirectory = @"c:\";
            //filedlgExcel.FileName = textBox1.Text;
            filedlgExcel.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
            filedlgExcel.FilterIndex = 1;
            filedlgExcel.RestoreDirectory = true;
            if (filedlgExcel.ShowDialog() == DialogResult.OK)
        {

            workbook = ExcelObj.Workbooks.Open(filedlgExcel.FileName, Missing.Value, Missing.Value,
                 Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                 Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            NwSheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
            ShtRange = NwSheet.UsedRange;
            for (int Cnum = 1; Cnum <= ShtRange.Columns.Count; Cnum++)
            {
                dt.Columns.Add(new DataColumn((ShtRange.Cells[1, Cnum] as Excel.Range).Value2.ToString()));
            }
            dt.AcceptChanges();
            string[] columnNames = new String[dt.Columns.Count];
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                columnNames[0] = dt.Columns[i].ColumnName;
            }
            //string[] columnNames = (from dc in dt.Columns.Cast<DataColumn>() select dc.ColumnName).ToArray();


            for (int Rnum = 2; Rnum <= ShtRange.Rows.Count; Rnum++)
            {
                DataRow dr = dt.NewRow();
                for (int Cnum = 1; Cnum <= ShtRange.Columns.Count; Cnum++)
                {
                    if ((ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2 != null)
                    {
                        dr[Cnum - 1] = (ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString();
                    }
                }
                dt.Rows.Add(dr);
                dt.AcceptChanges();
            }
            workbook.Close(true, Missing.Value, Missing.Value);
            ExcelObj.Quit();

            dataGridView1.DataSource = dt;                   

}

这是我用来将 excel 数据提取到 datagridview 中的代码。它写在按钮点击上。第一次单击按钮时,执行的操作非常完美,数据显示在 datagridview 中,但如果我再试一次,只会出现列名,但没有数据,它显示单行空白数据。我调试了代码,发现数据正在进入 datatable(dt),但是当它映射到 datagridview 时,问题出在某个地方。在这条线上

dataGridView1.DataSource = dt;

我应该怎么做才能解决这个问题

4

1 回答 1

1

* 编辑 *

问题是当您清除 DataTable 时,它​​只会删除 Rows。为了删除您需要执行此操作的列:-

dt.Columns.Clear();

dt.Reset() 不会删除数据,它只会丢弃所有更改。

每次单击按钮时调用此事件时,都需要删除 DataTable 中保存的先前行和列。

下面的代码对我来说很好:-

private void button1_Click(object sender, EventArgs e)
        {
            dt.Columns.Clear();
            dt.Rows.Clear();


            Excel.Workbook workbook;
            Excel.Worksheet NwSheet;
            Excel.Range ShtRange;
            Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();

            OpenFileDialog filedlgExcel = new OpenFileDialog();
            filedlgExcel.Title = "Select file";
            filedlgExcel.InitialDirectory = @"c:\";

            filedlgExcel.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
            filedlgExcel.FilterIndex = 1;
            filedlgExcel.RestoreDirectory = true;
            if (filedlgExcel.ShowDialog() == DialogResult.OK)
            {
                workbook = ExcelObj.Workbooks.Open(filedlgExcel.FileName, Missing.Value, Missing.Value,
                 Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                 Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

                NwSheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
                ShtRange = NwSheet.UsedRange;
                for (int Cnum = 1; Cnum <= ShtRange.Columns.Count; Cnum++)
                {
                    dt.Columns.Add(new DataColumn((ShtRange.Cells[1, Cnum] as Excel.Range).Value2.ToString()));
                }
                dt.AcceptChanges();
                string[] columnNames = new String[dt.Columns.Count];
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    columnNames[0] = dt.Columns[i].ColumnName;
                }
                //string[] columnNames = (from dc in dt.Columns.Cast<DataColumn>() select dc.ColumnName).ToArray();


                for (int Rnum = 2; Rnum <= ShtRange.Rows.Count; Rnum++)
                {
                    DataRow dr = dt.NewRow();
                    for (int Cnum = 1; Cnum <= ShtRange.Columns.Count; Cnum++)
                    {
                        if ((ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2 != null)
                        {
                            dr[Cnum - 1] = (ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString();
                        }
                    }
                    dt.Rows.Add(dr);
                    dt.AcceptChanges();
                }
                workbook.Close(true, Missing.Value, Missing.Value);
                ExcelObj.Quit();

                dataGridView1.DataSource = dt;   
            }
        }
     }
于 2012-12-11T09:53:20.210 回答