导出Excel的第一种方法用的是:
Excel::_ApplicationPtr ptrExcelApp;
Excel::_WorkbookPtr ptrBook;
Excel::_WorksheetPtr ptrSheet;
Excel::RangePtr ptrRange;
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE11\\MSO.DLL" rename( "RGB", "MSORGB" )
using namespace Office;
#pragma warning(disable : 4192)
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB"
#import "D:\\Program Files\\Microsoft Office\\OFFICE11\\EXCEL.EXE" \
rename( "DialogBox", "ExcelDialogBox" ) \
rename( "RGB", "ExcelRGB" ) \
rename( "CopyFile", "ExcelCopyFile" ) \
rename( "ReplaceText", "ExcelReplaceText" )
void CRecordView::OnFileExport()
{
TCHAR szFilter[] = { _T("Excel文件 (*.xls)|*.xls|所有文件 (*.*)|*.*||") };
CFileDialog SaveDialog(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);
if (SaveDialog.DoModal() == IDOK)
{
_variant_t varOption((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CWaitCursor wait;
try
{
Excel::_ApplicationPtr pExcelApp(_T("Excel.Application"));
Excel::_WorkbookPtr pBook = pExcelApp->Workbooks->Add(varOption);
Excel::_WorksheetPtr pSheet = pBook->ActiveSheet;
//Excel::RangePtr pRange = pSheet->GetRange(_T("A1"), _T("D1"));
Excel::RangePtr pRange = pSheet->Cells;
CHeaderCtrl* pHeader = m_wndList.GetHeaderCtrl();
int nColumn = pHeader->GetItemCount();
{
TCHAR szText[40] = { 0 };
HDITEM hdi = { 0 };
hdi.mask = HDI_TEXT;
hdi.pszText = szText;
hdi.cchTextMax = 40;
for (int i = 1; i < nColumn; i++)
{
pHeader->GetItem(i, &hdi);
pRange->Item[1][i] = hdi.pszText;
}
Excel::RangePtr pHeaderRange = pRange->GetRange(variant_t(_T("A1")), variant_t(_T("F1")));
pHeaderRange->Font->Bold = true;
}
for (int i = 0; i < m_wndList.GetItemCount(); i++)
{
for (int j = 1; j < nColumn; j++)
{
pRange->Item[i+2][j] = (LPCTSTR)m_wndList.GetItemText(i, j);
}
}
pRange = pRange->EntireColumn;
pRange->AutoFit();
pBook->Close(true, (LPCTSTR)SaveDialog.GetPathName(), varOption);
pExcelApp->Quit();
AfxMessageBox(_T("数据已成功导出到Excel表格中!"), MB_OK | MB_ICONINFORMATION);
}
catch (...)
{
AfxMessageBox(_T("导出数据错误!"), MB_OK | MB_ICONERROR);
return;
}
}
}
转载自 : http://www.cppblog.com/wangjs720/archive/2007/09/10/31934.html
导出Excel的第二种方法用的是如下 :
CApplication app;
CWorkbook0 book;
CWorkbooks0 books;
CWorksheet0 sheet; CWorksheets0 sheets;
CRange0 range;
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;
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];
//将列表内容写入EXCEL
for(iRow = 1;iRow <= m_rows;iRow++)
{
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(); //AlLogyString方法分配一个自动化兼容的新BSTR字符串。
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
转载自 : https://blog.csdn.net/qrx2013/article/details/12749333
转载一下下面的链接,感觉文章写的类似,代码比较全,但是注释没有上面的详细.
转载 : https://blog.csdn.net/limingmcu/article/details/38058507