![]() |
#2
追梦人zmrghy2023-04-01 22:37
C++/CLI 引用Microsoft::Office::Interop::Word
终于成功了。。。 代码从C# 移植到C++/CLI,用了4天时间终于,蒙对了。。。。 并且,对类、基类、接口类有了一定的了解。 自学,确实会走很多弯路。。。 这样的学习,却可以让自己,很好的正解很抽象的术语,和概念。。。。 看书学习抽象的术语,和概念一会头就大了。 看视频学习抽象的术语,和概念,就像我催眠曲一会就昏昏欲睡了。。。。 自学,走了弯路。带着问题去找答案。。。。 找到答案的时刻。。。便会更加深刻地了解抽象的术语,和概念。 正确代码 ![]() using namespace System; using namespace System::IO; using namespace System::Reflection; namespace MSWord = Microsoft::Office::Interop::Word; int main(array<System::String^>^ args) { Object^ path; //文件路径变量 String^ strContent; //文本内容变量 MSWord::_Application^ wordApp; //Word应用程序变量 MSWord::_Document^ wordDoc; //Word文档变量 array<String^>^ WriteDocumen = { "去年今日此门中,\n","人面桃花相映红。\n" ,"人面不知何处去,\n", "桃花依旧笑春风。" }; path = "C:\\Users\\Administrator\\Desktop\\测试" + DateTime::Now.ToString("yyyyMMddHHmmss") + ".doc"; wordApp = gcnew MSWord::ApplicationClass(); //初始化 wordApp->Visible = true;//使文档可见 //由于使用的是COM库,因此有许多变量需要用Missing.Value代替 Object^ Nothing = Missing::Value; wordDoc = wordApp->Documents->Add(Nothing, Nothing, Nothing, Nothing);//ref Nothing); //行间距与缩进、文本字体、字号 wordApp->Selection->ParagraphFormat->LineSpacing = 16;//设置文档的行间距 wordApp->Selection->ParagraphFormat->FirstLineIndent = 0;//首行缩进的长度 //写入普通文本 strContent = "题都城南庄\n"; wordDoc->Paragraphs->Last->Range->Font->Size = 22; wordDoc->Paragraphs->Last->Range->Font->Name = "方正小标宋简体"; wordDoc->Paragraphs->Last->Range->Text = strContent; wordApp->Selection->ParagraphFormat->Alignment = MSWord::WdParagraphAlignment::wdAlignParagraphCenter; Object^ unite = MSWord::WdUnits::wdStory; wordApp->Selection->ParagraphFormat->FirstLineIndent = 10;//设置首行缩进的长度 for each (auto i in WriteDocumen) { wordApp->Selection->EndKey(unite, Nothing);//将光标移到文本末尾 wordDoc->Paragraphs->Last->Range->Font->Name = "华文行楷"; wordDoc->Paragraphs->Last->Range->Font->Size = 48; wordDoc->Paragraphs->Last->Range->Text = i; wordApp->Selection->ParagraphFormat->Alignment = MSWord::WdParagraphAlignment::wdAlignParagraphCenter; } wordApp->Selection->EndKey(unite, Nothing); //将光标移动到文档末尾 //WdSaveFormat为Word 2003文档的保存格式 Object^ format = MSWord::WdSaveFormat::wdFormatDocument;// office 2007就是wdFormatDocumentDefault //将wordDoc文档对象的内容保存为DOCX文档 wordDoc->SaveAs(path, format, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing); Object^ saveChanges = MSWord::WdSaveOptions::wdSaveChanges; Object^ originalFormat = Type::Missing; Object^ routeDocument = Type::Missing; wordDoc->Close(saveChanges, originalFormat, routeDocument); wordApp->Quit(saveChanges, originalFormat, routeDocument); } Word终于可以关闭了 |
C3287 这是什么意思呀,应该如何处理???
只有本站会员才能查看附件,请 登录

