|
马上注册登录,享用更多网站功能!
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
我在UG二次开发时,用调用DLL的方式打开一个应用程序,DLL里包含有调用ODBC建立和操作".xls"的程序,运行后什么也得不到,而相同的程序用EXE方式却能生成“.xls”文件并能操作。如下例程:
1.EXE
void CTDlg::OnOK()
{
// TODO: Add extra validation here
WE();
// CDialog::OnOK();
}
void CTDlg::WE()
{
CDatabase database;
CString sDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; // Excel°²×°Çý¶¯
CString sExcelFile,sPath;
CString sSql;
//»ñÈ¡Ö÷³ÌÐòËùÔÚ·¾¶,´æÔÚsPathÖÐ
GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer ();
int nPos;
nPos=sPath.ReverseFind ('\\');
sPath=sPath.Left (nPos);
sExcelFile ="c:\\x.xls";// sPath + "\\Demo.xls"; // Òª½¨Á¢µÄExcelÎļþ
TRY
{
// ´´½¨½øÐдæÈ¡µÄ×Ö·û´®
sSql.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",sDriver, sExcelFile, sExcelFile);
// ´´½¨Êý¾Ý¿â (¼ÈExcel±í¸ñÎļþ)
if( database.OpenEx(sSql,CDatabase::noOdbcDialog) )
{
// ´´½¨±í½á¹¹(ÐÕÃû¡¢ÄêÁä)
sSql = "CREATE TABLE Exceldemo (Name TEXT,Age NUMBER)";
database.ExecuteSQL(sSql);
// ²åÈëÊýÖµ
sSql = "INSERT INTO Exceldemo (Name,Age) VALUES ('Ðì¾°ÖÜ',26)";
database.ExecuteSQL(sSql);
sSql = "INSERT INTO Exceldemo (Name,Age) VALUES ('ÐìÖ¾»Û',22)";
database.ExecuteSQL(sSql);
sSql = "INSERT INTO Exceldemo (Name,Age) VALUES ('¹ù»Õ',27)";
database.ExecuteSQL(sSql);
}
// ¹Ø±ÕÊý¾Ý¿â
database.Close();
AfxMessageBox("ExcelÎļþдÈë³É¹¦£¡");
}
CATCH_ALL(e)
{
TRACE1("ExcelÇý¶¯Ã»Óа²×°: %s",sDriver);
}
END_CATCH_ALL;
}
CString CTDlg::GetExcelDriver()
{
char szBuf[2001];
WORD cbBufMax = 2000;
WORD cbBufOut;
char *pszBuf = szBuf;
CString sDriver;
// »ñÈ¡ÒÑ°²×°Çý¶¯µÄÃû³Æ(º­ÊýÔÚodbcinst.hÀï)
if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
return "";
// ¼ìË÷ÒÑ°²×°µÄÇý¶¯ÊÇ·ñÓÐExcel...
do
{
if (strstr(pszBuf, "Excel") != 0)
{
//·¢ÏÖ !
sDriver = CString(pszBuf);
break;
}
pszBuf = strchr(pszBuf, '\0') + 1;
}
while (pszBuf[1] != '\0');
return sDriver;
}
2.DLL
void CClutchData::WriteExcel()
{
CDatabase database;
CString sDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; // Excel°²×°Çý¶¯
CString sExcelFile,sPath;
CString sSql;
//»ñÈ¡Ö÷³ÌÐòËùÔÚ·¾¶,´æÔÚsPathÖÐ
GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer ();
int nPos;
nPos=sPath.ReverseFind ('\\');
sPath=sPath.Left (nPos);
sExcelFile = "C:\\xx.xls";//sPath + "\\Demo.xls"; // Òª½¨Á¢µÄExcelÎļþ
TRY
{
// ´´½¨½øÐдæÈ¡µÄ×Ö·û´®
sSql.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",sDriver, sExcelFile, sExcelFile);
// ´´½¨Êý¾Ý¿â (¼ÈExcel±í¸ñÎļþ)
if( database.OpenEx(sSql,CDatabase::noOdbcDialog) )
{
// ´´½¨±í½á¹¹(ÐÕÃû¡¢ÄêÁä)
sSql = "CREATE TABLE Exceldemo (Name TEXT,Age NUMBER)";
database.ExecuteSQL(sSql);
// ²åÈëÊýÖµ
sSql = "INSERT INTO Exceldemo (Name,Age) VALUES ('Ðì¾°ÖÜ',26)";
database.ExecuteSQL(sSql);
sSql = "INSERT INTO Exceldemo (Name,Age) VALUES ('ÐìÖ¾»Û',22)";
database.ExecuteSQL(sSql);
sSql = "INSERT INTO Exceldemo (Name,Age) VALUES ('¹ù»Õ',27)";
database.ExecuteSQL(sSql);
}
// ¹Ø±ÕÊý¾Ý¿â
database.Close();
AfxMessageBox("ExcelÎļþдÈë³É¹¦£¡");
}
CATCH_ALL(e)
{
TRACE1("ExcelÇý¶¯Ã»Óа²×°: %s",sDriver);
}
END_CATCH_ALL;
}
CString CCData::GetExcelDriver()
{
char szBuf[2001];
WORD cbBufMax = 2000;
WORD cbBufOut;
char *pszBuf = szBuf;
CString sDriver;
// »ñÈ¡ÒÑ°²×°Çý¶¯µÄÃû³Æ(º­ÊýÔÚodbcinst.hÀï)
if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut))
return "";
// ¼ìË÷ÒÑ°²×°µÄÇý¶¯ÊÇ·ñÓÐExcel...
do
{
if (strstr(pszBuf, "Excel") != 0)
{
//·¢ÏÖ !
sDriver = CString(pszBuf);
break;
}
pszBuf = strchr(pszBuf, '\0') + 1;
}
while (pszBuf[1] != '\0');
return sDriver;
}
void CCDlg::OnClutchSave()
{
// TODO: Add your control notification handler code here
//Disable the CutchSave Button
m_sCData.WriteExcel();
} |
|