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

文章作者 100test 发表时间 2007:03:10 16:48:19
来源 100Test.Com百考试题网


如果您要在应用程序中处理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).
//0selection 往下移,以继续生成下一元素
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.
}

相关文章


C _CLI中实现singleton模式
全国计算机等级考试二级C语言上机题41-50
二级共公基础知识教程第3章上
全国计算机等级考试二级C语言上机题31-40
C 程序中导出Word文档简易方法
全国计算机等级考试二级C语言上机题21-30
二级共公基础知识教程第2章下
C 中接口与实现分离的技术
二级共公基础知识教程第2章
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