请教高手们,ClistCtl控件(列表控件)的内容如何导入到Excel中以便整理打印?回复都给分! 请教高手们,ClistCtl控件(列表控件)的内容如何导入到Excel中以便整理打印?这个函数怎么编啊?能贴出来么?感激涕零!!!回答都给分!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用OLE操作excel参考 http://www.cppblog.com/greatws/archive/2008/09/21/62423.html CExcelEx::CExcelEx(){ m_pApplication = new _Application(); m_pWorkbook = new _Workbook(); m_pWorksheet = new _Worksheet();}CExcelEx::~CExcelEx(){ Close(); if (m_pApplication->m_lpDispatch != NULL) { m_pApplication->Quit(); m_pApplication->ReleaseDispatch(); m_pApplication->m_lpDispatch = NULL; } delete m_pWorksheet; delete m_pWorkbook; delete m_pApplication; return; }BOOL CExcelEx::StartExcel(){ m_pApplication->CreateDispatch(_T("Excel.Application")); return (m_pApplication->m_lpDispatch != NULL);}BOOL CExcelEx::IsOpen() const{ return (m_pApplication->m_lpDispatch != NULL && m_pWorkbook->m_lpDispatch != NULL);}BOOL CExcelEx::Open(LPCTSTR lpszName){ if (lpszName != NULL) { if (!PathFileExists(lpszName)) { return FALSE; } } if (!m_pApplication->m_lpDispatch) { return FALSE; } CString strName = lpszName != NULL ? lpszName : _T(""); if (m_pWorkbook->m_lpDispatch != NULL) { //CString strOldName = m_Workbook.GetFullName(); //if (strOldName == strName) if (m_strFileName == strName) { return TRUE; } m_strFileName.Empty(); m_pWorkbook->ReleaseDispatch(); } Workbooks workbooks; workbooks.AttachDispatch(m_pApplication->GetWorkbooks()); m_pWorkbook->AttachDispatch(workbooks.Add(_variant_t(strName))); if (!m_pWorkbook->m_lpDispatch) { CString strTip; strTip.Format(_T("不能打开文档%s!"), strName); ::AfxMessageBox(strTip); return FALSE; } m_strFileName = lpszName; return TRUE;}BOOL CExcelEx::Close(){ if (!IsOpen()) { return TRUE; } _variant_t vFalse((short)FALSE); _variant_t vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); m_pWorkbook->Close(vFalse, _variant_t(_T("")), vOptional); if (m_pWorksheet->m_lpDispatch != NULL) { m_pWorksheet->ReleaseDispatch(); m_pWorksheet->m_lpDispatch = NULL; } m_pWorkbook->ReleaseDispatch(); m_pWorkbook->m_lpDispatch = NULL; return TRUE;}BOOL CExcelEx::SaveAs(LPCTSTR lpszName){ if (!lpszName) { return FALSE; } if (!IsOpen()) { return FALSE; } _variant_t vFalse((short)FALSE); _variant_t vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); if (m_strFileName != lpszName) { ::DeleteFile(lpszName); MakeDir(lpszName); m_pWorkbook->SaveAs(_variant_t(lpszName), vOptional, vOptional, vOptional, vOptional, vOptional, 0, vOptional, vFalse, vOptional, vOptional); } else { m_pWorkbook->Save(); } return TRUE;}BOOL CExcelEx::SetActiveSheet(long nIndex){ if (!IsOpen()) { return FALSE; } if (m_pWorksheet->m_lpDispatch != NULL) { if (m_pWorksheet->GetIndex() == nIndex) { return TRUE; } else { m_pWorksheet->ReleaseDispatch(); } } Worksheets worksheets = m_pWorkbook->GetWorksheets(); m_pWorksheet->AttachDispatch(worksheets.GetItem(_variant_t(nIndex))); if (m_pWorksheet->m_lpDispatch != NULL) { m_pWorksheet->Activate(); } return (m_pWorksheet->m_lpDispatch != NULL);}BOOL CExcelEx::GetActiveSheet(long& nIndex){ if (!IsOpen()) { return FALSE; } if (m_pWorksheet->m_lpDispatch == NULL) {// need modifying return FALSE; } nIndex = m_pWorksheet->GetIndex(); return TRUE;}BOOL CExcelEx::SetSheetName(long nIndex, LPCTSTR lpszName){ BOOL bRet = FALSE; long nSheetIndex = -1; GetActiveSheet(nSheetIndex); if (SetActiveSheet(nIndex)) { bRet = TRUE; m_pWorksheet->SetName(lpszName); } SetActiveSheet(nSheetIndex); return bRet;}int CExcelEx::GetMergeRange(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, Range& range){ if (!lpszCellBeg || !lpszCellEnd) { return -1; } if (!IsOpen() || m_pWorksheet->m_lpDispatch == NULL) { return -1; } Range rangeCell; rangeCell = m_pWorksheet->GetRange(_variant_t(lpszCellBeg), _variant_t(lpszCellEnd)); if (rangeCell.m_lpDispatch == NULL) { return -1; } _variant_t vTmp = rangeCell.GetMergeCells(); if (vTmp.boolVal == -1) { rangeCell = rangeCell.GetItem(_variant_t(1L), _variant_t(1L)).pdispVal; rangeCell = rangeCell.GetMergeArea(); range = rangeCell.DetachDispatch(); return 0; } else if (vTmp.boolVal == 0) { return 1; } else { return 2; }}BOOL CExcelEx::SetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, const CString& strValue){ Range range; int nRet = GetMergeRange(lpszCellBeg, lpszCellEnd, range); if (nRet < 0) { return FALSE; } else if (nRet == 0) { } else { //would you merge them? range = m_pWorksheet->GetRange(_variant_t(lpszCellBeg), _variant_t(lpszCellBeg)); } //range.SetHorizontalAlignment(_variant_t(3L)); //range.SetVerticalAlignment(_variant_t(2L)); range.SetValue(_variant_t(strValue)); return TRUE;}BOOL CExcelEx::SetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, double fValue){ CString strValue; strValue.Format(_T("%g"), fValue); return SetRangeValue(lpszCellBeg, lpszCellEnd, strValue);}BOOL CExcelEx::SetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, long nValue){ CString strValue; strValue.Format(_T("%d"), nValue); return SetRangeValue(lpszCellBeg, lpszCellEnd, strValue);}BOOL CExcelEx::GetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, CString& strValue){ Range range; _variant_t vResult; int nRet = GetMergeRange(lpszCellBeg, lpszCellEnd, range); if (nRet < 0) { } else if (nRet == 0) { vResult = range.GetValue(); try { strValue = (LPCTSTR)(_bstr_t)vResult; return TRUE; } catch (_com_error& ) { COleSafeArray sa(vResult); long index[2] = {1L, 1L}; sa.GetElement(index, &vResult); } } else { if (_tcscmp(lpszCellBeg, lpszCellEnd) != 0) { return FALSE; } range = m_pWorksheet->GetRange(_variant_t(lpszCellBeg), _variant_t(lpszCellEnd)); } try { strValue = (LPCTSTR)(_bstr_t)vResult; return TRUE; } catch (_com_error& err) { ::AfxMessageBox((LPCTSTR)(err.Description())); return FALSE; }}BOOL CExcelEx::GetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, double& fValue){ Range range; _variant_t vResult; int nRet = GetMergeRange(lpszCellBeg, lpszCellEnd, range); if (nRet < 0) { } else if (nRet == 0) { vResult = range.GetValue(); try { fValue = (double)vResult; return TRUE; } catch (_com_error& ) { COleSafeArray sa(vResult); long index[2] = {1L, 1L}; sa.GetElement(index, &vResult); } } else { if (_tcscmp(lpszCellBeg, lpszCellEnd) != 0) { return FALSE; } range = m_pWorksheet->GetRange(_variant_t(lpszCellBeg), _variant_t(lpszCellEnd)); } try { fValue = (double)vResult; return TRUE; } catch (_com_error& err) { ::AfxMessageBox((LPCTSTR)(err.Description())); return FALSE; }}BOOL CExcelEx::GetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, long& nValue){ Range range; _variant_t vResult; int nRet = GetMergeRange(lpszCellBeg, lpszCellEnd, range); if (nRet < 0) { } else if (nRet == 0) { vResult = range.GetValue(); try { nValue = (long)vResult; return TRUE; } catch (_com_error& ) { COleSafeArray sa(vResult); long index[2] = {1L, 1L}; sa.GetElement(index, &vResult); } } else { if (_tcscmp(lpszCellBeg, lpszCellEnd) != 0) { return FALSE; } range = m_pWorksheet->GetRange(_variant_t(lpszCellBeg), _variant_t(lpszCellEnd)); } try { nValue = (long)vResult; return TRUE; } catch (_com_error& err) { ::AfxMessageBox((LPCTSTR)(err.Description())); return FALSE; }} #include "excel9.h"class CExcelEx {public: CExcelEx(); virtual ~CExcelEx();public: virtual BOOL StartExcel(); virtual BOOL IsOpen() const; virtual BOOL Open(LPCTSTR lpszName = NULL); virtual BOOL SaveAs(LPCTSTR lpszName = NULL); virtual BOOL Close(); virtual BOOL SetActiveSheet(long nIndex); virtual BOOL GetActiveSheet(long& nIndex); virtual BOOL SetSheetName(long nIndex, LPCTSTR lpszName); virtual BOOL SetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, const CString& strValue); virtual BOOL SetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, double fValue); virtual BOOL SetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, long nValue); virtual BOOL GetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, CString& strValue); virtual BOOL GetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, double& fValue); virtual BOOL GetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, long& nValue);protected: int GetMergeRange(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, Range& range);protected: _Application* m_pApplication; _Workbook* m_pWorkbook; _Worksheet* m_pWorksheet; CString m_strFileName;}; 保存成txt文件,然后后缀改成xls。字串间用tab隔开! excel之间的数据是用"\t"分开的excel显示如下内容:c1 c2 c3h1 h2 h3放入字符串是:"c1\tc2\t\c3\r\nh1\th2\th3\t" 类似这样: FILE * pf; pf = fopen("C:\\1.xls", "a"); if(NULL != pf) { fprintf(pf, "111\t222"); fclose(pf); } 可以使用ado,创建excel文件,读取ClistCtl控件中的内容,写入对应的单元格。这种方法通用,简单,方便,高效。 http://www.vckbase.com/document/viewdoc/?id=815参考一下 1,在程序中编写代码把控件中的数据保存为*.txt文件格式,再改文件后缀名为excel2,有专门的转换excel的源代码,你可搜索一下,直接拿来用 http://topic.csdn.net/t/20050718/15/4151984.html类似的问题 VC怎么显示在线地图? 用MFC的Dialog写个登录界面对话框的程序 CMFCPropertyGridCtrl属性控件能否显示CHotKeyCtrl控件? 可以帮我看一下吗?——MFC关闭线程的问题!谢谢! 请问, 在OnDestroy时调用PostThreadMessage向ui线程发送消息好象不起作用, 为什么呢 类怎么加入多线程?在线等 马上解帖 小弟有一数据库删除问题?请各位指教!在线等!!!!!!!!! VC程序中如何加载一个NE格式的EXE中的资源? 急:创建线程问题 研究DICOM的同行们能互相沟通么? VC不小心卸载一个控件后出现的问题 MFC串口通信,读串口的一个问题
CExcelEx::CExcelEx()
{
m_pApplication = new _Application();
m_pWorkbook = new _Workbook();
m_pWorksheet = new _Worksheet();
}CExcelEx::~CExcelEx()
{
Close();
if (m_pApplication->m_lpDispatch != NULL)
{
m_pApplication->Quit();
m_pApplication->ReleaseDispatch();
m_pApplication->m_lpDispatch = NULL;
}
delete m_pWorksheet;
delete m_pWorkbook;
delete m_pApplication;
return;
}BOOL CExcelEx::StartExcel()
{
m_pApplication->CreateDispatch(_T("Excel.Application"));
return (m_pApplication->m_lpDispatch != NULL);
}BOOL CExcelEx::IsOpen() const
{
return (m_pApplication->m_lpDispatch != NULL && m_pWorkbook->m_lpDispatch != NULL);
}BOOL CExcelEx::Open(LPCTSTR lpszName)
{
if (lpszName != NULL)
{
if (!PathFileExists(lpszName))
{
return FALSE;
}
}
if (!m_pApplication->m_lpDispatch)
{
return FALSE;
} CString strName = lpszName != NULL ? lpszName : _T("");
if (m_pWorkbook->m_lpDispatch != NULL)
{
//CString strOldName = m_Workbook.GetFullName();
//if (strOldName == strName)
if (m_strFileName == strName)
{
return TRUE;
}
m_strFileName.Empty();
m_pWorkbook->ReleaseDispatch();
}
Workbooks workbooks;
workbooks.AttachDispatch(m_pApplication->GetWorkbooks());
m_pWorkbook->AttachDispatch(workbooks.Add(_variant_t(strName)));
if (!m_pWorkbook->m_lpDispatch)
{
CString strTip;
strTip.Format(_T("不能打开文档%s!"), strName);
::AfxMessageBox(strTip);
return FALSE;
}
m_strFileName = lpszName;
return TRUE;
}BOOL CExcelEx::Close()
{
if (!IsOpen())
{
return TRUE;
}
_variant_t vFalse((short)FALSE);
_variant_t vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
m_pWorkbook->Close(vFalse, _variant_t(_T("")), vOptional);
if (m_pWorksheet->m_lpDispatch != NULL)
{
m_pWorksheet->ReleaseDispatch();
m_pWorksheet->m_lpDispatch = NULL;
}
m_pWorkbook->ReleaseDispatch();
m_pWorkbook->m_lpDispatch = NULL;
return TRUE;
}BOOL CExcelEx::SaveAs(LPCTSTR lpszName)
{
if (!lpszName)
{
return FALSE;
}
if (!IsOpen())
{
return FALSE;
} _variant_t vFalse((short)FALSE);
_variant_t vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if (m_strFileName != lpszName)
{
::DeleteFile(lpszName);
MakeDir(lpszName);
m_pWorkbook->SaveAs(_variant_t(lpszName),
vOptional,
vOptional,
vOptional,
vOptional,
vOptional,
0,
vOptional,
vFalse,
vOptional,
vOptional);
}
else
{
m_pWorkbook->Save();
}
return TRUE;
}BOOL CExcelEx::SetActiveSheet(long nIndex)
{
if (!IsOpen())
{
return FALSE;
}
if (m_pWorksheet->m_lpDispatch != NULL)
{
if (m_pWorksheet->GetIndex() == nIndex)
{
return TRUE;
}
else
{
m_pWorksheet->ReleaseDispatch();
}
} Worksheets worksheets = m_pWorkbook->GetWorksheets();
m_pWorksheet->AttachDispatch(worksheets.GetItem(_variant_t(nIndex)));
if (m_pWorksheet->m_lpDispatch != NULL)
{
m_pWorksheet->Activate();
}
return (m_pWorksheet->m_lpDispatch != NULL);
}BOOL CExcelEx::GetActiveSheet(long& nIndex)
{
if (!IsOpen())
{
return FALSE;
}
if (m_pWorksheet->m_lpDispatch == NULL)
{// need modifying
return FALSE;
}
nIndex = m_pWorksheet->GetIndex();
return TRUE;
}BOOL CExcelEx::SetSheetName(long nIndex, LPCTSTR lpszName)
{
BOOL bRet = FALSE;
long nSheetIndex = -1;
GetActiveSheet(nSheetIndex);
if (SetActiveSheet(nIndex))
{
bRet = TRUE;
m_pWorksheet->SetName(lpszName);
}
SetActiveSheet(nSheetIndex);
return bRet;
}int CExcelEx::GetMergeRange(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, Range& range)
{
if (!lpszCellBeg || !lpszCellEnd)
{
return -1;
} if (!IsOpen() || m_pWorksheet->m_lpDispatch == NULL)
{
return -1;
} Range rangeCell;
rangeCell = m_pWorksheet->GetRange(_variant_t(lpszCellBeg), _variant_t(lpszCellEnd));
if (rangeCell.m_lpDispatch == NULL)
{
return -1;
}
_variant_t vTmp = rangeCell.GetMergeCells();
if (vTmp.boolVal == -1)
{
rangeCell = rangeCell.GetItem(_variant_t(1L), _variant_t(1L)).pdispVal;
rangeCell = rangeCell.GetMergeArea();
range = rangeCell.DetachDispatch();
return 0;
}
else if (vTmp.boolVal == 0)
{
return 1;
}
else
{
return 2;
}
}BOOL CExcelEx::SetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, const CString& strValue)
{
Range range;
int nRet = GetMergeRange(lpszCellBeg, lpszCellEnd, range);
if (nRet < 0)
{
return FALSE;
}
else if (nRet == 0)
{ }
else
{
//would you merge them?
range = m_pWorksheet->GetRange(_variant_t(lpszCellBeg), _variant_t(lpszCellBeg));
}
//range.SetHorizontalAlignment(_variant_t(3L));
//range.SetVerticalAlignment(_variant_t(2L));
range.SetValue(_variant_t(strValue));
return TRUE;
}
BOOL CExcelEx::SetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, double fValue)
{
CString strValue;
strValue.Format(_T("%g"), fValue);
return SetRangeValue(lpszCellBeg, lpszCellEnd, strValue);
}BOOL CExcelEx::SetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, long nValue)
{
CString strValue;
strValue.Format(_T("%d"), nValue);
return SetRangeValue(lpszCellBeg, lpszCellEnd, strValue);
}BOOL CExcelEx::GetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, CString& strValue)
{
Range range;
_variant_t vResult;
int nRet = GetMergeRange(lpszCellBeg, lpszCellEnd, range);
if (nRet < 0)
{
}
else if (nRet == 0)
{
vResult = range.GetValue();
try
{
strValue = (LPCTSTR)(_bstr_t)vResult;
return TRUE;
}
catch (_com_error& )
{
COleSafeArray sa(vResult);
long index[2] = {1L, 1L};
sa.GetElement(index, &vResult);
}
}
else
{
if (_tcscmp(lpszCellBeg, lpszCellEnd) != 0)
{
return FALSE;
}
range = m_pWorksheet->GetRange(_variant_t(lpszCellBeg), _variant_t(lpszCellEnd));
}
try
{
strValue = (LPCTSTR)(_bstr_t)vResult;
return TRUE;
}
catch (_com_error& err)
{
::AfxMessageBox((LPCTSTR)(err.Description()));
return FALSE;
}
}BOOL CExcelEx::GetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, double& fValue)
{
Range range;
_variant_t vResult;
int nRet = GetMergeRange(lpszCellBeg, lpszCellEnd, range);
if (nRet < 0)
{
}
else if (nRet == 0)
{
vResult = range.GetValue();
try
{
fValue = (double)vResult;
return TRUE;
}
catch (_com_error& )
{
COleSafeArray sa(vResult);
long index[2] = {1L, 1L};
sa.GetElement(index, &vResult);
}
}
else
{
if (_tcscmp(lpszCellBeg, lpszCellEnd) != 0)
{
return FALSE;
}
range = m_pWorksheet->GetRange(_variant_t(lpszCellBeg), _variant_t(lpszCellEnd));
}
try
{
fValue = (double)vResult;
return TRUE;
}
catch (_com_error& err)
{
::AfxMessageBox((LPCTSTR)(err.Description()));
return FALSE;
}
}BOOL CExcelEx::GetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, long& nValue)
{
Range range;
_variant_t vResult;
int nRet = GetMergeRange(lpszCellBeg, lpszCellEnd, range);
if (nRet < 0)
{
}
else if (nRet == 0)
{
vResult = range.GetValue();
try
{
nValue = (long)vResult;
return TRUE;
}
catch (_com_error& )
{
COleSafeArray sa(vResult);
long index[2] = {1L, 1L};
sa.GetElement(index, &vResult);
}
}
else
{
if (_tcscmp(lpszCellBeg, lpszCellEnd) != 0)
{
return FALSE;
}
range = m_pWorksheet->GetRange(_variant_t(lpszCellBeg), _variant_t(lpszCellEnd));
}
try
{
nValue = (long)vResult;
return TRUE;
}
catch (_com_error& err)
{
::AfxMessageBox((LPCTSTR)(err.Description()));
return FALSE;
}
}
{
public:
CExcelEx();
virtual ~CExcelEx();
public:
virtual BOOL StartExcel();
virtual BOOL IsOpen() const;
virtual BOOL Open(LPCTSTR lpszName = NULL);
virtual BOOL SaveAs(LPCTSTR lpszName = NULL);
virtual BOOL Close(); virtual BOOL SetActiveSheet(long nIndex);
virtual BOOL GetActiveSheet(long& nIndex);
virtual BOOL SetSheetName(long nIndex, LPCTSTR lpszName); virtual BOOL SetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, const CString& strValue);
virtual BOOL SetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, double fValue);
virtual BOOL SetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, long nValue); virtual BOOL GetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, CString& strValue);
virtual BOOL GetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, double& fValue);
virtual BOOL GetRangeValue(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, long& nValue);protected:
int GetMergeRange(LPCTSTR lpszCellBeg, LPCTSTR lpszCellEnd, Range& range);
protected:
_Application* m_pApplication;
_Workbook* m_pWorkbook;
_Worksheet* m_pWorksheet;
CString m_strFileName;
};
字串间用tab隔开!
excel显示如下内容:
c1 c2 c3
h1 h2 h3放入字符串是:
"c1\tc2\t\c3\r\nh1\th2\th3\t"
FILE * pf;
pf = fopen("C:\\1.xls", "a");
if(NULL != pf)
{
fprintf(pf, "111\t222");
fclose(pf);
}
参考一下
2,有专门的转换excel的源代码,你可搜索一下,直接拿来用
类似的问题