C++程序中导出Word文档简易方法

C++程序中导出Word文档简易方法,第1张

C++程序中导出Word文档简易方法,第2张

如果想在应用程序中处理Word文档,可以参考MSDN。萝莉·特纳。自动化Microsoft Office 97和Office 2000。这篇文章很详细,也很全面,但是按照这篇文章中的方法在C++程序中导出Word文档是很麻烦的,尤其是因为要填写的参数太多,所以我们考虑生成一个正确的VB脚本,然后执行生成Word文档的操作。这种方法的优点是:一方面可以填写较少的参数;另一方面,您可以使用Word中记录的宏脚本,只需做一些修改。我们给出一些简单的函数生成Word文档(主要是简单的表格),直接在内存中运行VB脚本。另外附上一个小例子。

//创建一个Word文档
STD::string Create _ new();
//保存Word文档
STD::string close _ Save(const char * filename);
//选择下移继续生成下一个元素
STD::string move _ down();
//插入段落字符
STD::string put _ Paragraph();
//添加标题
STD::string put _ title(const char * title,constchar * title _ type = "title 1 ",int ALIGN = ALIGN _ LEFT);
//Add " Title 1 "
STD::string put _ Title 1(const char * Title,int align = align _ center);
//Add " Title 2 "
STD::string put _ Title 2(const char * Title,int align = align _ left);
//Add " Title 3 "
STD::string put _ Title 3(const char * Title,int align = align _ left);
//添加红色预警信息
STD::string Add _ warning _ msg(const char * msg = " no data ");
//从表中添加一行数据(无此函数)
STD::string Add _ grid _ ln(const char * line);
//添加表格
STD::string put _ grid(const char * content);

//运行脚本
extern " c " void Run script(const char * script _ str);下面是一个小例子。我们希望它能在你的电脑上运行良好。该程序将生成一个Word文档,路径为C:\ test . doc。Word XP需要安装在您的计算机上。
int main(int argc,char * argv[])
{
ostringstream ostr;
ostr <
ostr < ostr <
STD::string str _ buffer;
read _ file _ as _ grid _ content(" tab . txt ",str _ buffer);

ostr < ostr < ostr <
/输出到文件中查看VB脚本的内容
/*
STD::of stream of file;
of file . open(" c:\ \ temp . VBS ");
of ile < of ile . close();
*/
//BeginWaitCursor();
RunScript( ostr.str()。c _ str());//运行生成的脚本
//EndWaitCursor();
返回0;
}

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » C++程序中导出Word文档简易方法

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情