敬业的IT人 >> 编程开发 >> C/C++ >> C++程序中导出Word文档的简易方法

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

敬业的IT人 互联网 佚名 2008-1-3 19:15:39

  如果您要在应用程序中处理Word文档,可以参考MSDN. Lori Turner. Automating MicrosoftOffice97 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_str());    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;} 
转载:进入讨论组讨论。
粤ICP备06119539号
Copyright CiscoSky.Org,Some Rights Reserved.
Email:me1228#tom.com