如何将DBGrid 中的数据导入到EXCEL中

来源:互联网 发布:python 贝叶斯分类 编辑:程序博客网 时间:2024/05/21 07:00

如何将DBGrid 中的数据导入到EXCEL中

//--------------------------------------------------------------------------- 
// 将DBGrid中的数据导出到Word文档 
// v0.1 by ccrun(老妖) 2005.10.13 1:40 
//--------------------------------------------------------------------------- 
void __fastcall DBGrid2Word(TDBGrid *dbg, String strDocFile) 

if(!dbg->DataSource->DataSet->Active) // 数据集没有打开就返回 
return; 
Variant vWordApp, vTable, vCell; 
try 

vWordApp = Variant::CreateObject("Word.Application"); 

catch(...) 

MessageBox(0, "启动 Word 出错, 可能是没有安装Word.", 
"DBGrid2Word", 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", dbg->Font->Size); 
vSelect.OlePropertyGet("Font").OlePropertySet("Name", dbg->Font->Name.c_str()); 
// 要插入表格的行数 
int nRowCount(dbg->DataSource->DataSet->RecordCount + 1); 
nRowCount = nRowCount < 2? 2: nRowCount; 
// 要插入表格的列数 
int nColCount(dbg->Columns->Count); 
nColCount = nColCount < 1? 1: nColCount; 
// 在Word文档中插入与DBGrid行数列数基本相同的一个表格 
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 = dbg->Columns->Items[i]->Width; 
vTable.OlePropertyGet("Columns").OleFunction("Item", i + 1) 
.OlePropertySet("PreferredWidthType", 3); // wdPreferredWidthPoints 
vTable.OlePropertyGet("Columns").OleFunction("Item", i + 1) 
.OlePropertySet("PreferredWidth", nColWidth); 

//---------------------------------------------------------------------------- 
// 抱歉,这个提示又来了,为了防止不负责任的转载者,只好在此留些信息。 
// 作者:ccrun(老妖) info@ccrun.com 
// 本文转自 C++Builder 研究 - http://www.ccrun.com/article/go.asp?i=635&d=g75jbn 
//---------------------------------------------------------------------------- 
// 先将列名写入Word表格 
for(int j=0; j <dbg->Columns->Count; j++) 

vCell = vTable.OleFunction("Cell", 1, j + 1); 
vCell.OlePropertySet("Range", dbg->Columns->Items[j]->FieldName.c_str()); 
// 列名单元格背景颜色 // wdColorGray125 
vCell.OlePropertyGet("Shading") 
.OlePropertySet("BackgroundPatternColor", 14737632); 

// 将DBGrid中的数据写入Word表格 
dbg->DataSource->DataSet->First(); 
for(int i=0; i <nRowCount; i++) 

// 63 63 72 75 6E 2E 63 6F 6D 
for(int j=0; j <dbg->Columns->Count; j++) 

vCell = vTable.OleFunction("Cell", i + 2, j + 1); 
vCell.OlePropertySet("Range", 
dbg->DataSource->DataSet->FieldByName( 
dbg->Columns->Items[j]->FieldName)->AsString.c_str()); 

dbg->DataSource->DataSet->Next(); 

// 保存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, "DBGrid2Word 转换结束!", 
"DBGrid2Word", MB_OK | MB_ICONINFORMATION); 

//--------------------------------------------------------------------------- 
// 将DBGrid中的数据导出到Excel文档 
// v0.1 by ccrun(老妖) 2005.10.13 1:55 
//--------------------------------------------------------------------------- 
void __fastcall DBGrid2Excel(TDBGrid *dbg, String strXlsFile) 

if(!dbg->DataSource->DataSet->Active) // 数据集没有打开就返回 
return; 
Variant vExcelApp, vSheet; 
try 

vExcelApp = Variant::CreateObject("Excel.Application"); 

catch(...) 

MessageBox(0, "启动 Excel 出错, 可能是没有安装Excel.", 
"DBGrid2Excel", MB_OK | MB_ICONERROR); 
return; 

// 隐藏Excel界面 
vExcelApp.OlePropertySet("Visible", false); 
// 新建一个工作表 
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", dbg->Font->Size); 
vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font") 
.OlePropertySet("Name", dbg->Font->Name.c_str()); 
vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font") 
.OlePropertySet("FontStyle", "常规"); 
vSheet.OlePropertyGet("Cells", 1, 1).OleProcedure("Select"); 
// 表格的行数 
int nRowCount(dbg->DataSource->DataSet->RecordCount + 1); 
nRowCount = nRowCount < 2? 2: nRowCount; 
// 表格的列数 
int nColCount(dbg->Columns->Count); 
nColCount = nColCount < 1? 1: nColCount; 
// 设置单元格的宽度 
for(int i=0; i <nColCount; i++) 

int nColWidth = dbg->Columns->Items[i]->Width; 
vExcelApp.OlePropertyGet("Columns", i + 1) 
.OlePropertySet("ColumnWidth", nColWidth / 7); 

//---------------------------------------------------------------------------- 
// 抱歉,这个提示又来了,为了防止不负责任的转载者,只好在此留些信息。 
// 作者:ccrun(老妖) info@ccrun.com 
// 本文转自 C++Builder 研究 - http://www.ccrun.com/article/go.asp?i=635&d=g75jbn 
//---------------------------------------------------------------------------- 
// 先将列名写入Excel表格 
for(int j=0; j <dbg->Columns->Count; j++) 

// 标题行的行高 
vExcelApp.OlePropertyGet("Rows", 1).OlePropertySet("RowHeight", 20); 
// 
vSheet.OlePropertyGet("Cells", 1, j + 1) 
.OlePropertySet("Value", 
dbg->Columns->Items[j]->FieldName.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 

// 将DBGrid中的数据写入Excel表格 
dbg->DataSource->DataSet->First(); 
for(int i=0; i <nRowCount; i++) 

// 普通数据行的行高16 
vExcelApp.OlePropertyGet("Rows", i + 2).OlePropertySet("RowHeight", 16); 
// 63 63 72 75 6E 2E 63 6F 6D 
for(int j=0; j <dbg->Columns->Count; j++) 

vSheet.OlePropertyGet("Cells", i + 2, j + 1) 
.OlePropertySet("Value", 
dbg->DataSource->DataSet->FieldByName( 
dbg->Columns->Items[j]->FieldName)->AsString.c_str()); 

dbg->DataSource->DataSet->Next(); 

// 保存Excel文档并退出 
vExcelApp.OlePropertyGet("ActiveWorkbook") 
.OleFunction("SaveAs", strXlsFile.c_str()); 
vExcelApp.OleFunction("Quit"); 
vSheet = Unassigned; 
vExcelApp = Unassigned; 
// 工作结束 
MessageBox(0, "DBGrid2Excel 转换结束!", 
"DBGrid2Excel", MB_OK | MB_ICONINFORMATION);