officeba > 单独文章


用C++程序中导出Word文档的简单办法

  如果您要在应用程序中处理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<

  ostr<

  ostr<

  std::string str_buffer;

  read_file_as_grid_content("tab.txt", str_buffer);

  ostr<

  ostr< 

  ostr<

  //输出到文件看看VB脚本的内容

  /*

  std::ofstream ofile;

  ofile.open("c:\\temp.vbs");

  ofile<

  ofile.close();

  */

  //BeginWaitCursor();

  RunScript( ostr.str().c_str() );//运行生成的脚本

  //EndWaitCursor();

  return 0;

  }


声明:欢迎各大网站转载本站文章,还请保留一条能直接指向本站的超级链接,谢谢!

时间:2007-05-13 11:11:03,点击:65824


【OfficeBa论坛】:阅读本文时遇到了什么问题,可以到论坛进行交流!Excel专家邮件:342327115@qq.com(大家在Excel使用中遇到什么问题,可以咨询此邮箱)。

【声明】:以上文章或资料除注明为Office自创或编辑整理外,均为各方收集或网友推荐所得。其中摘录的内容以共享、研究为目的,不存在任何商业考虑。如有任何异议,请与本站联系,本站确认后将立即撤下。谢谢您的支持与理解!


相关评论

我要评论

评论内容