MFC List导出到XLS
在实际使用时,将List中的数据导出到XLS中是一种很常用的数据导出做法。
先建立一个XLS文件,然后 _wfopen 打开文件,用 fwrite 写入,所以先获取表的数据就好。
当然只要有数据导出哪里的数据都一样。
获取列表数据并导出
BOOL xxxx::ExportCurrentLog(CListCtrl* pList, CHeaderCtrl* pHeader)
{
// TODO: 在此添加控件通知处理程序代码
if (pList->GetItemCount() <= 0)
{
CString strMsg = _T("导出失败,当前页没有数据!");
GETResStringSafe(IDS_STRING_EXPORT_FAILED_NEW, strMsg);
CErrorDlg dlg(strMsg);
dlg.DoModal();
return FALSE;
}
//保存为 Excel 文件, 返回文件路径
CString strDbLogExporterPath;
TCHAR BASED_CODE szFilter[] = _T("Excel File (*.csv)|*.csv|");
CString strText = L"New_FileInfo";
CFileDialog fileDlg(false, _T("*.csv"), strText, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, szFilter, this);
if (fileDlg.DoModal() != IDOK)
{
return FALSE;
}
//打开创建的文件
USES_CONVERSION;
CString strFile = fileDlg.GetPathName();
BOOL bALL = FALSE;
//CXTListCtrl *pList = &m_clientXTList;
FILE *pFile = NULL;
pFile = _wfopen(strFile.GetBuffer(), _T("w+"));
if (pFile == NULL)
{
CString strMsg = _T("导出失败:创建文件失败或文件已经被打开!");
//添加个提示
return FALSE;
}
//获取表头先写入
int nCount = pHeader->GetItemCount();
LPINT pnOrder = (LPINT) malloc(nCount * sizeof(int)); //给表头分一段内存
ASSERT(pnOrder != NULL);
pHeader->GetOrderArray(pnOrder, nCount);
CString strLine;
CString strTok =_T(",");
HDITEM hdi;
enum { sizeOfBuffer = 256 };
TCHAR lpBuffer[sizeOfBuffer];
bool fFound = false;
hdi.mask = HDI_TEXT;
hdi.pszText = lpBuffer;
hdi.cchTextMax = sizeOfBuffer;
for (int i = 0;!fFound && (i < nCount);i++)
{
if (pList->GetColumnWidth(pnOrder[i])==0)
{
continue;
}
pHeader->GetItem(pnOrder[i], &hdi);
strLine += hdi.pszText;
strLine += strTok;
}
strLine += _T("\n");
if (pList == NULL)
{
free(pnOrder);
return FALSE;
}
char *pText = W2A(strLine.GetBuffer());
fwrite( pText, sizeof(char), strlen(pText),pFile);
CString strContext =_T("");
//写入每项数据
for (int index = 0; index<pList->GetItemCount(); index++)
{
strLine = _T("");
for (int i=0; i< pHeader->GetItemCount(); i++)
{
if (pList->GetColumnWidth(pnOrder[i])==0)
{
continue;
}
CString strLog = pList->GetItemText(index, pnOrder[i]);
strLog.Replace(_T("\r\n"),_T(" "));
strLog.Replace(_T("\""),_T("\"\""));
strContext.Format(_T("\"%s\""),strLog);
strLine += strContext;
strLine += strTok;
}
strLine += _T("\n");
CW2A texta(strLine.GetBuffer());
char *pText=texta;
fwrite( pText, sizeof(char), strlen(pText),pFile);
}
fclose(pFile);
free(pnOrder);
return TRUE;
}
导入任意数据
BOOL ExportCurrentLog(vector<CString> &vecInfo)
{
if (vecInfo.empty())
{
return FALSE;
}
//保存为 Excel 文件, 返回文件路径
CString strDbLogExporterPath;
TCHAR BASED_CODE szFilter[] = _T("Excel File (*.csv)|*.csv|");
CString strText = L"New_FileInfo";
CFileDialog fileDlg(false, _T("*.csv"), strText, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, szFilter, this);
if (fileDlg.DoModal() != IDOK)
{
return FALSE;
}
//打开创建的文件
USES_CONVERSION;
CString strFile = fileDlg.GetPathName();
BOOL bALL = FALSE;
//CXTListCtrl *pList = &m_clientXTList;
FILE *pFile = NULL;
pFile = _wfopen(strFile.GetBuffer(), _T("w+"));
if (pFile == NULL)
{
CString strMsg = _T("导出失败:创建文件失败或文件已经被打开!");
GETResStringSafe(IDS_STING_EXPORT_FILE_ERROR,strMsg);
CErrorDlg dlg(strMsg);
dlg.DoModal();
return FALSE;
}
//获取表头先写入
int nCount = pHeader->GetItemCount();
LPINT pnOrder = (LPINT) malloc(nCount * sizeof(int)); //给表头分一段内存
ASSERT(pnOrder != NULL);
pHeader->GetOrderArray(pnOrder, nCount);
CString strLine;
CString strTok =_T(",");
HDITEM hdi;
enum { sizeOfBuffer = 256 };
TCHAR lpBuffer[sizeOfBuffer];
bool fFound = false;
hdi.mask = HDI_TEXT;
hdi.pszText = lpBuffer;
hdi.cchTextMax = sizeOfBuffer;
CString strContext =_T("");
//写入每项数据
vector<CString>::iterator iterInfo = vecInfo.begin();
for (; iterInfo != vecInfo.end(); iterInfo++)
{
strLine = _T("");
CString strLog = *iterInfo;
strLog.Replace(_T("\r\n"),_T(" "));
strLog.Replace(_T("\""),_T("\"\""));
strContext.Format(_T("\"%s\""),strLog);
strLine += strContext;
strLine += strTok;
}
strLine += _T("\n");
CW2A texta(strLine.GetBuffer());
char *pText=texta;
fwrite( pText, sizeof(char), strlen(pText),pFile);
fclose(pFile);
free(pnOrder);
return TRUE;
}