使用MFC将数据插入excel 2003,我发现了65536行的限制,
我不想创建另一张表来保存数据,所以我想使用excel 2007,
扩展名为.xlsx。
我使用以下代码来获取驱动程序
((CDR12Dlg*)AfxGetMainWnd())->sExcelDriver = _T("MICROSOFT EXCEL DRIVER (*.xls)");
((CDR12Dlg*)AfxGetMainWnd())->sTab1CurveFile.strExcelFilePath.Format(((CDR12Dlg*)AfxGetMainWnd())->sTab1CurveFile.strBaseFolder);
((CDR12Dlg*)AfxGetMainWnd())->sTab1CurveFile.strExcelFilePath.AppendFormat(_T("%s_%s_%.0f℃_%d%s"),
((CDR12Dlg*)AfxGetMainWnd())->sTab1CurveFile.strDevID,
((CDR12Dlg*)AfxGetMainWnd())->sTab1CurveFile.strDevModel,
theApp.fTab1TestAvgTemp,
loop,
_T(".xls"));
TRY
{
// Build the creation string for access without DSN
strSql.Format(_T("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=/%s/;DBQ=%s"),
((CDR12Dlg*)AfxGetMainWnd())->sExcelDriver,
((CDR12Dlg*)AfxGetMainWnd())->sTab1CurveFile.strExcelFilePath,
((CDR12Dlg*)AfxGetMainWnd())->sTab1CurveFile.strExcelFilePath);
// Create the database (i.e. Excel sheet)
if( ((CDR12Dlg*)AfxGetMainWnd())->database.OpenEx(strSql,CDatabase::noOdbcDialog) )
{
((CDR12Dlg*)AfxGetMainWnd())->sTab1CurveFile.strExcelTableName.Empty();
((CDR12Dlg*)AfxGetMainWnd())->sTab1CurveFile.strExcelTableName.Format(_T("%s_%s"),
((CDR12Dlg*)AfxGetMainWnd())->sTab1CurveFile.strDevID,
((CDR12Dlg*)AfxGetMainWnd())->sTab1CurveFile.strDevModel);
CString strSqlField;
strSqlField.Format(_T("RecvTime TEXT,"));
for(int loop=1; loop<=theApp.nTab1TestPointNumber; loop++)
{
if(loop == theApp.nTab1TestPointNumber)
{
strSqlField.AppendFormat(_T("TestPoint%d NUMBER"), loop);
} else {
strSqlField.AppendFormat(_T("TestPoint%d NUMBER,"), loop);
}
}
// Create table structure
strSql.Format(_T("CREATE TABLE \"%s\" (%s)"),
((CDR12Dlg*)AfxGetMainWnd())->sTab1CurveFile.strExcelTableName,
strSqlField);
((CDR12Dlg*)AfxGetMainWnd())->database.ExecuteSQL(strSql);
}
// Close database
((CDR12Dlg*)AfxGetMainWnd())->database.Close();
}
CATCH_ALL(e)
{
TRACE1("Driver not installed: %s\n",((CDR12Dlg*)AfxGetMainWnd())->sExcelDriver);
}
END_CATCH_ALL;
好的,上面的代码在Excel 2003中运行良好,但我更改了代码。
sExcelDriver = _T("MICROSOFT EXCEL DRIVER (*.xls)");
替换
sExcelDriver = _T("MICROSOFT EXCEL DRIVER (*.xlsx)");
或
sExcelDriver = _T("MICROSOFT EXCEL DRIVER (*.xls, xlsx)");
并保存为.xlsx文件
但结果会显示“未安装驱动程序”
我尝试安装链接excel 2007的AccessDatabaseEngine,
如何更改代码以访问excel 2007文件。
答案 0 :(得分:1)
好的,我找到了解决方案,
在windows7控制面板 - &gt;系统管理工具 - &gt;数据源(ODBC)
中选择标签
的“用户数据源名称”找到Excel文件字段,并在代码中找到sExcelDriver CString,
必须匹配驱动程序字符串包括空白和长度。
如下代码
sExcelDriver = _T("MICROSOFT EXCEL DRIVER (*.xls, *.xlsx, *.xlsm, *.xlsb)");