对于MFC大学根本没学过,现在突然叫自己做这么强大的功能,一阵恐惧袭来。
怎么办,上网百度、谷歌了!有些事情不是自己弄不了,只是自己懒的。
CListCtrl控件导出excel大概步骤如下:
1.首先打开vs2010,建一个项目(ExportExcel),建立完后点击右键,添加--类--MFC--TypeLib中的MFC类,到该页面点添加如下:
2.点添加完后,注意下,找到相关头文件,注释红色那行
//#import "D:\\Program Files\\Microsoft Office\\OFFICE11\\EXCEL.EXE" no_namespace
// CWorkbook 包装类
class CWorkbook : public COleDispatchDriver
{
public:
CWorkbook(){} // 调用 COleDispatchDriver 默认构造函数
CWorkbook(LPDISPATCH pDispatch) : COleDispatchDriver(pDispatch) {}
CWorkbook(const CWorkbook& dispatchSrc) : COleDispatchDriver(dispatchSrc) {}
3.注释完后,把上面添加的类的头文件加到你要实现导出excel功能的类中,如下:
#include "CApplication.h"
#include "CRange.h"
#include "CWorkbooks.h"
#include "CWorksheets.h"
#include "CWorkbook.h"
#include "CWorksheet.h"
4.实现主要代码如下:
//导出 void Cimportexcel2Dlg::OnBnClickedButton1() { CFileDialog FileDialog(FALSE,L"xls",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, _T("Microsoft Excel 2000(*.xls)|*.xls|所有文件(*.*)"),this); if(FileDialog.DoModal()!=IDOK) { return; } CString cStrFile=FileDialog.GetPathName(); //选择保存路径名称 if(::PathFileExists(cStrFile)) DeleteFile(cStrFile); //CString cStrFile = _T("E:\\myexcel.xls"); COleVariant covTrue((short)TRUE),covFalse((short)FALSE),covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR); CApplication app; //Excel程序 CWorkbooks books; //工作簿集合 CWorkbook book; //工作表 CWorksheets sheets; //工作簿集合 CWorksheet sheet; //工作表集合 CRange range; //使用区域 CoUninitialize(); book.PrintPreview(_variant_t(false)); if(CoInitialize(NULL)==S_FALSE) { MessageBox(L"初始化COM支持库失败!"); return; } if(!app.CreateDispatch(_T("Excel.Application"))); //创建IDispatch接口对象 { //MessageBox(_T("Error!")); } books = app.get_Workbooks(); book = books.Add(covOptional); sheets = book.get_Worksheets(); sheet = sheets.get_Item(COleVariant((short)1)); //得到第一个工作表 CHeaderCtrl *pmyHeaderCtrl= m_list.GetHeaderCtrl(); //获取表头 int m_cols = pmyHeaderCtrl-> GetItemCount(); //获取列数 int m_rows = m_list.GetItemCount(); //获取行数 TCHAR lpBuffer[256]; HDITEM hdi; //This structure contains information about an item in a header control. This structure has been updated to support header item images and order values. hdi.mask = HDI_TEXT; hdi.pszText = lpBuffer; hdi.cchTextMax = 256; CString colname; CString strTemp; int iRow,iCol; for(iCol=0; iCol <m_cols; iCol++)//将列表的标题头写入EXCEL { GetCellName(1 ,iCol + 1, colname); //(colname就是对应表格的A1,B1,C1,D1) range = sheet.get_Range(COleVariant(colname),COleVariant(colname)); pmyHeaderCtrl-> GetItem(iCol, &hdi); //获取表头每列的信息 range.put_Value2(COleVariant(hdi.pszText)); //设置每列的内容 int nWidth = m_list.GetColumnWidth(iCol)/6; //得到第iCol+1列 range.AttachDispatch(range.get_Item(_variant_t((long)(iCol+1)),vtMissing).pdispVal,true); //设置列宽 range.put_ColumnWidth(_variant_t((long)nWidth)); } range = sheet.get_Range(COleVariant( _T("A1 ")), COleVariant(colname)); range.put_RowHeight(_variant_t((long)50));//设置行的高度 range.put_VerticalAlignment(COleVariant((short)-4108));//xlVAlignCenter = -4108 COleSafeArray saRet; //COleSafeArray类是用于处理任意类型和维数的数组的类 DWORD numElements[]={m_rows,m_cols}; //行列写入数组 saRet.Create(VT_BSTR, 2, numElements); //创建所需的数组 range = sheet.get_Range(COleVariant( _T("A2 ")),covOptional); //从A2开始 range = range.get_Resize(COleVariant((short)m_rows),COleVariant((short)m_cols)); //表的区域 long index[2]; for( iRow = 1; iRow <= m_rows; iRow++)//将列表内容写入EXCEL { for ( iCol = 1; iCol <= m_cols; iCol++) { index[0]=iRow-1; index[1]=iCol-1; CString szTemp; szTemp=m_list.GetItemText(iRow-1,iCol-1); //取得m_list控件中的内容 BSTR bstr = szTemp.AllocSysString(); //The AllocSysString method allocates a new BSTR string that is Automation compatible saRet.PutElement(index,bstr); //把m_list控件中的内容放入saRet SysFreeString(bstr); } } range.put_Value2(COleVariant(saRet)); //将得到的数据的saRet数组值放入表格 book.SaveCopyAs(COleVariant(cStrFile)); //保存到cStrFile文件 book.put_Saved(true); books.Close(); // book.ReleaseDispatch(); books.ReleaseDispatch(); app.ReleaseDispatch(); app.Quit(); }
5.添加完,代码编译会出现错误,修改:双击提示,在DialogBox()前加下划线,就可以了
6.重新编译,OK
自己记录下步骤!oh,yeah.
按以上六个步骤,就可以实现导出了,以上代码片段网上有比较多,大家可以百度谷歌。需要代码的留邮箱!
参考资料:
http://hi.baidu.com/log4/blog/item/66204c2d8e1b4de18b1399a3.html
http://blog.19lou.com/11935056/viewspace-1905798