您的位置:程序门 -> .net技术 -> c#



用oledb的方法向excel中写数据出错.


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


用oledb的方法向excel中写数据出错.[已结贴,结贴人:lin_jianren]
发表于:2007-03-02 10:30:27 楼主
用oledb的方法向excel中写字符串出错(写入数字没问题).   提示data   type   mismatch   in   criteria   expression.   可是我的cell   format已经是   general.   数据类型是兼容的啊!不知道是什么原因.还请高人解答
发表于:2007-03-02 11:16:101楼 得分:0
自己顶
发表于:2007-03-02 11:31:132楼 得分:0
帮你顶
发表于:2007-03-02 12:30:113楼 得分:0
///  
///   写入excel文档
///  
///   文件名称
public   bool   savefp2toexcel(string   path)
{
try
{
string   strconn   =   "provider=microsoft.jet.oledb.4.0; "   + "data   source= "+   path   + "; "+ "extended   properties=excel   8.0; ";
oledbconnection   conn   =   new   oledbconnection(strconn);
conn.open();  
system.data.oledb.oledbcommand   cmd=new   oledbcommand   ();
cmd.connection   =conn;
//cmd.commandtext   = "update   [sheet1$]   set   姓名= '2005-01-01 '   where   工号= '日期 ' ";
//cmd.EXECutenonquery   ();
for(int   i=0;i   {
if(fp2.sheets   [0].cells[i,0].text!= " ")
{
cmd.commandtext   = "insert   into   [sheet1$]   (工号,姓名,部门,职务,日期,时间)   values( ' "+fp2.sheets   [0].cells[i,0].text+   " ', ' "+
fp2.sheets   [0].cells[i,1].text+ " ', ' "+fp2.sheets   [0].cells[i,2].text+ " ', ' "+fp2.sheets   [0].cells[i,3].text+
" ', ' "+fp2.sheets   [0].cells[i,4].text+ " ', ' "+fp2.sheets   [0].cells[i,5].text+ " ') ";
cmd.EXECutenonquery   ();
}
}
conn.close   ();
return   true;
}
catch(system.data.oledb.oledbexception   ex)
{
system.diagnostics.debug.writeline   ( "写入excel发生错误: "+ex.message   );
}
return   false;
}
发表于:2007-03-02 13:47:114楼 得分:40
你的数据类型不匹配.   检查一下,你要写入字符串那列是否有数字?   如果有的话,把数字去掉.


快速检索

最新资讯
热门点击