如标题所言,将ListCtrl的内容导出到Excel文件。不需要Excel环境支持!
TCHAR szFilter[] = _T("Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*||");
CFileDialog file(FALSE, _T(".xls"), _T("TransForm"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
CString strSavePath = _T(""); //!<保存路径
if(IDOK == file.DoModal())
{
strSavePath = file.GetPathName();
}
//!<导出的xls文件路径
if (strSavePath != _T(""))
{
std::string szTitle = "TransForm";
std::string szHander = "";
szHander = "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>";
szHander = szHander + "<x:ExcelWorksheet><x:Name>" + CFunLib::MakeSafeHTMLText(szTitle)
+ "</x:Name><x:WorksheetOptions><x:Print><x:ValidPrinterInfo /></x:Print></x:WorksheetOptions>";
szHander += "</x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" /><style type=\"text/css\">td {mso-number-format:''\@'';}</style></head><body><div><table><tr>";
//!<html结束符号
std::string szEnd = "</table></div></body></html>";
std::string szTR = "<tr>";
std::string szEndTR = "</tr>";
CHeaderCtrl* pHeaderCtrl = m_lstResult.GetHeaderCtrl();
int nCloumn = pHeaderCtrl->GetItemCount(); ///列数
std::fstream file;
file.open(strSavePath, std::ios_base::out);
file.write(szHander.c_str(), szHander.length());
//!<写入标题行
std::string szFiled = "<td></td><td align=\"center\" valign=\"middle\" height=\"50\"><strong>"
+ CFunLib::MakeSafeHTMLText(szTitle) + "</strong></td></tr><tr></tr>";
file.write(szFiled.c_str(), szFiled.length());
file.write(szTR.c_str(), szTR.length());
//!<根据列头写单元格
char szText[MAX_PATH] = {0};
LVCOLUMN LvColumn;
LvColumn.mask = LVCF_TEXT;
LvColumn.pszText = szText; //!<标题名称
LvColumn.cchTextMax = MAX_PATH;
for (int i = 0; i < nCloumn; ++i)
{
if(m_lstResult.GetColumnWidth(i) <= 0) continue; //!<如果列宽为0则视为隐藏
m_lstResult.GetColumn(i, &LvColumn);
szFiled = "<td>" + CFunLib::MakeSafeHTMLText(LvColumn.pszText) + "</td>";
file.write(szFiled.c_str(), szFiled.length());
}
file.write(szEndTR.c_str(), szEndTR.length());
CString strValue = _T("");
int nItemsCount = m_lstResult.GetItemCount(); //!<获取列表总行数
if (nItemsCount > 0)
{
for(int i = 0; i < nItemsCount; ++i) //!<一行一行的写,遍历行在外层循环
{
file.write(szTR.c_str(), szTR.length());
for (int j = 0; j < nCloumn; ++j)
{
if(m_lstResult.GetColumnWidth(j) <= 0) continue;
strValue = m_lstResult.GetItemText(i, j);
szFiled = "<td>" + CFunLib::MakeSafeHTMLText(strValue.operator LPCTSTR()) + "</td>";
file.write(szFiled.c_str(), szFiled.length());
}
file.write(szEndTR.c_str(), szEndTR.length());
}
file.write(szEndTR.c_str(), szEndTR.length());
}
file.close();
ShellExecute(NULL, _T("open"), strSavePath, NULL, NULL, SW_SHOW);
}
std::string MakeSafeHTMLText(const std::string& v_szContent)
{
std::string szResult = "";
for (int i = 0; i < v_szContent.length(); ++i)
{
char ch = v_szContent[i];
switch(ch)
{
case '<':
szResult += "<";
case '>':
szResult += ">";
case '&':
szResult += "&";
case '"':
szResult += """;
case '\'':
szResult += "'";
break;
default:
szResult += ch;
break;
}
}
return szResult;
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。