void __fastcall TfrmSytQuery::Button3Click(TObject *Sender)
{ if(!DBGrid1->DataSource->DataSet->Active) // 数据集没有打开就返回
return;
SaveDialog1->Title = "保存文件";
SaveDialog1->Filter = "excel文件(*.xls)|*.xls";
if (SaveDialog1->Execute())
{
Variant vExcelApp, vSheet;
try
{
vExcelApp = Variant::CreateObject("Excel.Application");
}
catch(...)
{
MessageBox(0, "启动 Excel 出错, 可能是没有安装Excel.",
"DBGrid1rid2Excel", 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", DBGrid1->Font->Size);
vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
.OlePropertySet("Name", DBGrid1->Font->Name.c_str());
vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
.OlePropertySet("FontStyle", "常规");
vSheet.OlePropertyGet("Cells", 1, 1).OleProcedure("Select");
// 表格的行数
// int nRowCount(DBGrid1->DataSource->DataSet->RecordCount + 1);
int nRowCount(DBGrid1->DataSource->DataSet->RecordCount );
nRowCount = nRowCount < 2? 2: nRowCount;
// 表格的列数
int nColCount(DBGrid1->Columns->Count);
nColCount = nColCount < 1? 1: nColCount;
// 设置单元格的宽度
for(int i=0; i<nColCount; i++)
{
int nColWidth = DBGrid1->Columns->Items[i]->Width;
vExcelApp.OlePropertyGet("Columns", i + 1)
.OlePropertySet("ColumnWidth", nColWidth / 7);
}
// 先将列名写入Excel表格
for(int j=0; j<DBGrid1->Columns->Count; j++)
{
// 标题行的行高
vExcelApp.OlePropertyGet("Rows", 1).OlePropertySet("RowHeight", 20);
//
// vSheet.OlePropertyGet("Cells", 1, j + 1)
// .OlePropertySet("HorizontalAlignment",2);
vSheet.OlePropertyGet("Cells", 1, j + 1)
.OlePropertySet("Value",
DBGrid1->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
}
// 将DBGrid1rid中的数据写入Excel表格
DBGrid1->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
AnsiString card;
char endcard[20];
memset(endcard,0,sizeof(endcard));
strcpy(endcard,"'");
for(int j=0; j<DBGrid1->Columns->Count; j++)
{ if(( j == 3 ) || (j == 4)) //为了使得在输出时卡号为字符串,在第4,5列的字段前加了“‘”号
{
card=DBGrid1->DataSource->DataSet->FieldByName(DBGrid1->Columns->Items[j]->FieldName)->AsString.c_str();
// MessageBox(0, card.c_str(), "导出提示", MB_OK | MB_ICONINFORMATION);
strcat(endcard,card.c_str());
vSheet.OlePropertyGet("Cells", i + 2, j + 1)
.OlePropertySet("Value",endcard);
memset(endcard,0,sizeof(endcard));
strcpy(endcard,"'");
}
else
{ vSheet.OlePropertyGet("Cells", i + 2, j + 1)
.OlePropertySet("Value",
DBGrid1->DataSource->DataSet->FieldByName(
DBGrid1->Columns->Items[j]->FieldName)->AsString.c_str());
}
}
DBGrid1->DataSource->DataSet->Next();
}
// 保存Excel文档并退出
vExcelApp.OlePropertyGet("ActiveWorkbook")
.OleFunction("SaveAs", SaveDialog1->FileName.c_str());
vExcelApp.OleFunction("Quit");
vSheet = Unassigned;
vExcelApp = Unassigned;
// 工作结束
MessageBox(0, "导出结束!",
"导出提示", MB_OK | MB_ICONINFORMATION);
// ex.OleFunction("Quit");//退出Excel
}
}