1

尝试检查excel中的单元格值是否为空。如果它是空的,我想写一个关闭单元格的值(例如,A25 是空的。我想要 B25 值)。我想浏览整个excel文件。这是我现在的代码,我卡住了!(滚动到代码底部。最后一部分很重要)。

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

        private void btnReadExcel_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtFileName.Text + ";Extended Properties=Excel 12.0");


            OleDbDataAdapter da = new OleDbDataAdapter("select * from[Sheet1$]", con);
            da.Fill(dsContacts);
            MessageBox.Show(dsContacts.Tables[0].Rows.Count.ToString());
            dgContacts.DataSource = dsContacts.Tables[0];


        }

        private void tblContactsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.tblContactsBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.database11DataSet);

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            this.tblContactsTableAdapter.Fill(this.database11DataSet.tblContacts);

        }

        private void button1_Click(object sender, EventArgs e)
        {
                    foreach (DataRow r in dsContacts.Tables[0].Rows) 
                     {

                       DataRow dr = database11DataSet.tblContacts.NewRow();
                        dr[0] = r[0];
                        dr[1] = r[1];
                        dr[2] = r[2];
                        database11DataSet.tblContacts.Rows.Add(dr);
                     }
                }
            }
        }
4

0 回答 0