图形图像 多媒体类 机械电子 办公系列 程序设计 网站开发 数据库教程 路由技术 网络原理 网络应用 考试认证
手机分类
  网站导航: 电脑时代在线 · 程序设计 · C++语言 · C++OLECOMDLLXML

C++里面将ListView里面地内容导出到Word和Excel(新)

手机资讯
『C++里面将ListView里面地内容导出到Word和Excel(新)』如果文章中有大量图片,显示会较慢,请等待图片下载完成
 
点击数: 来源:本站原创 更新时间:2006-3-10 11:43:23 
经常看到有网友发帖子询问如何将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<nColCount; 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; i<lv->Columns->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<nRowCount - 1; i++)
    {
        // 63 63 72 75 6E 2E 63 6F 6D
        vCell = vTable.OleFunction("Cell", i + 2, 1);
        vCell.OlePropertySet("Range", lv->Items->Item[i]->Caption.c_str());
        for(int j=0; j<lv->Items->Item[i]->SubItems->Count; j++)
        {
            if(lv->Columns->Count > j)
            {
                vCell = vTable.OleFunction("Cell", i + 2, j + 2);
                vCell.OlePropertySet("Range",
                        lv->Items->Item[i]->SubItems->Strings[j].c_str());
            }
        }
    }
    // 保存Word文档并退出
    vWordApp.OlePropertyGet("ActiveDocument")
            .OleProcedure("SaveAs", strDocFile.c_str());
    vWordApp.OlePropertyGet("ActiveDocument").OleProcedure("Close");
    Application->ProcessMessages();
    vWordApp.OleProcedure("Quit");
    Application->ProcessMessages();
    vWordApp = Unassigned;
    // 工作结束
    MessageBox(0, "ListView2Doc 转换结束!",
            "ListView2Doc", MB_OK | MB_ICONINFORMATION);
}
//---------------------------------------------------------------------------
// 将ListView中的内容导出到Excel文档
// v0.2 by ccrun(老妖) 2005.10.13
//---------------------------------------------------------------------------
void __fastcall ListView2Excel(TListView *lv, String strXlsFile)
{
    Variant vExcelApp, vSheet;
    try
    {
        vExcelApp = Variant::CreateObject("Excel.Application");
    }
    catch(...)
    {
        MessageBox(0, "启动 Excel 出错, 可能是没有安装Excel.",
                "ListView2Excel", MB_OK | MB_ICONERROR);
        vExcelApp = Unassigned;
        return;
    }
    // 隐藏Excel界面
    vExcelApp.OlePropertySet("Visible"true);
    // 新建一个工作表
    vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); // 工作表
    // 操作这个工作表
    vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook")
            .OlePropertyGet("Sheets", 1);
    // 设置Excel文档的字体
    vSheet.OleProcedure("Select");
    vSheet.OlePropertyGet("Cells").OleProcedure("Select");
    vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
            .OlePropertySet("Size", lv->Font->Size);
    vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
            .OlePropertySet("Name", lv->Font->Name.c_str());
    vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
            .OlePropertySet("FontStyle""常规");
    vSheet.OlePropertyGet("Cells", 1, 1).OleProcedure("Select");
    // 表格的行数
    int nRowCount(lv->Items->Count + 1);
    nRowCount = nRowCount < 2? 2: nRowCount;
    // 表格的列数
    int nColCount(lv->Columns->Count);
    nColCount = nColCount < 1? 1: nColCount;
    // 设置单元格的宽度
    for(int i=0; i<nColCount; i++)
    {
        int nColWidth;
        if(lv->Columns->Count > i)
            nColWidth = lv->Columns->Items[i]->Width;
        else
            nColWidth = 100; // 如果没有设置列,就随便设置个默认值
        vExcelApp.OlePropertyGet("Columns", i + 1)
                .OlePropertySet("ColumnWidth", nColWidth / 2.6);
    }
    // 先将列名写入Excel表格
    for(int j=0; j<lv->Columns->Count; j++)
    {
        // 标题行的行高
        vExcelApp.OlePropertyGet("Rows", 1).OlePropertySet("RowHeight", 20);
        // 写入标题
        vSheet.OlePropertyGet("Cells", 1, j + 1)
                .OlePropertySet("Value",
                lv->Columns->Items[j]->Caption.c_str());
        // 设置列名单元格的背景色
        Variant vInter = vSheet.OlePropertyGet(
                "Cells", 1, j + 1).OlePropertyGet("Interior");
        vInter.OlePropertySet("ColorIndex", 15); // 灰色
        vInter.OlePropertySet("Pattern", 1); // xlSolid
        vInter.OlePropertySet("PatternColorIndex", -4105); // xlAutomatic
    }
    //----------------------------------------------------------------------------
    // 抱歉,这个提示又来了,为了防止不负责任的转载者,只好在此留些信息。
    // 作者:ccrun(老妖) info@ccrun.com
    // 本文转自 C++Builder 研究 - http://www.ccrun.com/article/go.asp?i=633&d=vytvc6
    //----------------------------------------------------------------------------   
    // 将ListView中的数据写入Excel表格
    for(int i=0; i<nRowCount - 1; i++)
    {
        // 63 63 72 75 6E 2E 63 6F 6D
        // 普通数据行的行高16
        vExcelApp.OlePropertyGet("Rows", i + 2).OlePropertySet("RowHeight", 16);
        //
        vSheet.OlePropertyGet("Cells", i + 2, 1)
                .OlePropertySet("Value", lv->Items->Item[i]->Caption.c_str());
        for(int j=0; j<lv->Items->Item[i]->SubItems->Count; j++)
        {
            vSheet.OlePropertyGet("Cells", i + 2, j + 2)
                .OlePropertySet("Value",
                lv->Items->Item[i]->SubItems->Strings[j].c_str());
        }
    }
    // 保存Excel文档并退出
    vExcelApp.OlePropertyGet("ActiveWorkbook")
            .OleFunction("SaveAs", strXlsFile.c_str());
    vExcelApp.OleFunction("Quit");
    vSheet = Unassigned;
    vExcelApp = Unassigned;
    // 工作结束
    MessageBox(0, "ListView2Excel 转换结束!",
            "ListView2Excel", MB_OK | MB_ICONINFORMATION);
}

