如果您要在应用程序中处理Word文档,可以参考MSDN. Lori Turner. Automating Microsoft Office 97 and Office 2000,该文内容详细全面,但是要在C++程序中导出Word文档,按照文中的方法来处理是很麻烦的,特别是需要填写的参数太多,所以我们考虑生成正确的VB脚本,然后执行生成Word文档的操作,这个方法的优点在于:一方面可以少填写参数;另一方面可以使用在Word中录制的宏脚本,而只需作少量的修改。我们给出了一些简单的函数来方便生成Word文档(主要是简单的表格)和直接运行内存中的VB脚本,此外,还附带了一个小小的例子。
//创建Word文档
std::string create_new();
//保存Word文档
std::string close_save(const char* filename);
//selection 往下移,以继续生成下一元素
std::string move_down();
//插入分段符
std::string put_Paragraph();
//添加标题
std::string put_title(const char* title, const char* title_type=”标题 1″, int align=ALIGN_LEFT);
//添加“标题1”
std::string put_title1(const char* title, int align=ALIGN_CENTER);
//添加“标题2”
std::string put_title2(const char* title, int align=ALIGN_LEFT);
//添加“标题3”
std::string put_title3(const char* title, int align=ALIGN_LEFT);
//添加红色警告信息
std::string add_warning_msg(const char* msg=”无数据”);
//添加表格的一行数据(不用此函数)
std::string add_grid_ln(const char* line);
//添加表格
std::string put_grid(const char* content);
//运行脚本
extern “C” void RunScript(const char* script_str); 下面是一个小例子,我们期望它在您的计算机上能够很好的运行,程序将生成一个Word文档,路径位于c:\test.doc,计算机上需要安装Word XP。
int main(int argc, char* argv[])
{
ostringstream ostr;
ostr<<create_new();
ostr<<put_title1(“统计结果”);
ostr<<put_title2(“统计子项详细信息”);
std::string str_buffer;
read_file_as_grid_content(“tab.txt”, str_buffer);
ostr<<put_Paragraph();
ostr<<put_grid(str_buffer.c_st r());
ostr<<close_save(“c:\\test.doc “);
//输出到文件看看VB脚本的内容
/*
std::ofstream ofile;
ofile.open(“c:\\temp.vbs”);
ofile<<ostr.str().c_str();
ofile.close();
*/
//BeginWaitCursor( );
RunScript( ostr.str().c_str() );//运行生成的脚本
//EndWaitCursor();
return 0;
}
-
近期文章
近期评论
- coolker 发表在《打造最快的Hash表》
- struggle 发表在《提供C语言教学课件(适用于初学者)》
- zhanghaibo 发表在《提供C语言教学课件(适用于初学者)》
- zhanghaibo 发表在《提供C语言教学课件(适用于初学者)》
- diys 发表在《C语言编程宝典(王大刚) 1.1 C 语言的产生与发展》
文章归档
- 2022 年十月
- 2014 年一月
- 2013 年十二月
- 2012 年十一月
- 2012 年七月
- 2012 年六月
- 2012 年五月
- 2012 年四月
- 2012 年三月
- 2012 年二月
- 2011 年十二月
- 2011 年十月
- 2011 年九月
- 2011 年八月
- 2011 年七月
- 2011 年六月
- 2011 年五月
- 2011 年四月
- 2011 年三月
- 2011 年二月
- 2011 年一月
- 2010 年十二月
- 2010 年十一月
- 2010 年十月
- 2010 年九月
- 2010 年八月
- 2010 年七月
- 2010 年六月
- 2010 年五月
- 2010 年四月
- 2010 年三月
- 2010 年二月
- 2010 年一月
- 2009 年十二月
- 2009 年十一月
- 2009 年十月
- 2009 年九月
- 2009 年八月
- 2009 年七月
- 2009 年六月
- 2009 年五月
- 2009 年四月
- 2009 年三月
- 2009 年二月
- 2009 年一月
- 2008 年十二月
- 2008 年十一月
- 2008 年十月
- 2008 年九月
- 2008 年八月
- 2008 年七月
- 2008 年六月
- 2008 年五月
- 2008 年四月
- 2008 年三月
- 2008 年二月
- 2008 年一月
- 2007 年十二月
- 2007 年十一月
- 2007 年十月
- 2007 年九月
- 2007 年八月
- 2007 年七月
- 2007 年六月
- 2007 年三月
- 2007 年二月
- 2007 年一月
- 2006 年十二月
- 2006 年十一月
- 2006 年十月
- 2006 年九月
- 2006 年八月
- 2006 年七月
- 2006 年六月
- 2006 年五月
- 2006 年四月
- 2006 年三月
- 2006 年二月
- 2006 年一月
- 2005 年十二月
- 2005 年十一月
分类目录
功能