我有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表操作的前辈帮帮忙!在这里谢谢各位了!
我对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表操作的前辈帮帮忙!在这里谢谢各位了!
解决方案 »
- C# 把“http://forum.csdn.net”字符串 转成0x003c77d8
- 关于随机取值的问题
- 代码中添加!IsPostBack 和在前台选中“启用AutoPostBack”有什么区别啊?
- 企业级c/s该怎么设计?
- MouseButtonEventHandler
- rtf 中文版的规范文档谁有啊,帮忙啊
- 我想把网站中的asxc模板文件全部写到dll中去,如何来做呢?
- 各位专家,请问word的bookmarks没有get_item方法怎么解决的啊?谢谢
- 请教连接数据库
- c#中如何使用ffmpeg实现视频截图
- 咨询各位大虾一个简单的关于internet 服务器的问题啊!!!!!
- 事务函数?
http://blog.csdn.net/downmoon/archive/2009/01/16/3796652.aspx
然后根据你的要求修改ds,再把ds中的数据写入到excel就行了