首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

请问!如何把chart画出的图导出到excel

2013-09-07 
请教!怎么把chart画出的图导出到excel? if(SaveDialog1-Execute()){Variant vExcelApp, vSheet , Range ,

请教!怎么把chart画出的图导出到excel?


 if(SaveDialog1->Execute())
{
        Variant vExcelApp, vSheet , Range , Chart;
        try
        {
                    vExcelApp = Variant::CreateObject("Excel.Application");
        }
        catch(...)
        {
                    MessageBox(0, "启动 Excel 出错, 可能是没有安装Excel.",
                    "ListView2Excel", MB_OK | MB_ICONERROR);
                    vExcelApp = Unassigned;
                    return;
        }
        DisplayInfoWhetherChanged(false);
        AnsiString FilePath=SaveDialog1->FileName;
        // 隐藏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", Find_FileName_Info->Font->Size);


        vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
                .OlePropertySet("Name", Find_FileName_Info->Font->Name.c_str());
        vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
                .OlePropertySet("FontStyle", "常规");
        vSheet.OlePropertyGet("Cells", 1, 1).OleProcedure("Select");
        // 表格的行数
        int nRowCount(Find_FileName_Info->Items->Count + 1);
        nRowCount = nRowCount < 2? 2: nRowCount;
        // 设置单元格的宽度
        vExcelApp.OlePropertyGet("Columns",2).OlePropertySet("ColumnWidth",22);
        // 设置单元格字体居中
        vExcelApp.OlePropertyGet("Cells").OlePropertySet("HorizontalAlignment",3);
        // 先将列名写入Excel表格
        for(int j=0; j<Find_FileName_Info->Columns->Count; j++)
        {

                    // 标题行的行高
                    vExcelApp.OlePropertyGet("Rows", 1).OlePropertySet("RowHeight", 20);
                    // 写入标题
                    vSheet.OlePropertyGet("Cells", 1, j + 1)
                    .OlePropertySet("Value",
                    Find_FileName_Info->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
        }
         pTimes=Now().Val*100000;
        // 将ListView中的数据写入Excel表格
        for(int i=0; i<nRowCount - 1; i++)
        {
                    //设置编号列的颜色
                    Variant vInter = vSheet.OlePropertyGet(
                    "Cells", i + 2, 1).OlePropertyGet("Interior");
                    vInter.OlePropertySet("ColorIndex", 15); // 灰色
                    vInter.OlePropertySet("Pattern", 1); // xlSolid
                    vInter.OlePropertySet("PatternColorIndex", -4105); // xlAutomatic


                    // 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", Find_FileName_Info->Items->Item[i]->Caption.c_str());


                    for(int j=0; j<Find_FileName_Info->Items->Item[i]->SubItems->Count; j++)
                    {
                            vSheet.OlePropertyGet("Cells", i + 2, j + 2)
                                    .OlePropertySet("Value",
                            Find_FileName_Info->Items->Item[i]->SubItems->Strings[j].c_str());
                    }
        }

        //计时
        cTimes=Now().Val*100000;
        const AnsiString Precision="0.000";
        Label7->Caption = FormatFloat(Precision,cTimes-pTimes) +" 秒";

        // 保存Excel文档并退出
        vExcelApp.OlePropertyGet("ActiveWorkbook")
                .OleFunction("SaveAs", FilePath.c_str());
        vSheet = Unassigned;
        vExcelApp = Unassigned;

        DisplayInfoWhetherChanged(true);
        }
  }

C++builder 导出excel
[解决办法]
试试这个
  ExcelApplication1->set_Caption(WideString("测试Test")); 


  ExcelApplication1->set_Visible(0,true);

  ExcelWorkbook1->ConnectTo(ExcelApplication1->Workbooks->Add(TNoParam(), 0));
  ExcelWorksheet1->ConnectTo(ExcelWorkbook1->Worksheets->get_Item(TVariant(1)));
  RangePtr r;
  Chart1->CopyToClipboardBitmap();
  r = ExcelWorksheet1->get_Range(TVariant("a2"),TVariant("e13"));
  ExcelWorksheet1->Paste(TVariant(LPDISPATCH(r)), TNoParam(), 0);


[解决办法]
本帖最后由 ccrun 于 2013-09-05 13:48:07 编辑 OLE专业户来也!!!

#include <clipbrd.hpp>

// 创建一个位图对象
Graphics::TBitmap *bmp = new Graphics::TBitmap;
bmp->Width = Chart1->Width;
bmp->Height = Chart1->Height;

// 创建一个控件画布对象
TControlCanvas *c = new TControlCanvas;
c->Control = Chart1;

// 将Chart上的图像复制到位图中
TRect rct(0, 0, Chart1->Width, Chart1->Height);
bmp->Canvas->CopyRect(rct, c, rct);

// 将位图中的图像复制到剪贴板
Clipboard()->Assign(bmp);

delete c;
delete bmp;

// 创建Excel对象
Variant vExcelApp;
try
{
    vExcelApp = Variant::CreateObject("Excel.Application");
}
catch(...)
{
    MessageBox(0, "启动 Excel 出错, 可能是没有安装Excel.",
        "", MB_OK 
[解决办法]
 MB_ICONERROR);

    return;
}

// 使其可视
vExcelApp.OlePropertySet("Visible", true);

// 新建一个工作表
vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); // 工作表

// 执行粘贴操作
vExcelApp.OlePropertyGet("ActiveSheet").OleProcedure("Paste");

// 后续代码忽略
// ...

热点排行