龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

C++中将ListView中的内容导出到Word和Excel(新)

时间:2009-12-22 15:42来源:未知 作者:admin 点击:
分享到:
经常看到有网友发帖子询问如何将ListView中的内容导出到Excel或Word文档中,其实在BCB中用OLE技术来操作,并不复杂,大概是有的人懒的写吧,于是ccrun(老妖)花了点时间写了以下两个函数

  经常看到有网友发帖子询问如何将ListView中的内容导出到Excel或Word文档中,其实在BCB中用OLE技术来操作,并不复杂,大概是有的人懒的写吧,于是ccrun(老妖)花了点时间写了以下两个函数,实现了将本程序中ListView中内容导出到Excel文档和Word文档。看在写代码很辛劳的份上,请在转载时留下出处和原作者信息。Thank了。:D

  

  假如您有好的想法,欢迎来信讨论: info@ccrun.com

  

  2005.10.13 v0.2

  + 导出表格增加了标题一栏

  2005.10.12 v0.1

  初版发布

  

  //---------------------------------------------------------------------------

  // 将ListView中的内容导出到Word文档

  // v0.2 by ccrun(老妖) 2005.10.13

  //---------------------------------------------------------------------------

  void __fastcall ListView2Word(TListView *lv, String strDocFile)

  {

   Variant vWordApp, vTable, vCell;

   try

   {

   vWordApp = Variant::CreateObject("Word.Application");

   }

   catch(...)

   {

   MessageBox(0, "启动 Word 出错, 可能是没有安装Word.",

   "ListView2Doc", MB_OK MB_ICONERROR);

   vWordApp = Unassigned;

   return;

   }

   // 隐藏Word界面

   vWordApp.OlePropertySet("Visible", false);

   // 新建一个文档

   vWordApp.OlePropertyGet("Documents").OleFunction("Add");

   //

   Variant vSelect = vWordApp.OlePropertyGet("Selection");

   // 设置一下字体,大小

   vSelect.OlePropertyGet("Font").OlePropertySet("Size", lv->Font->Size);

   vSelect.OlePropertyGet("Font").OlePropertySet("Name", lv->Font->Name.c_str());

   // 要插入表格的行数

   int nRowCount(lv->Items->Count + 1);

   nRowCount = nRowCount < 2? 2: nRowCount;

   // 要插入表格的列数

   int nColCount(lv->Columns->Count);

   nColCount = nColCount < 1? 1: nColCount;

   // 在Word文档中插入与ListView行数列数相同的一个表格

   vWordApp.OlePropertyGet("ActiveDocument").OlePropertyGet("Tables")

   .OleProcedure("Add",

   vSelect.OlePropertyGet("Range"),

   nRowCount, // 行数

   nColCount, // 列数

   1, // DefaultTableBehavior:=wdWord9TableBehavior

   0); // AutoFitBehavior:=wdAutoFitFixed

   // 操作这个表格

   vTable = vWordApp.OlePropertyGet("ActiveDocument").

   OleFunction("Range").OlePropertyGet("Tables").OleFunction("Item", 1);

   // 设置单元格的宽度

   for(int i=0; i

   {

   int nColWidth;

   if(lv->Columns->Count > i)

   nColWidth = lv->Columns->Items[i]->Width;

   else

   nColWidth = 100; // 假如没有设置列,就随便设置个默认值

   vTable.OlePropertyGet("Columns").OleFunction("Item", i + 1)

   .OlePropertySet("PreferredWidthType", 3); // wdPreferredWidthPoints

   vTable.OlePropertyGet("Columns").OleFunction("Item", i + 1)

   .OlePropertySet("PreferredWidth", nColWidth * 2);

   }

   //----------------------------------------------------------------------------

   // 抱歉,这个提示又来了,为了防止不负责任的转载者,只好在此留些信息。

   // 作者:ccrun(老妖) info@ccrun.com

   // 本文转自 C++Builder 研究 - http://www.ccrun.com/article/go.ASP?i=633&d=vytvc6

   //----------------------------------------------------------------------------

   // 将列名写入Word表格先

   for(int i=0; iColumns->Count; i++)

   {

   vCell = vTable.OleFunction("Cell", 1, i + 1);

   vCell.OlePropertySet("Range", lv->Columns->Items[i]->Caption.c_str());

   // 列名单元格背景颜色 // wdColorGray125

   vCell.OlePropertyGet("Shading")

   .OlePropertySet("BackgroundPatternColor", 14737632);

   }

   // 将ListView中的数据写入Word表格

   for(int i=0; i

   {

   // 63 63 72 75 6E 2E 63 6F 6D

   vCell = vTable.OleFunction("Cell", i + 2, 1);

   vCell.OlePropertySet("Range", lv->Items->Item[i]-&

  

  

精彩图集

赞助商链接