Domanda

Use MFC to insert data to excel 2003, I found the limit of 65536 rows,

I don't want to create another sheet to save data, so I want use excel 2007,

which extension is .xlsx.

I use follow code to get driver

((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;

Ok, above code is work well in Excel 2003, but I change the code from .

sExcelDriver = _T("MICROSOFT EXCEL DRIVER (*.xls)");

replace

sExcelDriver = _T("MICROSOFT EXCEL DRIVER (*.xlsx)");

or

sExcelDriver = _T("MICROSOFT EXCEL DRIVER (*.xls, xlsx)");

and save as .xlsx file

but the result will show me "Driver not installed"

I have try to install the AccessDatabaseEngine, which link the excel 2007,

how do I change the code to acces excel 2007 file.

È stato utile?

Soluzione

OK, I find the solution,

In windows7 control panel->system menage tool->data source(ODBC)

select the "user data source name" of tab

find the Excel file field, and the sExcelDriver CString in the code,

Must match the driver string include blank and length.

as follow code

sExcelDriver = _T("MICROSOFT EXCEL DRIVER (*.xls, *.xlsx, *.xlsm, *.xlsb)");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top