标签:简单 range initial eth 实现 路径 绑定 执行 blog
一、加载
1、 在VC6.0里创建一个MFC工程
2、打开MFCClassWizard窗口(查看—>建立类向导),选择Automation,单击AddClass按钮,选择Froma type library...,弹出文件选择对话框,之后定位到C:\Program Files\MicrosoftOffice\OFFICE11\EXCEL.EXE,在生成类中添加所有的对象(其实添加需要的即可,为了简便,不出错保留了冗余),如下图。
3、返回编辑器,查看工程文件,可发现多了EXCEL.9H及EXCEL9.CPP两个文件,拷贝出来,放在VS2005需要使用excel的工程文件中。
4. 打开stdafx.h头文件确保包含如下头文件:
#include <afxdisp.h>(这个一般有了)
#include "excel.h" (手动添加这个即可)
5. 打开TestExcel.cpp文件,修改CTestExcelApp::InitInstance(),加入如下代码:
if(!AfxOleInit() ){
AfxMessageBox("初始化Ole出错!");
return FALSE;
}
为保证编译时不产生重复定义错误(可以验证一下是否成功加载,没有也能正常执行),我编译时出现了很多“类重复定义”异常,打开excel.h文件,在文件开始位置加入如下代码:
#if !defined _HEAD_FILE_EXCEL9_
#define _HEAD_FILE_EXCEL9_
相应的,在文件末尾加入:
#endif
成功
Excel接口
|
导入类
|
头文件
|
说明
|
_Application
|
CApplicaton
|
Application.h
|
Excel应用程序。
|
Workbooks
|
CWorkbooks
|
Workbooks.h
|
工作簿的容器,里面包括了Excel应用程序打开的所有工作簿。
|
_Workbook
|
CWorkbook
|
Workbook.h
|
单个工作簿。
|
Worksheets
|
CWorksheets
|
Worksheets.h
|
单个工作簿中的Sheet表格的容器,包括该工作簿中的所有Sheet。
|
_Worksheet
|
CWorksheet
|
Worksheet.h
|
单个Sheet表格。
|
Range
|
CRange
|
Range.h
|
一定数量的单元格,可对单元格进行单个或多个单元格进行操作。
|
在MSDN中,很少有excel方面的资料,但是在http://msdn.microsoft.com/zh-cn/ms348103.aspx中可以找到C#控制EXCEL方面的说明
二、操作EXCEL文件
1. 新建一个excel表,并填充两个单元格的实例
- void CTestExcelDlg::OnButton1()
- {
- _Application app;
- Workbooks books;
- _Workbook book;
- Worksheets sheets;
- _Worksheet sheet;
- Range range;
- Font font;
- Range cols;
- COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
- if( !app.CreateDispatch("Excel.Application") ){
- this->MessageBox("无法创建Excel应用!");
- return;
- }
- books=app.GetWorkbooks();
- book=books.Add(covOptional);
- sheets=book.GetSheets();
- sheet=sheets.GetItem(COleVariant((short)1));
- range=sheet.GetRange(COleVariant("A1"),COleVariant("A1"));
- range.SetValue(COleVariant("HELLO EXCEL!"));
- font=range.GetFont();
- font.SetBold(COleVariant((short)TRUE));
- 式
- range=sheet.GetRange(COleVariant("A2"),COleVariant("A2"));
- range.SetFormula(COleVariant("=RAND()*100000"));
- range.SetNumberFormat(COleVariant("$0.00"));
- cols=range.GetEntireColumn();
- cols.AutoFit();
- app.SetVisible(TRUE);
- app.SetUserControl(TRUE);
2. 打开一个已有的excel表格实例
- CString strPath;
- strPath += "C:\\template.xlt";
- CFileFind filefind;
- if( !filefind.FindFile( strPath ) )
- {
- AfxMessageBox( "没有找到模版文档,请其查找" );
- return;
- }
- LPDISPATCH lpDisp;
- books=app.GetWorkbooks();
- lpDisp = books.Open(m_filepath,
- covOptional, covOptional, covOptional, covOptional,
- covOptional, covOptional, covOptional, covOptional,
- covOptional, covOptional, covOptional, covOptional,
- covOptional, covOptional
- );
- book.AttachDispatch(lpDisp);
3. 保存一个excel文件实例
4. 另存一个excel文件实例
- book.SaveAs(COleVariant(m_filename),covOptional,
- covOptional,covOptional,
- covOptional,covOptional,(long)0,
- covOptional,covOptional,covOptional,
- covOptional,covOptional);
5. 释放一个excel文件实例
经试验证实,不释放第二次使用excel时会中断,放在类的析构里面有时调用不到,主动调用最保险。(有没有AttachDispatch()过都要释放,否则报错)
- Rang.ReleaseDispatch();
- sheet.ReleaseDispatch();
- sheets.ReleaseDispatch();
- book.ReleaseDispatch();
- books.ReleaseDispatch();
- app.Quit();
- app.ReleaseDispatch();
6. 修改一个excel单元格
- range=sheet.GetRange(COleVariant(IndexToString(row,col)),COleVariant(IndexToString(row,col)));
- range.SetValue2(COleVariant(value));
7. 取出一个excel单元格
实现Variant数据类型转换为CString类,这个只是一个示例,转换较为简单。
- range=sheet.GetRange(COleVariant(IndexToString(row,col)),COleVariant(IndexToString(row,col)));
- COleVariant rValue;
- rValue=COleVariant(range.GetValue2());
- rValue.ChangeType(VT_BSTR);
- return CString(rValue.bstrVal);
8. 还有释放问题是最重要的问题:
首先变量必须全释放,无论当初是否绑定过;
其次,程序释放和程序关闭的顺序必须是
- app.Quit();
- app.ReleaseDispatch();
- 如果顺如颠倒如下:
- app.ReleaseDispatch();
- app.Quit();
- 出现的后果是程序关闭后,excel进程仍然运行,所以无法正常打开程序曾经打开excel表格。
附录(操作类源码):
- #include "../Stdafx.h"
- #include "OptExcel.h"
- #include "excel.h"
- #include "comdef.h"
-
-
- _Application app;
- Workbooks books;
- _Workbook book;
- Worksheets sheets;
- _Worksheet sheet;
- Range range;
- Range cell;
- Font font;
-
- COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
-
-
- COptExcel::COptExcel(void)
- {
- if (::CoInitialize( NULL ) == E_INVALIDARG)
- {
- AfxMessageBox(_T("初始化Com失败!"));
- return;
- }
-
-
-
- if( !app.CreateDispatch(_T("Excel.Application")) )
- {
- AfxMessageBox(_T("无法创建Excel应用!"));
- return;
- }
-
-
-
- }
-
-
- COptExcel::~COptExcel(void)
- {
-
-
-
- books.ReleaseDispatch();
- book.ReleaseDispatch();
- sheets.ReleaseDispatch();
- sheet.ReleaseDispatch();
- range.ReleaseDispatch();
- font.ReleaseDispatch();
- cell.ReleaseDispatch();
-
- app.Quit();
-
- app.ReleaseDispatch();
- ::CoUninitialize();
- }
-
-
- bool COptExcel::OpenExcelBook(CString filename)
- {
- CFileFind filefind;
- if( !filefind.FindFile(filename) )
- {
- AfxMessageBox(_T("文件不存在"));
- return false;
- }
- LPDISPATCH lpDisp;
- books=app.GetWorkbooks();
- lpDisp = books.Open(filename,
- covOptional, covOptional, covOptional, covOptional,
- covOptional, covOptional, covOptional, covOptional,
- covOptional, covOptional, covOptional, covOptional,
- covOptional, covOptional
- );
- book.AttachDispatch(lpDisp);
- sheets=book.GetSheets();
- sheet=sheets.GetItem(COleVariant((short)1));
- return true;
- }
- void COptExcel::NewExcelBook()
- {
- books=app.GetWorkbooks();
- book=books.Add(covOptional);
- sheets=book.GetSheets();
- sheet=sheets.GetItem(COleVariant((short)1));
- }
-
- void COptExcel::OpenExcelApp(void)
- {
- app.SetVisible(TRUE);
- app.SetUserControl(TRUE);
- }
-
- void COptExcel::SaveExcel(void)
- {
- book.SetSaved(TRUE);
- }
-
- void COptExcel::SaveAsExcel(CString filename)
- {
- book.SaveAs(COleVariant(filename),covOptional,
- covOptional,covOptional,
- covOptional,covOptional,(long)0,covOptional,covOptional,covOptional,
- covOptional,covOptional);
- }
-
-
- void COptExcel::SetCellValue(int row, int col,int Align)
- {
- range=sheet.GetRange(COleVariant(IndexToString(row,col)),COleVariant(IndexToString(row,col)));
- range.SetValue2(COleVariant(value));
- cell.AttachDispatch((range.GetItem (COleVariant(long(1)), COleVariant(long(1)))).pdispVal);
- cell.SetHorizontalAlignment(COleVariant((short)Align));
- }
-
- CString COptExcel::GetCellValue(int row, int col)
- {
- range=sheet.GetRange(COleVariant(IndexToString(row,col)),COleVariant(IndexToString(row,col)));
- COleVariant rValue;
- rValue=COleVariant(range.GetValue2());
- rValue.ChangeType(VT_BSTR);
- return CString(rValue.bstrVal);
- }
- void COptExcel::SetRowHeight(int row, CString height)
- {
- int col = 1;
- range=sheet.GetRange(COleVariant(IndexToString(row,col)),COleVariant(IndexToString(row,col)));
- range.SetRowHeight(COleVariant(height));
- }
- void COptExcel::SetColumnWidth(int col,CString width)
- {
- int row = 1;
- range=sheet.GetRange(COleVariant(IndexToString(row,col)),COleVariant(IndexToString(row,col)));
- range.SetColumnWidth(COleVariant(width));
- }
-
- CString COptExcel::GetColumnWidth(int col)
- {
- int row = 1;
- range=sheet.GetRange(COleVariant(IndexToString(row,col)),COleVariant(IndexToString(row,col)));
- VARIANT width = range.GetColumnWidth();
- CString strwidth;
- strwidth.Format(CString((LPCSTR)(_bstr_t)(_variant_t)width));
- return strwidth;
- }
-
- CString COptExcel::GetRowHeight(int row)
- {
- int col = 1;
- range=sheet.GetRange(COleVariant(IndexToString(row,col)),COleVariant(IndexToString(row,col)));
- VARIANT height = range.GetRowHeight();
- CString strheight;
- strheight.Format(CString((LPCSTR)(_bstr_t)(_variant_t)height));
- return strheight;
- }
-
-
- CString COptExcel::IndexToString( int row, int col )
- {
- CString strResult;
- if( col > 26 )
- {
- strResult.Format(_T("%c%c%d"),‘A‘ + (col-1)/26-1,‘A‘ + (col-1)%26,row);
- }
- else
- {
- strResult.Format(_T("%c%d"), ‘A‘ + (col-1)%26,row);
- }
- return strResult;
- }
-
- int COptExcel::LastLineIndex()
- {
- int i,j,flag=0;
- CString str;
- for(i=1;;i++)
- {
- flag = 0;
-
- for(j=1;j<=5;j++)
- {
- str.Format(_T("%s"),this->GetCellValue(i,j).Trim());
- if(str.Compare(_T(""))!=0)
- {
- flag = 1;
- break;
- }
-
- }
- if(flag==0)
- return i;
-
- }
- }
-
使用MFC操作EXCEL文件
标签:简单 range initial eth 实现 路径 绑定 执行 blog
原文地址:http://www.cnblogs.com/huhewei/p/6216986.html