您的位置:首页 > 编程语言 > Delphi

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

2015-02-13 11:03 429 查看
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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: