Ways to export Word documents in C + + programs

Source: Internet
Author: User

If you want to work with Word documents in your application, you can refer to MSDN. Lori Turner. Automating Microsoft Office and Office 2000, the content is comprehensive, but to export Word documents in a C + + program is cumbersome, especially if you need to fill in too many parameters.

So we consider generating the correct VB script and then performing the action to generate the Word document, which has the advantage that you can write less parameters on the one hand, and a macro script that is recorded in Word instead of a small amount of modification. We've given some simple functions to facilitate the generation of Word documents (mostly simple tables) and VB scripts that run directly in memory, along with a small example.

//创建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);

Here is a small example, we expect it to work well on your computer, the program will generate a Word document, the path is located in C:\Test.doc, you need to install Word XP on your computer.

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.