Home > Software engineering >  MFC how to use the Excel has read in data
MFC how to use the Excel has read in data

Time:09-18

Have used the corresponding program, will specify Excel spreadsheet to read, but now want to use this data, the idea is to edit a function, but always stop working, and in the process of debugging himself MFC just contact, seek help from bosses, below attached source code,
Open the Excel code
Int DuQu (int, int j)
{
//the file path


BOOL isOpen=TRUE;//whether open (or save)
Cstrings defaultDir=L "E: \ ";//the default file path
Cstrings fileName=L \ \ "signal data tables. XLSX";//the default file name
//cstrings filter=L "file (*.doc; *. PPT; *. XLSX) | *. Doc; *. PPT; *. XLSX | | ";//the type of file filter
//CFileDialog openFileDlg (isOpen defaultDir, fileName, OFN_HIDEREADONLY | OFN_READONLY, filter, NULL);
//openFileDlg. GetOFN (.) lpstrInitialDir=L "E: \ \ \ \ thesis signal data tables. XLSX";
.//the INT_PTR result=openFileDlg DoModal ();
Cstrings filePath=defaultDir + "\ \ signal data tables. XLSX";


COleVariant covOptional ((long) DISP_E_PARAMNOTFOUND VT_ERROR);


Books=app. Get_Workbooks ();
//open the Excel, of which the pathname for the path of the Excel table name
LpDisp=books. The Open (filePath, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional);
Book. AttachDispatch (lpDisp);
Sheets.=the book get_Worksheets ();
Sheet=sheets. Get_Item (COleVariant ((short) 1));

Cstrings CellName;
//to get any cell values
CellName. The Format (_T (" c % d) ", I, j);//A - Z 65-90
The range=sheet. Get_Range (COleVariant (CellName), COleVariant (CellName));
//get the content of the cell
COleVariant rValue;
RValue=https://bbs.csdn.net/topics/COleVariant (range. Get_Value2 ());
RValue. ChangeType (VT_I4);
Int A;
A=rValue. IntVal;
//book put_Saved (TRUE);
//app. The Quit ();
Return A;
};


Want to use this data can be directly call this function?

CodePudding user response:

Does not recommend using this function, you can't read a data will open a excel, very slow, you can define a function, read all the data are one-off,

When debugging to which stop?

CodePudding user response:

Now I use excel to read and write code, the hope can help you
Bool CDlgMakeBatchTitle: : readExcelData (cstrings strFileName)
{
//TODO: Add your the control notification handler code here
if( ! App. CreateDispatch (" Excel. Application "))
{
CommonFile: : AddSystemLog (" readExcelData function error, unable to create Excel application!" );
MessageBox (" unable to create Excel application!" );
return false;
}

COleVariant covOptional ((long) DISP_E_PARAMNOTFOUND VT_ERROR);

///////////////////////////////////reading excel test/////////////////////////////////////////////////////////////
Books=app. Get_Workbooks ();
//books. AttachDispatch (app. Get_Workbooks () function, as well as the above
//open the Excel, of which the pathname for the path of the Excel table name
LpDisp=books. The Open (strFileName, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional);
Book. AttachDispatch (lpDisp);
Sheets.=the book get_Worksheets ();
//sheets. AttachDispatch (book. Get_Sheets (), true);
Sheet=sheets. Get_Item (COleVariant ((short) 1));
//sheet. AttachDispatch (sheets. Get_Item (COleVariant ((short) 1)), TRUE);

//get the execl data rows and columns
CRange usedRange;
CRange mRange;
UsedRange. AttachDispatch (sheet. Get_UsedRange ());
MRange. AttachDispatch (usedRange. Get_Rows (), true);
Int countRow=mRange. Get_Count ();
//mRange AttachDispatch (usedRange get_Columns (), true);
//int countColumn=mRange. Get_Count ();
UsedRange. ReleaseDispatch ();
MRange. ReleaseDispatch ();
//if the interface by the number of lines of the input is greater than the excel parameter data of the actual number of lines is take the actual data lines
If (m_nEndRow & gt; CountRow)
{
M_nEndRow=countRow;
}

M_sumRow=m_nEndRow - m_nStartRow;

//read the value of the first cell
//range. AttachDispatch (sheet. Get_Cells ());
Int cloumn=2;
Cstrings strTemp (" ");
COleVariant vResult;

//rows and columns can fill in the follow-up in the interface, prevent infinite loop read error
While (m_nStartRow & lt; M_nEndRow)
{
While (true)
{
Range. AttachDispatch (sheet. Get_Cells ());
Range. AttachDispatch (range. Get_Item (COleVariant ((long) m_nStartRow), COleVariant ((long) cloumn)). PdispVal);
VResult=range. Get_Value2 ();

If (vResult. N==VT_BSTR)//string
{
StrTemp=vResult. BstrVal;
}
Else if (vResult. N==VT_R8)//8 byte number
{
If (4==cloumn)
{
//plot data there is no decimal point
StrTemp. The Format (_T (" % 0.0 f), vResult. DblVal);
}
The else
{
StrTemp. The Format (_T (" % 0.2 f), vResult. DblVal);
}

}
The else
{
StrTemp="";
}

If (strTemp. GetLength () & lt;=0)
{
break;
}

If (2==cloumn)
{
M_map_Every_info [" household name "]=strTemp;
}
Else if (3==cloumn)
{
M_map_Every_info [" family members "]=strTemp;
}
Else if (4==cloumn)
{
M_map_Every_info [" plot number "]=strTemp;
}
Else if (5==cloumn)
{
M_map_Every_info [" contracting area "]=strTemp;
}
Else if (6==cloumn)
{
M_map_Every_info [" contract term "]=strTemp;
}
Else if (7==cloumn)
{
M_map_Every_info [" certificate number "]=strTemp;
}
Else if (8==cloumn)
{
M_map_Every_info [" id number "]=strTemp;
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related