#pragma once
namespace ExportWordDocumen {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Reflection;
/// <summary>
/// Form1 摘要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此处添加构造函数代码
//
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// 必需的设计器变量。
/// </summary>
System::ComponentModel::Container^ components;
#pragma region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(96, 209);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(64, 27);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
namespace MSWord = Microsoft::Office::Interop::Word;
Object^ path; //文件路径变量
String^ strContent; //文本内容变量
MSWord::Application^ wordApp; //Word应用程序变量
MSWord::Document^ wordDoc; //Word文档变量
path = "C:\\Users\\Administrator\\Desktop\\测试" + DateTime::Now.ToString("yyyyMMddHHmmss") + ".doc";
wordApp = gcnew MSWord::ApplicationClass(); //初始化
wordApp->Visible = true;//使文档可见
//如果已存在,则删除
if (File::Exists((String^)path))
{
File::Delete((String^)path);
}
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
Object^ Nothing = Missing::Value;
wordDoc = wordApp->Documents->Add(Nothing, Nothing, Nothing, Nothing);//ref Nothing);
//#region 行间距与缩进、文本字体、字号
wordApp->Selection->ParagraphFormat->LineSpacing = 16;//设置文档的行间距
wordApp->Selection->ParagraphFormat->FirstLineIndent = 0;//首行缩进的长度
//写入普通文本
strContent = "题都城南庄\n";
wordDoc->Paragraphs->Last->Range->Font->Size = 22;
wordDoc->Paragraphs->Last->Range->Font->Name = "方正小标宋简体";
wordDoc->Paragraphs->Last->Range->Text = strContent;
wordApp->Selection->ParagraphFormat->Alignment = MSWord::WdParagraphAlignment::wdAlignParagraphCenter;
Object^ unite = MSWord::WdUnits::wdStory;
wordApp->ActiveDocument->PageSetup->LeftMargin = wordApp->CentimetersToPoints(float::Parse("1.5"));//左页边距
wordApp->Selection->EndKey(unite, Nothing);//将光标移到文本末尾
wordApp->Selection->ParagraphFormat->FirstLineIndent = 10;//设置首行缩进的长度
strContent = "去年今日此门中,\n";
wordDoc->Paragraphs->Last->Range->Font->Name = "华文行楷";
wordDoc->Paragraphs->Last->Range->Font->Size = 48;
wordDoc->Paragraphs->Last->Range->Text = strContent;
wordApp->Selection->ParagraphFormat->Alignment = MSWord::WdParagraphAlignment::wdAlignParagraphCenter;
wordApp->Selection->EndKey(unite, Nothing);//将光标移到文本末尾
strContent = "人面桃花相映红。\n";
wordDoc->Paragraphs->Last->Range->Text = strContent;
wordApp->Selection->ParagraphFormat->Alignment = MSWord::WdParagraphAlignment::wdAlignParagraphCenter;
wordApp->Selection->EndKey(unite, Nothing);//将光标移到文本末尾
strContent = "人面不知何处去,\n";
wordDoc->Paragraphs->Last->Range->Text = strContent;
wordApp->Selection->ParagraphFormat->Alignment = MSWord::WdParagraphAlignment::wdAlignParagraphCenter;
wordApp->Selection->EndKey(unite, Nothing);//将光标移到文本末尾
strContent = "桃花依旧笑春风。";
wordDoc->Paragraphs->Last->Range->Text = strContent;
wordApp->Selection->ParagraphFormat->Alignment = MSWord::WdParagraphAlignment::wdAlignParagraphCenter;
//#endregion
wordApp->Selection->EndKey(unite, Nothing); //将光标移动到文档末尾
//WdSaveFormat为Word 2003文档的保存格式
Object^ format = MSWord::WdSaveFormat::wdFormatDocument;// office 2007就是wdFormatDocumentDefault
//将wordDoc文档对象的内容保存为DOCX文档
wordDoc->SaveAs(path, format, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
Object^ saveChanges = MSWord::WdSaveOptions::wdSaveChanges;
Object^ originalFormat = Type::Missing;
Object^ routeDocument = Type::Missing;
wordDoc->_Document::Close(saveChanges, originalFormat, routeDocument);
}
};
}
namespace ExportWordDocumen {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Reflection;
/// <summary>
/// Form1 摘要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此处添加构造函数代码
//
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// 必需的设计器变量。
/// </summary>
System::ComponentModel::Container^ components;
#pragma region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(96, 209);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(64, 27);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
namespace MSWord = Microsoft::Office::Interop::Word;
Object^ path; //文件路径变量
String^ strContent; //文本内容变量
MSWord::Application^ wordApp; //Word应用程序变量
MSWord::Document^ wordDoc; //Word文档变量
path = "C:\\Users\\Administrator\\Desktop\\测试" + DateTime::Now.ToString("yyyyMMddHHmmss") + ".doc";
wordApp = gcnew MSWord::ApplicationClass(); //初始化
wordApp->Visible = true;//使文档可见
//如果已存在,则删除
if (File::Exists((String^)path))
{
File::Delete((String^)path);
}
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
Object^ Nothing = Missing::Value;
wordDoc = wordApp->Documents->Add(Nothing, Nothing, Nothing, Nothing);//ref Nothing);
//#region 行间距与缩进、文本字体、字号
wordApp->Selection->ParagraphFormat->LineSpacing = 16;//设置文档的行间距
wordApp->Selection->ParagraphFormat->FirstLineIndent = 0;//首行缩进的长度
//写入普通文本
strContent = "题都城南庄\n";
wordDoc->Paragraphs->Last->Range->Font->Size = 22;
wordDoc->Paragraphs->Last->Range->Font->Name = "方正小标宋简体";
wordDoc->Paragraphs->Last->Range->Text = strContent;
wordApp->Selection->ParagraphFormat->Alignment = MSWord::WdParagraphAlignment::wdAlignParagraphCenter;
Object^ unite = MSWord::WdUnits::wdStory;
wordApp->ActiveDocument->PageSetup->LeftMargin = wordApp->CentimetersToPoints(float::Parse("1.5"));//左页边距
wordApp->Selection->EndKey(unite, Nothing);//将光标移到文本末尾
wordApp->Selection->ParagraphFormat->FirstLineIndent = 10;//设置首行缩进的长度
strContent = "去年今日此门中,\n";
wordDoc->Paragraphs->Last->Range->Font->Name = "华文行楷";
wordDoc->Paragraphs->Last->Range->Font->Size = 48;
wordDoc->Paragraphs->Last->Range->Text = strContent;
wordApp->Selection->ParagraphFormat->Alignment = MSWord::WdParagraphAlignment::wdAlignParagraphCenter;
wordApp->Selection->EndKey(unite, Nothing);//将光标移到文本末尾
strContent = "人面桃花相映红。\n";
wordDoc->Paragraphs->Last->Range->Text = strContent;
wordApp->Selection->ParagraphFormat->Alignment = MSWord::WdParagraphAlignment::wdAlignParagraphCenter;
wordApp->Selection->EndKey(unite, Nothing);//将光标移到文本末尾
strContent = "人面不知何处去,\n";
wordDoc->Paragraphs->Last->Range->Text = strContent;
wordApp->Selection->ParagraphFormat->Alignment = MSWord::WdParagraphAlignment::wdAlignParagraphCenter;
wordApp->Selection->EndKey(unite, Nothing);//将光标移到文本末尾
strContent = "桃花依旧笑春风。";
wordDoc->Paragraphs->Last->Range->Text = strContent;
wordApp->Selection->ParagraphFormat->Alignment = MSWord::WdParagraphAlignment::wdAlignParagraphCenter;
//#endregion
wordApp->Selection->EndKey(unite, Nothing); //将光标移动到文档末尾
//WdSaveFormat为Word 2003文档的保存格式
Object^ format = MSWord::WdSaveFormat::wdFormatDocument;// office 2007就是wdFormatDocumentDefault
//将wordDoc文档对象的内容保存为DOCX文档
wordDoc->SaveAs(path, format, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
Object^ saveChanges = MSWord::WdSaveOptions::wdSaveChanges;
Object^ originalFormat = Type::Missing;
Object^ routeDocument = Type::Missing;
wordDoc->_Document::Close(saveChanges, originalFormat, routeDocument);
}
};
}