ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 【自动生成excel与word并打开 】

【自动生成excel与word并打开 】

原创 Linux操作系统 作者:heerofriend 时间:2010-07-03 14:25:35 0 删除 编辑

核心代码如下,基于VS2010创建一个空白CLR工程,插入下列C++代码,直接编译执行之,

 

view plaincopy to clipboardprint?

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

//.h file code:  

 

using namespace System;  

using namespace System::Collections::Generic;  

 

//引用office 2010  

namespace Excel = Microsoft::Office::Interop::Excel;  

namespace Word = Microsoft::Office::Interop::Word;  

//创建引用类 Account  

public ref class Account  

{  

public:  

    property int ID;  

    property double Balance;  

};  

声明程序接口与方法  

public ref class Program  

{  

    static void Main(array ^args);  

private:  

static void AnonymousMethod1(System::Object ^account, System::Object ^cell);  

 

public:  

    static void DisplayInExcel(IEnumerable ^accounts, Action ^DisplayFunc);  

};  

 

//.cpp file code:  

 

using namespace System;  

using namespace System::Collections::Generic;  

namespace Excel = Microsoft::Office::Interop::Excel;  

namespace Word = Microsoft::Office::Interop::Word;  

//开始执行程序  

void Program::Main(array ^args)  

{  

 

    var ^checkAccounts = gcnew List { gcnew Account { ID = 345, Balance = 541.27 }, gcnew Account { ID = 123, Balance = -127.44 } };  

 

        //设置自定义处理规则  

    DisplayInExcel(checkAccounts, AnonymousMethod1);  

 

 

    var ^word = gcnew Word.Application();  

    word->Visible = true;  

    word->Documents->Add();  

    word->Selection->PasteSpecial(Link: true, DisplayAsIcon: true);  

}  

 

void Program::AnonymousMethod1(System::Object ^account, System::Object ^cell)  

{  

        cell->Value2 = account->ID;  

        cell->get_Offset(0, 1)->Value2 = account->Balance;  

        if (account->Balance < 0)  

        {  

            cell->Interior->Color = 255;   

            cell->get_Offset(0, 1)->Interior->Color = 255;  

        }  

}  

//打开并创建excel  

void Program::DisplayInExcel(IEnumerable ^accounts, Action ^DisplayFunc)  

{  

 

    var ^xl = gcnew Excel::Application();  

 

    xl->Workbooks->Add();  

    xl->Visible = true;  

    xl->Cells[1, 1] = "ID";  

    xl->Cells[1, 2] = " Balance";  

    xl->Cells[2, 1]->Select();  

 

    for each (var ^ac in accounts)  

    {  

        DisplayFunc(ac, xl->ActiveCell);  

        xl->ActiveCell->get_Offset(1, 0)->Select();  

    }  

 

    xl->get_Range("A1:B3")->Copy();  

 

    xl->Columns[1]->AutoFit();  

    xl->Columns[2]->AutoFit();  

} 

//.h file code:

 

using namespace System;

using namespace System::Collections::Generic;

 

//引用office 2010

namespace Excel = Microsoft::Office::Interop::Excel;

namespace Word = Microsoft::Office::Interop::Word;

//创建引用类 Account

public ref class Account

{

public:

      property int ID;

      property double Balance;

};

声明程序接口与方法

public ref class Program

{

      static void Main(array ^args);

private:

static void AnonymousMethod1(System::Object ^account, System::Object ^cell);

 

public:

      static void DisplayInExcel(IEnumerable ^accounts, Action ^DisplayFunc);

};

 

//.cpp file code:

 

using namespace System;

using namespace System::Collections::Generic;

namespace Excel = Microsoft::Office::Interop::Excel;

namespace Word = Microsoft::Office::Interop::Word;

//开始执行程序

void Program::Main(array ^args)

{

 

      var ^checkAccounts = gcnew List { gcnew Account { ID = 345, Balance = 541.27 }, gcnew Account { ID = 123, Balance = -127.44 } };

 

           //设置自定义处理规则

      DisplayInExcel(checkAccounts, AnonymousMethod1);

 

 

      var ^word = gcnew Word.Application();

      word->Visible = true;

      word->Documents->Add();

      word->Selection->PasteSpecial(Link: true, DisplayAsIcon: true);

}

 

void Program::AnonymousMethod1(System::Object ^account, System::Object ^cell)

{

           cell->Value2 = account->ID;

           cell->get_Offset(0, 1)->Value2 = account->Balance;

           if (account->Balance < 0)

           {

                 cell->Interior->Color = 255;

                 cell->get_Offset(0, 1)->Interior->Color = 255;

           }

}

//打开并创建excel

void Program::DisplayInExcel(IEnumerable ^accounts, Action ^DisplayFunc)

{

 

      var ^xl = gcnew Excel::Application();

 

      xl->Workbooks->Add();

      xl->Visible = true;

      xl->Cells[1, 1] = "ID";

      xl->Cells[1, 2] = " Balance";

      xl->Cells[2, 1]->Select();

 

      for each (var ^ac in accounts)

      {

           DisplayFunc(ac, xl->ActiveCell);

           xl->ActiveCell->get_Offset(1, 0)->Select();

      }

 

      xl->get_Range("A1:B3")->Copy();

 

      xl->Columns[1]->AutoFit();

      xl->Columns[2]->AutoFit();

}

 

 执行效果如下

 

生成excel 2010

 

生成 word 2010

 

61.JPG

62.JPG

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/506115/viewspace-667080/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2010-07-03

  • 博文量
    1
  • 访问量
    1102