Home > Software engineering >  MFC exported list control data to Excel
MFC exported list control data to Excel

Time:02-11

Based on MFC written VS2010 software I need to List the data import to Excel in the Control, the code can use baidu, debugging for a long time also don't know where the problem is, you teach, related routines is best, I'll attach my code, compiled through, just can't save

Void CPort16Dlg: : OnBnClickedSave ()
{
//first determines whether there is record in the list box
If (m_list GetItemCount () & lt;=0)
{
AfxMessageBox (_T (" no data to save!" ));
The return;
}
//open the save as dialog
CFileDialog DLG (FALSE, _T (" *. XLS "), _T (" FileList "),
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T (" (*. XLS) | *. XLS | | "));
DLG. M_ofn. LpstrTitle=_T (" save file list as ");

If (DLG. DoModal ()!=IDOK)
The return;
Cstrings strFilePath;
StrFilePath=DLG. GetPathName ();//get the file path
//determine whether the file already exists, exists reconstruction are removed
DWORD dwRe=GetFileAttributes (strFilePath);
If (dwRe!=(DWORD) - 1)
{
The DeleteFile (strFilePath);
}
CDatabase database;
Cstrings sDriver=_T (" MICROSOFT EXCEL DRIVER (*. XLS) ");//Excel drive
Cstrings sSql strInsert;

TRY
{
//create access string
SSql. The Format (_T (" DRIVER={% s}; DSN=' '; FIRSTROWHASNAMES=1; READONLY=FALSE; CREATE_DB=\ \ "% s"; DBQ=% s ")
, sDriver strFilePath strFilePath);
//create the database (both Excel spreadsheet file)
If (database. OpenEx (sSql CDatabase: : noOdbcDialog))
{
//don't get column frame to the total number of columns
Int iColumnNum iRowCount;
The LVCOLUMN lvCol;
Cstrings strColName;//used to save the column header name
Int I, j;//column, line cycle parameters

IColumnNum=m_list. GetHeaderCtrl () - & gt; GetItemCount ();
IRowCount=m_list. GetItemCount ();

SSql="CREATE TABLE DSO_DX (";
StrInsert="INSERT INTODSO_DX (";
//get the column header name
LvCol. Mask=LVCF_TEXT;//that LVCOLUMN pszText parameters in variable effective
LvCol. CchTextMax=32;//the PSZ parameter points to the size of the string
LvCol. PszText=strColName. GetBuffer (32);//points to the actual storage location of the string
//the above three parameter is set to pass GetColumn () function to obtain the name of the column header
For (I=0; I & lt; IColumnNum; I++)
{
if (! (m_list GetColumn (I, & amp; LvCol)))
The return;
If (I & lt; IColumnNum - 1)
{
SSql=sSql + lvCol. PszText + _T (" TEXT, ");
StrInsert=strInsert + lvCol. PszText + _T (", ");
}
The else
{
SSql=sSql + lvCol. PszText + _T (" TEXT ");
StrInsert=strInsert + lvCol. PszText + _T (") VALUES (");
}
}

//to create ExCEL file
The database. The ExecuteSQL (sSql);
AfxMessageBox (_T (after success! "" ));

//cycle extraction records and insert to EXCEL in
SSql=strInsert;
Char chTemp [100].
for (j=0; J{
Memset (chTemp, 0100);
For (I=0; I{
M_list. GetItemText (j, I, _T (" chTemp "), 100);
If (i<(iColumnNum - 1))
{
SSql=sSql + _T (chTemp, "");
}
The else
{
SSql=sSql + _T (" chTemp) ");
}
}
//insert record form
The database. The ExecuteSQL (sSql);
SSql=strInsert;
}
}
//close Excel spreadsheet file
The database. The Close ();
AfxMessageBox (_T (" save for Excel file success!" ));
}
CATCH_ALL (e)
{
//error types are many, according to the need to report errors
AfxMessageBox (_T (" save failed!" ));
}
END_CATCH_ALL; */
}
  • Related