// 测试代码
ListView2Word(ListView1, "C:\\ccrun\\234.doc");
ListView2Excel(ListView1, "C:\\ccrun\\234.xls");
】【关闭窗口
  上一页:
  下一页:
 ·网站导航: 电脑时代在线 · 程序设计 · C++语言 · C++OLECOMDLLXML
C++OLECOMDLLXML:相关文章
C++OLECOMDLLXML点击榜
推荐教程C++里面将DBGrid里面地数据导出
推荐教程C++里面将ListView里面地内容导
推荐教程C++里面怎么样显示/隐藏IE浏览器
推荐教程C++里面用BCB编SQL Server2000地
普通教程ActiveX控件注册地几种办法
普通教程怎么样改变CppWebBrowser地Html
普通教程怎么样在C++ Builder里面创建运
普通教程运用Shell对象控制Windows系统
普通教程怎么样在BCB里面调整测试Active
普通教程COM编制程序初步掌握1
搞笑自拍|图片故事|美女图库|体坛宝贝|明星爆料|世界奇观|风光摄影|历史回忆|大千世界
C++OLECOMDLLXML推荐图片

没有任何图片教程
advertisement
关于站点 - 广告服务 - 联系我们 - 版权隐私 - 免责声明 - 合作伙伴 - 程序支持 - 网站地图 - 返回顶部  
网站文本地图
  版权所有:电脑时代在线 2005-2009 欢迎各种媒体转载我们的原创作品[转载请注明出处]
copyright © 2005-2007 www.PCvz.com online services. all rights reserved. 蜀ICP备05015578
Template designed by PCX. Optimized for 1024x768 to Firefox,Opera and MS-IE. Site powered by EQL.
红盾
热爱电脑,热爱生活
拥有电脑,拥有生命
让我们享受拥有电脑的时光