BCB使用ADO访问Excel
9 月.22, 2015 in
编程小记
在ADO方式访问Excel时,需要注意几个问题:
1)新旧格式Excel(xls和xlsx)访问ado字符串的区别
2)获得多表单(sheet)的表单名
if(connXLS->Connected) connXLS->Connected = false; if(CompareStr(sFileExtName, "xls")==0) { sConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Persist Security Info=false;Data Source=" + sXlsFileName; //2003(Microsoft.Jet.Oledb.4.0) //string strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", excelFilePath); }else{ sConnectString = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 8.0;HDR=YES;IMEX=1';Persist Security Info=false;Data Source=" + sXlsFileName; //2007(Microsoft.ACE.OLEDB.12.0) //string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", excelFilePath); } connXLS->ConnectionString = sConnectString; adoXLS->Connection = connXLS; try { AnsiString sTableName; OleVariant EmptyParam = varEmpty; EmptyParam.VType = VT_ERROR; EmptyParam.VError = DISP_E_PARAMNOTFOUND; auto_ptr<TADOQuery> query(new TADOQuery(NULL)); query->Connection = connXLS; connXLS->OpenSchema(siTables, EmptyParam, EmptyParam, (TADODataSet*)query.get()); for ( ; !query->Eof; query->Next() ) { sTableName = query->FieldByName("TABLE_NAME")->AsString; printscreen("当前Excel %s", sTableName.c_str()); } if(adoXLS->Active) adoXLS->Close(); adoXLS->TableName = sTableName; adoXLS->Open(); }catch(Exception &exception){ ShowMessage(exception.Message); return; } |
Leave a Reply
You must be logged in to post a comment.