Delphi 导出、导入Excel的一个快速方法

The quickest way is to use an array of Variant,and just pass the entire array to Excel:

var

    xls, wb, Range: OLEVariant;

    arrData: Variant;

    RowCount, ColCount, i, j: Integer;

begin

    {create variant array where we'll copy our data}

    RowCount := StringGrid1.RowCount;

    ColCount := StringGrid1.ColCount;

    arrData := VarArrayCreate([1, RowCount,1, ColCount], varVariant);

    {fill array}

    for i:=1to RowCountdo

        for j:=1to ColCountdo

            arrData[i, j]:= StringGrid1.Cells[j-1, i-1];

    {initialize an instance of Excel}

    xls := CreateOLEObject('Excel.Application');

    {create workbook}

    wb := xls.Workbooks.Add;

    {retrieve a range where data must be placed}

    Range := wb.WorkSheets[1].Range[wb.WorkSheets[1].Cells[1,1],

        wb.WorkSheets[1].Cells[RowCount, ColCount]];

    {copy data from allocated variant array}

    Range.Value:= arrData;

    {show Excel with our data}

    xls.Visible:=True;

end;


转换为C++builder方式为:

#define   PG   OlePropertyGet
#define   PS   OlePropertySet
#define   FN   OleFunction
#define   PR   OleProcedure

void __fastcall TForm1::btn2Click(TObject *Sender)

{

    Variant vExcelApp, vSheet;
    try
    {
        vExcelApp = Variant::CreateObject("Excel.Application");
    }
    catch(...)
    {
        // excel没有安装,或者没有正确安装
        // 如果没有正确安装,则会运行时异常:
        // Exception class EOleSysError with message '无效的类字符串'.
        // 这个时候需要重新安装excel或者修复excel
        MessageBox(0, _T("启动 Excel 出错, 可能是没有安装Excel."), _T("警告"),
            MB_OK | MB_ICONERROR);
        return;
    }
    // 隐藏Excel界面
    vExcelApp.OlePropertySet("Visible", false);
    // 新建一个工作表
    vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1);
    // 操作这个工作表
    vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet
        ("Sheets", 1);

    // 创建二维数组,下标从0开始
    int bounds[4] = {0,2,0,2};//{0,列数,0,行数}
    Variant variantValues = VarArrayCreate(bounds, 3, varVariant);
    variantValues.PutElement("a", 0,0);//(value,列,行)
    variantValues.PutElement("b", 1,0);
    variantValues.PutElement("c", 2,0);
    variantValues.PutElement("d", 0,1);
    variantValues.PutElement("e", 1,1);
    variantValues.PutElement("f", 2,1);
    variantValues.PutElement("g", 0,2);
    variantValues.PutElement("h", 1,2);
    variantValues.PutElement("i", 2,2);

    try{
        Variant excelCell = vSheet.PG("Cells");
        Variant cellRange = vSheet.PG(
            "Range",
            excelCell.PG("Item", 1, 1),  // start cell
            excelCell.PG("Item", 3, 3) // finishing cell
            );

        // place array into excel
        cellRange.PS("value",vExcelApp.FN("Transpose", variantValues));

        WideString wfileName("c:\\123.xlsx");
        if(FileExists(wfileName))
        {
            DeleteFile(wfileName);
        }
        vExcelApp.PG("ActiveWorkbook").FN("SaveAs",
            wfileName.c_bstr());
    }
    __finally
    {
        vExcelApp.PS("Visible",true);
        //vExcelApp.OleFunction("Quit");
        vSheet = Unassigned;
        vExcelApp = Unassigned;
    }

}


导入:

void __fastcall TForm1::btn3Click(TObject *Sender)
{
    Variant vExcelApp, vSheet;
    try
    {
        vExcelApp = Variant::CreateObject("Excel.Application");
    }
    catch(...)
    {
        // excel没有安装,或者没有正确安装
        // 如果没有正确安装,则会运行时异常:
        // Exception class EOleSysError with message '无效的类字符串'.
        // 这个时候需要重新安装excel或者修复excel
        MessageBox(0, _T("启动 Excel 出错, 可能是没有安装Excel."), _T("警告"),
            MB_OK | MB_ICONERROR);
        return;
    }
    // 隐藏Excel界面
    vExcelApp.PS("Visible", false);
    WideString wfileName("c:\\123.xlsx");
    // 打开
    vExcelApp.PG("Workbooks").FN("Open", wfileName.c_bstr());
    // 操作这个工作表
    vSheet = vExcelApp.PG("ActiveWorkbook").PG("Sheets", 1);

    try{
        Variant excelCell = vSheet.PG("Cells");
        Variant cellRange = vSheet.PG(
            "Range",
            excelCell.PG("Item", 1, 1),  // start cell
            excelCell.PG("Item", 3, 3) // finishing cell
            );

        Variant  variantValues = cellRange.PG("value"); 
        for(int i=1; i<4; ++i)//下标从1开始
        {
            for(int j=1; j<4; ++j)
            {
                Variant data = variantValues.GetElement(i,j);//GetElement(行,列)
                String tmp = data;
                int a = 0;
            }
        }
    }
    __finally
    {
        vExcelApp.PS("Visible",true);
        //vExcelApp.OleFunction("Quit");
        vSheet = Unassigned;
        vExcelApp = Unassigned;
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值