void SaveToExcel(CStringArray &arry,int RowCount,CString filePath)
{
int i,j,ColCount;
ColCount=arry.GetSize()/RowCount;
CString filename,strCreatTable,strInsert,strValue;
//filePath.Format("d://Test.xls");
DeleteFile(filePath);
CDatabase database;
CString sDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; // Excel安装驱动
CString sSql;
sSql.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=/"%s/";DBQ=%s", sDriver, filePath, filePath);
TRY{
if( database.OpenEx(sSql, CDatabase::noOdbcDialog) )
{
//赋值strCreatTable
strCreatTable="CREATE TABLE 结果表 ( ";
for (j=0;j <ColCount;j++)
{
if (j==ColCount-1)
strValue+=arry.GetAt(j)+" TEXT ";
else
strValue+=arry.GetAt(j)+" TEXT "+", ";
}
strCreatTable+=strValue+")";
sSql=strCreatTable;
database.ExecuteSQL(sSql);
//赋值strInsert, 从第二行开始 。
for (i=1;i <RowCount;i++)
{
strInsert=" INSERT INTO 结果表 (" + strValue +")VALUES"+"(";
for (j=0;j <ColCount;j++)
{
strInsert = strInsert + " '" + arry.GetAt(i*ColCount+j) +"' ";
if (j!=ColCount-1)
strInsert+=",";
}
strInsert+=")";
sSql=strInsert;
database.ExecuteSQL(sSql);
} //end of for();
// 关闭数据库
database.Close();
}
ShellExecute(NULL, NULL,"Excel.exe", filePath, NULL, SW_SHOWNORMAL);
}
CATCH_ALL(e)
{
//TRACE1("Excel驱动没有安装: %s",sDriver);
AfxMessageBox("不能打开Excel文件!");
}
END_CATCH_ALL;
}
其中strCreatTable="CREATE TABLE 结果表 ( PL5.1 TEXT , 理论 TEXT , 实际 TEXT , 偏差 TEXT , 上公差 TEXT , 下公差 TEXT , 超差 TEXT )"
这是在调试时strCreatTable的值,但执行这个sql语句时,程序就会执行到
CATCH_ALL(e)
{
//TRACE1("Excel驱动没有安装: %s",sDriver);
AfxMessageBox("不能打开Excel文件!");
}