我遇到了一个很怪的问题,用excel导入到oracle中,
public DataSet ExceltoDs(string FilePath)
{
string strCon;
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + "; Extended Properties=Excel 8.0;";
OleDbConnection olecon = new OleDbConnection(strCon);
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", olecon);
DataSet myds = new DataSet();
myda.Fill(myds);
File.Delete(FilePath);
return myds;
}//先导入到dataset中
private void ExcelDaru(string FilePath)
{
Model.SYSDB.BINFO_DEPARTMENT model_binfo_department = new Model.SYSDB.BINFO_DEPARTMENT();
DataSet ds_binfo_department = ExceltoDs(FilePath); //将Excel中的数据放入ds
foreach (DataRow dr in ds_binfo_department.Tables[0].Rows)
{
BLL.SYSDB.BINFO_DEPARTMENT bll_binfo_department = new BLL.SYSDB.BINFO_DEPARTMENT();
model_binfo_department.depname = dr[1].ToString();
model_binfo_department.depcode = dr[0].ToString();
model_binfo_department.iseffect = '1';
if (model_binfo_department.depcode != "" & model_binfo_department.depname != "")//2008.06.20
{
if (!bll_binfo_department.Add(model_binfo_department))
{
MsgBox msg = new MsgBox();
msg.Show(this, "数据有误,请检查数据!");
}
}
}
}
depcode depname
123 A厂
234 B厂
ww C厂
导入数据如上,可是我发现如果depcode全是数字或者全是字符,顺利导入,但是如果depcode即有数字又有字符,dataset中只有数字,没有字符,也有是只能导入前两个,很诡异,不知道是什么原因,请各个大虾帮帮忙!非常感谢!
public DataSet ExceltoDs(string FilePath)
{
string strCon;
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + "; Extended Properties=Excel 8.0;";
OleDbConnection olecon = new OleDbConnection(strCon);
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", olecon);
DataSet myds = new DataSet();
myda.Fill(myds);
File.Delete(FilePath);
return myds;
}//先导入到dataset中
private void ExcelDaru(string FilePath)
{
Model.SYSDB.BINFO_DEPARTMENT model_binfo_department = new Model.SYSDB.BINFO_DEPARTMENT();
DataSet ds_binfo_department = ExceltoDs(FilePath); //将Excel中的数据放入ds
foreach (DataRow dr in ds_binfo_department.Tables[0].Rows)
{
BLL.SYSDB.BINFO_DEPARTMENT bll_binfo_department = new BLL.SYSDB.BINFO_DEPARTMENT();
model_binfo_department.depname = dr[1].ToString();
model_binfo_department.depcode = dr[0].ToString();
model_binfo_department.iseffect = '1';
if (model_binfo_department.depcode != "" & model_binfo_department.depname != "")//2008.06.20
{
if (!bll_binfo_department.Add(model_binfo_department))
{
MsgBox msg = new MsgBox();
msg.Show(this, "数据有误,请检查数据!");
}
}
}
}
depcode depname
123 A厂
234 B厂
ww C厂
导入数据如上,可是我发现如果depcode全是数字或者全是字符,顺利导入,但是如果depcode即有数字又有字符,dataset中只有数字,没有字符,也有是只能导入前两个,很诡异,不知道是什么原因,请各个大虾帮帮忙!非常感谢!
我把数字前加单引号,OK!
难道只能导全部是数字或者全部是字符?我调试了一下发现DataSet ds_binfo_department后面多了好多空行,怎么会这样,哪们高手帮忙解决一下!谢谢了
你可下载一个专门用于读取excel的控件,Aspose.Cells,很好用,不会再出现你所说的问题。
为数字,字符为字符