我有sql数据库(DB),Excel表,DataSet
我对Excel表有如下操作
首先我将Excel表的数据读到ds中,然后将DB的数据导入到Excel表中,要求是:如果DB中的数据与Excel表的数据相同,就删掉Excel表的数据,然后再插入DB的数据。
现在我碰到的问题是这样的:
当我DB中有10条数据,Excel中也有10条数据,这些数据都是一模一样的。以下是我的代码摘要。
System.Data.DataTable dt = CDB.GetSQLTable(mySQL);//读DB的数据DataSet ds = new DataSet();
ds = GetExcelDs(myPath, "[" + mySheet + "$]");////获取Excel中的数据!
int waterdataRows = dt.Rows.Count;
int waterdataCols = dt.Columns.Count;string PropertyNo = "(";DataView DView;                for (int i = 1; i <= waterdataRows; i++)
                {
                    DataRow dr = dt.Rows[i - 1];
                    string id = dr["PropertyNo"].ToString().Trim();                    PropertyNo += "'" + id + "',";
                    DView = ds.Tables[0].DefaultView;                    for (int k = 16; k <= DView.Count; k++)
                    {                        if (DView[k - 1][2].ToString().Trim() == "")
                        {
                            break;
                        }
                        //判断要插入的数据在Excel表中是否存在,存在删除,否则插入
                        if (id == DView[k - 1][2].ToString().Trim())
                        {
                            
                            
                            ((Excel.Range)excel.Cells[k, 3]).EntireRow.Delete(XlDeleteShiftDirection.xlShiftUp);/////删除重复数据                            
                            continue;
                        }
                    }
                    //插入数据
                    ((Excel.Range)excel.Cells[i + 16, 3]).EntireRow.Insert(0, 0);
                    
                    for (int j = 1; j <= waterdataCols; j++)
                    {
                        excel.Cells[i + 16, j + 2] = dt.Rows[i - 1].ItemArray.GetValue(j - 1);
                        
                    }
看到这里,我的问题来了,我这个代码并不能把Excel数据删掉,并且会删错数据,插入时也会出错!
请问,有做过对Excel表操作的前辈帮帮忙!在这里谢谢各位了!