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;
}
}
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;
}
}
相关文章推荐
- delphi 的一个导出excel的方法的类
- 详讲:C#快速导出多个sheet到excel的两种方法(Cell和Range方法), 解决了(导入时外部表不是预期的格式)
- 如何快速导出数据库为EXCEL文档(另一种方法)
- SQL Server中导入/导出 Excel (包括SQL实现和Net代码实现)的基本方法
- 从SQL Server中导入/导出 Excel 的基本方法
- Java利用xml将大批量数据导出到excel的一个方法
- SQL_Access_Excel导出导入工具(包含Delphi源代码)
- 从SQL Server中导入/导出 Excel 的基本方法
- 从SQL Server中导入/导出 Excel 的基本方法
- (Delphi)Excel的快速导入
- PHP导入导出Excel方法
- (转) 导入/导出 Excel 的基本方法
- PHP导入导出Excel的方法小结
- 比较简单的导入导出EXCEL数据的方法
- 从SQL Server中导入/导出 Excel 的基本方法
- 从SQL Server中导入/导出 Excel 的基本方法
- 在sql中快速导入、导出Excel
- SQL SERVER使用OpenRowset,、OpenDataSource函数导入、导出数据到Excel 的几种方法(整理)
- 从SQL Server中导入/导出 Excel 的基本方法
- Delphi中Excel的快速导入