描述:
我已经在word里插如了一个图表(如柱状图吧),现在我想根据得到的不同数据改变图表中的数据,使图表比例发生变法,该怎么做呢?谢谢
解决方案1:
首先要得到一个chart对象,然后再得到CELL的RANGE范围,具体在论坛上搜索一下
解决方案2:看看!
解决方案3: yintongshun(踏雪有痕) ( ) 信誉:105
的方法可以借鉴!
在VC中如何将数据导入Word中
[03-9-10 10:36] 作者:何鸿鹏
在给企业开发的项目中,客户经常要求将数据汇总显示并打印。站在程序员的立场上,简单数据的汇总打印可以采用VB自带报表,对复杂的数据用水晶报表或第三方打印控件。在企业中,汇总数据另一个目的是为了便于交流和共享资源,报表的格式通常固定,内容可以自己调整,他们对 Excel 和 Word 情有独钟。
刚完成的项目中,客户要求将汇总的数据以三种方式进行显示和打印:AutoCad、Excel 和 Word 文档中。前两种方式网上资源很多,对 Word 文档我找到例子不是很多。由于需要和 AutoCad 开发程序结合,不能将 Word 文档以 OLE 的方式 嵌入到程序中,文档只能在内存中形成,这将占用大量的CPU资源,另开一个线程进行处理,线程间以发送消息的方式进行通信。
首先在VC引入Word, 对格式相同的地方一定使用模版的方式进行处理,这可以大大的提高运行速度,Word中的模版为*.dot。加载模版的代码为:
sDocs=sApp.GetDocuments();
sDocs.AttachDispatch(sApp.GetDocuments(),true);
COleVariant vFalse((long)0),vTrue((long)1);
sDoc.AttachDispatch(sDocs.Add(COleVariant(_variant_t(strFileName)), vFalse,vFalse, vTrue));
需要注意的是,如果Word中使用到表格,VBA录制的全是Selection对象的处理方法,在VC中使用Selection对象的MoveUp和MoveDown函数时会出现不确定现象(如果你知道为什么的话请告诉我)。如果你需要绘制表格,不要用程序来动态形成,绘制表格的代码是很繁琐的,而且在页眉中绘制单元格是很难控制的。可以先在模版中绘出你要的表格格式,然后用程序复制单元格或者直接使用模版中的单元格。由于我做的项目表格的行数不确定,我采用的是复制单元格的方式(速度比较慢,谁有更好的方法?)。
剩下的处理流程,就是你想做什么操作,先在Word中录制一段宏,然后查看其VBA代码,并将其转化到Vc程序中。这就看你Word使用的熟练程度了,例如如何把不同页的页眉页脚设置不同,如何得到当前页的页数以及总的文档的页数,具体可以参考程序。
VC中也可以像Excel中调用模版中存在的宏,代码如下:
VARIANT vtMissing ;vtMissing.vt =VT_ERROR;
vtMissing.scode =DISP_E_PARAMNOTFOUND;
sApp.Run("AddCell",&vtMissing,&vtMissing,&vtMissing,
&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,
&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,
&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,
&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing);
运用VC或Java对Office进行编程操作 etre(原作)
用VC对Office进行操作的介绍已经不少了,但是从来没有把word,excel,powerPoint进进全面的介绍的。
由于工作的需要,我需要对在自己的软件中对word,excel,powerPoint进行操作。所以把自己的体会写出来和大家分享,希望对大家有所帮助。当然还有很多不当之处,希望大家指出。
用例子来说明吧,首先创建一个MFC AppWizard(EXE)工程,然后通过在VIEW菜单中,选ClassWizard,选Automation选项卡,选Add Class,选择From a TypeLibrary, 选中Microsoft Office 2000 类型库:Excel9.olb,MSPPT9.OLB,MSWORD9.OLB(在Microsoft Office\Office目录下) 会将类型库中的所有类添加到你的工程中。
然后写一个类来操作Office吧!
ObtGuiGcomOfficePrinter .h
#if !defined(AFX_OBTGUIGCOMOFFICEPRINTER_H__03A0C2D8_DFC8_4B51_8ADB_994B86BACB82__INCLUDED_)
#define AFX_OBTGUIGCOMOFFICEPRINTER_H__03A0C2D8_DFC8_4B51_8ADB_994B86BACB82__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "comdef.h"
#include "ObtGuiGcomMsWord9.h"
#include "ObtGuiGcomMsPpt9.h"
#include "ObtGuiGcomExcel9.h"
class AFX_EXT_CLASS ObtGuiGcomOfficePrinter
{
public:
ObtGuiGcomOfficePrinter();
virtual ~ObtGuiGcomOfficePrinter();
//Operator
public:
BOOL WordPrinterToJcf(LPCTSTR lpszFileName,LPCTSTR lpszActivePrinter);
BOOL ExcelPrinterToJcf(LPCTSTR lpszFileName,LPCTSTR lpszActivePrinter);
BOOL PowerPointPrinterToJcf(LPCTSTR lpszFileName,LPCTSTR lpszActivePrinter);
};
#include "stdafx.h"
#include "ObtGuiGcomOfficePrinter.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
ObtGuiGcomOfficePrinter::ObtGuiGcomOfficePrinter()
{
}
ObtGuiGcomOfficePrinter::~ObtGuiGcomOfficePrinter()
{
}
BOOL ObtGuiGcomOfficePrinter::PowerPointPrinterToJcf(LPCTSTR lpszFileName,LPCTSTR lpszActivePrinter)
{
_PptApplication m_powerpointApp;
Presentations m_powerpointPres;
_Presentation m_powerpointPre;
m_powerpointPres.ReleaseDispatch();
m_powerpointPre.ReleaseDispatch();
if(!m_powerpointApp.CreateDispatch("PowerPoint.Application"))
{
AfxMessageBox("创建PowerPoint服务失败!");
return FALSE;
}
m_powerpointApp.m_bAutoRelease=true;
m_powerpointApp.SetVisible(TRUE);//对于PowerPoint必须设置为TRUE
m_powerpointPres.AttachDispatch(m_powerpointApp.GetPresentations());
m_powerpointPres.Open(lpszFileName,-1,-1,-1);
m_powerpointPre.AttachDispatch(m_powerpointApp.GetActivePresentation(),TRUE);
m_powerpointPre.PrintOut(-1,-1,"",long(1),-1);
m_powerpointApp.Quit();
m_powerpointPre.ReleaseDispatch();
m_powerpointPres.ReleaseDispatch();
m_powerpointApp.ReleaseDispatch();
return TRUE;
}
BOOL ObtGuiGcomOfficePrinter::ExcelPrinterToJcf(LPCTSTR lpszFileName,LPCTSTR lpszActivePrinter)
{
_ExcelApplication m_excelApp;//定义Excel提供的应用程序对象
Workbooks m_excelBooks;
_Workbook m_excelBook;
m_excelBooks.ReleaseDispatch();
m_excelBook.ReleaseDispatch();
m_excelApp.m_bAutoRelease=true;
//创建Excel 2000服务器(启动Excel)
if (!m_excelApp.CreateDispatch("Excel.Application"))
{
AfxMessageBox("创建Excel服务失败!");
return FALSE;
}
&